- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 android native 代码用我的自定义方法替换 android 系统调用,通过谷歌搜索我发现了一些博客,但在尝试给定代码时我遇到了错误。
通过关注博客
我正在使用此 blog 中的以下代码创建 android Native 方法
#include "com_appwrapping_tunneling_SimpleActivity.h"
int Java_com_appwrapping_tunneling_SimpleActivity_swap_1virtual_1methods(char *origclass, char *origmeth, char *newclass, char *newmeth) {
int i = 0;
ClassObject *newclazz = g_dvmfindloadedclass(newclass);
if (!newclazz) {
return 0;
}
ClassObject *oldclazz = g_dvmfindclass(origclass, newclazz->classLoader);
if (!oldclazz) {
return 0;
}
struct Method *oldm = NULL, *newm = NULL;
if (newclazz) {
for (i = 0; i < newclazz->vtableCount; i++) {
if(!strcmp(newclazz->vtable[i]->name, newmeth))
// this is the new method
newm = newclazz->vtable[i];
}
}
if (oldclazz) {
for (i = 0; i < oldclazz->vtableCount; i++) {
if(!strcmp(oldclazz->vtable[i]->name, origmeth)) {
// save old method
oldm = oldclazz->vtable[i];
// put new method in place of old
oldclazz->vtable[i] = newm;
}
}
}
if (!newm || !oldm) {
__android_log_print(ANDROID_LOG_ERROR, MYLOG_TAG, "failed to find methods/objects");
return 0;
}
// add some space for original method
oldclazz->vtable = g_dvmlinearrealloc(oldclazz->classLoader,
oldclazz->vtable,
sizeof(*(oldclazz->vtable)) * (oldclazz->vtableCount + 1));
// we put it at the end of the table
oldclazz->vtableCount++;
oldclazz->vtable[oldclazz->vtableCount - 1] = oldm;
// now new method gets old method name
newm->name = oldm->name;
char *fname = NULL;
// leaking memory here
fname = (char*) malloc(strlen(origmeth) + strlen(FAKE_PREFIX) + 1);
sprintf(fname, "%s%s", FAKE_PREFIX, origmeth);
// now old method will get _orig_ prefix, so it can be looked up later
oldm->name = fname;
// swap method indexes
newm->methodIndex = oldm->methodIndex;
// now old method gets proper index
oldm->methodIndex = oldclazz->vtableCount - 1;
g_dvmdumpclass(oldclazz, 1);
g_dvmdumpclass(newclazz, 1);
__android_log_write(ANDROID_LOG_DEBUG, MYLOG_TAG, "swap successful!");
return 1;
}
一切顺利,但在使用以下命令生成 .so 文件时:
<NDK-Home>$ ndk-build
我收到以下错误:
JNIApp/jni/testLib.c: In function 'swap_1virtual_1methods':
JNIApp/jni/testLib.c:6:3: error: unknown type name 'ClassObject'
JNIApp/jni/testLib.c:6:27: warning: initialization makes pointer from integer without a cast [enabled by default]
JNIApp/jni/testLib.c:10:3: error: unknown type name 'ClassObject'
JNIApp/jni/testLib.c:10:61: error: request for member 'classLoader' in something not a structure or union
JNIApp/jni/testLib.c:14:25: error: 'NULL' undeclared (first use in this function)
JNIApp/jni/testLib.c:14:25: note: each undeclared identifier is reported only once for each function it appears in
JNIApp/jni/testLib.c:16:29: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:17:28: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:19:27: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:23:29: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:24:28: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:26:27: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:28:20: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:33:25: error: 'ANDROID_LOG_ERROR' undeclared (first use in this function)
JNIApp/jni/testLib.c:33:44: error: 'MYLOG_TAG' undeclared (first use in this function)
JNIApp/jni/testLib.c:37:11: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:37:49: error: request for member 'classLoader' in something not a structure or union
JNIApp/jni/testLib.c:38:31: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:39:40: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:39:62: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:41:11: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:42:11: error: request for member 'vtable' in something not a structure or union
JNIApp/jni/testLib.c:42:28: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:44:7: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:44:20: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:47:19: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
JNIApp/jni/testLib.c:47:26: warning: incompatible implicit declaration of built-in function 'strlen' [enabled by default]
JNIApp/jni/testLib.c:47:52: error: 'FAKE_PREFIX' undeclared (first use in this function)
JNIApp/jni/testLib.c:48:3: warning: incompatible implicit declaration of built-in function 'sprintf' [enabled by default]
JNIApp/jni/testLib.c:50:7: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:52:7: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:52:27: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:54:7: error: dereferencing pointer to incomplete type
JNIApp/jni/testLib.c:54:31: error: request for member 'vtableCount' in something not a structure or union
JNIApp/jni/testLib.c:57:23: error: 'ANDROID_LOG_DEBUG' undeclared (first use in this function)
make: *** [JNIApp/obj/local/armeabi-v7a/objs/testLib/testLib.o] Error 1
请帮助我。提前致谢。
最佳答案
您收到的错误是由于编译器无法找到您应用程序中包含的符号的有效定义而引起的。最可能的原因是您忘记在实现代码中包含 jni.h
。
#include <jni.h>
还要确保jni.h
文件位于您的include-path
上(添加上述行后)。如果您已包含它,但编译器找不到它,那么您的错误将表明这一点。
jni.h
包含在 Oracle 的 JDK 包中。不过,您可能需要一个特定于 Android 使用的 JVM。
编辑
您尝试使用的符号不是 JNI 符号...我正在考虑 jclass
。 ClassObject
是 Dvorak 虚拟机中类
的内部表示。从这里开始:
On Dalvik all Java class/object mapping to native C structs is happening in vm/oo/* files. Object instances are mirrored with ClassObject structs, and methods with Methods. So each ClassObject comes with 2d vtable array, which simply contains pointers to Methods.
因此您需要包含来自 vm/oo/
的 Dvorak VM header 。您正在尝试直接操作虚拟机的内部结构。您可能应该再次阅读该文章,以确保您已全部掌握。
关于java - 错误: unknown type name 'ClassObject' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24388279/
下面是我的代码,其中我定义了两个类 Node 和 List 来执行您认为它们执行的操作。在扩展列表时,我需要创建一个新的 Node 对象(请参阅代码中带有注释的行)。如果我使用 new Node(da
我原以为计数是 3,但结果是 0。我已经连续编码了 9 个小时,所以也许我已经盯着这个看太久了。 我对 Swift 的理解可能有一个漏洞。任何帮助表示赞赏。 struct Service { en
我正在尝试使用 android native 代码用我的自定义方法替换 android 系统调用,通过谷歌搜索我发现了一些博客,但在尝试给定代码时我遇到了错误。 通过关注博客 我正在使用此 blog
我需要给我的公共(public)成员(member)打电话。采用 1 个参数的构造函数。 这是我的代码的样子://主要 char tmpArray[100] = {}; while ( !inFile
我正在使用 Python 3.1,并且我想创建一个游戏。我做了一个class Board(Canvas):为什么?因为我需要通过“标签”来跟踪这些片段。但是,当我尝试将标签绑定(bind)到一 blo
我想了解 Class.forName("ClassName") 和 ClassObject.getClass返回类的运行时实例。那么为什么在比较从两次获取中获得的结果类对象时我们得到一个 boolea
我有一个 Blazor 项目,在 Startup.cs 中我添加了一个服务 public void ConfigureServices(IServiceCollection servi
我是一名优秀的程序员,十分优秀!