无法从JAR中找到自定义Java类,

发布于 2025-01-27 16:22:35 字数 2409 浏览 3 评论 0原文

我正在使用Visual Studio进行Android Anture-Activity应用程序,从头开始(我正在尝试首先在基本的测试应用程序中起作用)。它使用蚂蚁构建APK。

在我的本机代码中,我需要调用来自JAR文件的自定义Java方法(不是Java本机),因此我计划使用JNI。我发现jnii findClass无法使用,因为我的Java功能不是本地的,因此没有加载在类路径中。我正在使用此代码来从我的罐子里加载Jnihelpers类:

    const ANativeActivity* activity = state->activity;
    activity->vm->AttachCurrentThread(&env, 0);

    jobject   jobj = activity->clazz;
    jclass    clazz = env->GetObjectClass(jobj);
    jmethodID getClassLoader = env->GetMethodID(clazz, "getClassLoader", "()Ljava/lang/ClassLoader;");
    jobject   cls = env->CallObjectMethod(jobj, getClassLoader);
    jclass    classLoader = env->FindClass("java/lang/ClassLoader");
    jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    jstring strClassName = env->NewStringUTF("com.vivox.sdk.JniHelpers");
    jclass  jniHelpersClass = (jclass)(env->CallObjectMethod(cls, findClass, strClassName));
    env->DeleteLocalRef(strClassName);
    if (NULL == jniHelpersClass)
    {
        LOGW("Can't find the lib");
    }

现在我的问题是如何找到我的Java类?

在几个教程之后(下面列出),我知道我的罐子应该在我的 *.packaging的根部文件夹

    <target name="-pre-compile">
      <path id="project.all.jars.path">
        <path path="${toString:project.all.jars.path}"/>
        <fileset dir="${jar.libs.dir}">
          <include name="*.jar"/>
        </fileset>
      </path>
    </target>

下要正确复制(我认为?)中的生成的构建,但JNI/在classPath中仍然找不到。在教程中,它们都创建了一个自定义活动以使用system.loadlibrary,但是一旦我设置了androidManifest.xml- application:hascode:hascode to true to true to true to true to true oppode)我的C ++代码了。是否可以在不创建自定义活动的情况下加载JAR文件?或者,如果没有,输入自定义活动后,我该如何达到本机代码?

非常感谢,我已经被困住了很长时间了,所有帮助都非常感谢!

教程:

I'm working on an Android Native-Activity application with Visual Studio, starting from scratch (I'm trying to make it work in a basic test app first). It uses Ant to build the apk.

In my native code, I need to call a custom java method (not java native) coming from a jar file, so I plan on using JNI. I found that the jni->findClass wouldn't work because my java function is not native, thus not loaded in the classpath. I'm using this code instead to load the jniHelpers class from my jar :

    const ANativeActivity* activity = state->activity;
    activity->vm->AttachCurrentThread(&env, 0);

    jobject   jobj = activity->clazz;
    jclass    clazz = env->GetObjectClass(jobj);
    jmethodID getClassLoader = env->GetMethodID(clazz, "getClassLoader", "()Ljava/lang/ClassLoader;");
    jobject   cls = env->CallObjectMethod(jobj, getClassLoader);
    jclass    classLoader = env->FindClass("java/lang/ClassLoader");
    jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    jstring strClassName = env->NewStringUTF("com.vivox.sdk.JniHelpers");
    jclass  jniHelpersClass = (jclass)(env->CallObjectMethod(cls, findClass, strClassName));
    env->DeleteLocalRef(strClassName);
    if (NULL == jniHelpersClass)
    {
        LOGW("Can't find the lib");
    }

Now my question is how can my java class be found ?

Following a couple of tutorials (listed below), I understand that my jar should be under a libs folder at the root of my *.Packaging, and loaded from the build.xml here :

    <target name="-pre-compile">
      <path id="project.all.jars.path">
        <path path="${toString:project.all.jars.path}"/>
        <fileset dir="${jar.libs.dir}">
          <include name="*.jar"/>
        </fileset>
      </path>
    </target>

My jar file seems to be properly copied (I think?) into the generated build, but it still not found by JNI/in the classpath. In the tutorials, they all create a custom activity to use System.LoadLibrary, but as soon as I set the AndroidManifest.xml-application:hasCode to true (thus not using the built-in NativeActivity framework class), I can't reach my c++ code anymore. Is it possible to load the jar file without creating a custom activity ? Or if not, how can I reach my native code after entering my custom activity ?

Thanks a lot, I've been stuck on this for way too long already, all help is more than appreciated !

Tutorials :

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文