java - 错误: unknown type name 'ClassObject'

标签 java android c++ c android-ndk

我正在尝试使用 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 符号...我正在考虑 jclassClassObject 是 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/

相关文章:

java - 用于标准化英国电话号码的正则表达式

java - Android GoogleApiClient 应用程序在 connect() 时崩溃

android - 以编程方式克隆 Android 应用程序

c++ - 我无法初始化 C 类的 shared_ptr

c++ - C++中增量垃圾收集模拟的内存分配

java - BufferedReader 的 read() 方法如何工作?

java - 方法可以在不声明的情况下抛出 java.lang.Exception 吗?

java - 如何在 JDT TypeHierarchy 中设置区域(= java 元素集)参数?

android - 如何更改 ShareActionProvider 的颜色?

c++ - 无法将 CLion 链接到 Windows 10 上的 QT