相关疑难解决方法(0)

如何在Android上的C中加载我自己的Java类?

我试图调用我使用Android NDK从C编写的一些Java代码.该应用程序是NativeActivity应用程序.我必须访问一些仅在Java中可用的功能,并且该功能要求您子类化另一个类,所以我不能直接从C执行调用.因此,我有这样的Java代码:

// src/com/example/my/package/SubClass.java
package com.example.my.package;

import android.foo.TheSuperClass;

public class SubClass extends TheSuperClass {
  public SubClass() {
    setImportantProperty(true);
  }
}
Run Code Online (Sandbox Code Playgroud)

我也有这样的C代码:

// Some file.c
void setThatImportantJavaProperty() {
  JavaVM *vm = AndroidGetJavaVM(); // This returns a valid JavaVM object
  JNIEnv* env;
  (*vm)->AttachCurrentThread(vm, &env, 0);

  jclass theSubClass = (*env)->FindClass(env, "com/example/my/package/SubClass");
  jthrowable exception = (*env)->ExceptionOccurred(env);
  if (exception) {
    (*env)->ExceptionDescribe(env);
    // This gives me: "java.lang.NoClassDefFoundError: [generic]".
    // Also, theSubClass is null, so the next line causes a segfault.
  }
  jmethodID theSubClassConstructor = (*env)->GetMethodID(env, theSubClass, …
Run Code Online (Sandbox Code Playgroud)

c java java-native-interface android android-ndk

10
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

android-ndk ×1

c ×1

java ×1

java-native-interface ×1