调用JNI_CreateJavaVM总是返回-1

时间:2019-05-24 09:54:15

标签: android c++ xamarin.android java-native-interface dlsym

我正在使用带有C ++部分的Xamarin.Android应用程序。现在,我需要从C ++库直接调用Android Java接口。

我从Caleb Fenton's detailed and very helpful blog post复制了代码,该代码使用JNI将从C ++调用到Java 。但是我不能像他那样获得指向JVM的指针。

(顺便说一下,我主要是C#程序员,所以我在C ++中犯了一个基本错误是完全有可能的。)

在头文件中:

 #pragma once
class MyJniClass
{
    //Create this once and cache it.
    JavaVM *m_jvm;                      // Pointer to the JVM (Java Virtual Machine)
    JNIEnv *m_env;                      // Pointer to native interface
    bool init_jvm();
}

在.cpp文件中:

    #include <jni.h>
#include <dlfcn.h>
#include "MyJniClass.h"

typedef int(*JNI_CreateJavaVM_t)(void *, void *, void *);


/**Code is based on https://github.com/rednaga/native-shim/blob/master/vm.c  
*/
bool MyJniClass::init_jvm() 
{
    // https://android.googlesource.com/platform/frameworks/native/+/ce3a0a5/services/surfaceflinger/DdmConnection.cpp
    JavaVMOption opt[1];
    opt[0].optionString = "-Djava.class.path=."; // I added a small java class to the dll to which this C++ class is linked, 
                                                 //so that there would be a java class in the current directory.  

    //opt/*[1]*/.optionString = "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";


    JavaVMInitArgs args;
    args.version = JNI_VERSION_1_6;
    args.options = opt;
    args.nOptions = 1;
    args.ignoreUnrecognized = JNI_FALSE;

    void *libart_dso = dlopen("libart.so", RTLD_NOW); //libdvm.so is outdated,  libnativehelper.so doesn't work

    if (!libart_dso ) 
    {
        //Execution doesn't pass through here 
        return false;
    }

    //Try to get the JNI_CreateJavaVM function pointer
    JNI_CreateJavaVM_t JNI_CreateJavaVM;
    JNI_CreateJavaVM = (JNI_CreateJavaVM_t)dlsym(libart_dso, "JNI_CreateJavaVM");
    if (!JNI_CreateJavaVM) 
    {
        //Execution doesn't pass through here 
        return false;
    }

    signed int result = JNI_CreateJavaVM(&(m_jvm), &(m_env), &args);

    if ( result != 0)
    {
        ostringstream os;
        os << "Call to JNI_CreateJavaVM returned ";
        os << result;
        m_logger->writeEntry(Loglevel::debug, os.str()); // ===> Here, I can see that result is always -1
        return false;
    }

    return true;
}

我试图在ART源代码here中找到函数JNI_CreateJavaVM,但找不到。但是肯定应该在那里,以便dlsym可以找到该功能?我认为我必须进一步寻找libart.so的源代码。

我做错了什么,导致无法有效调用JNI_CreateJavaVM?

2 个答案:

答案 0 :(得分:0)

这里的第一个修改是添加诊断选项Xcheck:jni。 如果有错误,将提供详细信息。 通过将JavaVMOption opt[1];修改为JavaVMOption opt[2];来添加另一个选项 然后,添加以下选项: opt[1].optionString = "-Xcheck:jni"

此外,该dll必须从其原始位置加载(因为涉及其他DLL),而不是从您的项目目录中加载。 在以下帖子中提供了详细信息: JNI_CreateJavaVM() terminates with exit code 1

最后,您应该通过修改将指针投射到本机接口JNIEnv:

signed int result = JNI_CreateJavaVM(&(m_jvm), &(m_env), &args);

signed int result = JNI_CreateJavaVM(&(m_jvm), (void**)&(m_env), &args);

这应该可以解决问题。

答案 1 :(得分:0)

问题似乎与提供JNI_CreateJavaVM的库有关。我在这里参考这行

void *libart_dso = dlopen("libart.so", RTLD_NOW); 

如果我从与JVM不相关的所有内容中删除您的代码,并且如果我使用基于macOS的JDK,则效果很好。

--- 8< CUT HERE 8< ----
#include <jni.h>
#include <dlfcn.h>
#include <iostream>
#include "MyJniClass.h"

using namespace std;

bool MyJniClass::init_jvm()
{
    JavaVM *jvm;
    JNIEnv *env = NULL;
    JavaVMOption opt[1];
    opt[0].optionString = "-Djava.class.path=.";

    JavaVMInitArgs args;
    args.version = JNI_VERSION_1_6;
    args.options = opt;
    args.nOptions = 1;
    args.ignoreUnrecognized = JNI_FALSE;

    int status = JNI_CreateJavaVM (&jvm, (void **) &env, &args);

    cout << status << endl;

    return true;
}

int main(int argc, char **argv) {
  MyJniClass jni;
  jni.init_jvm();
}
--- 8< CUT HERE 8< ----
--- 8< CUT HERE 8< ----
#pragma once
class MyJniClass
{
    //Create this once and cache it.
    JavaVM *m_jvm;                      // Pointer to the JVM (Java Virtual Machine)
    JNIEnv *m_env;                      // Pointer to native interface
    public: bool init_jvm();
};
--- 8< CUT HERE 8< ----

然后,我编译代码:

g++ -o lib/recipeNo027_main c/recipeNo027_main.c \
        -I/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/include \
        -I/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/include/darwin/ \
    -rpath -L/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/lib/server -ljvm

(它基于此处的配方No027:http://jnicookbook.owsiak.org/recipe-no-027/

我可以毫无问题地运行它

> lib/recipeNo027_main
0

您的JNI_CreateJavaVM实现中似乎发生了一些混乱。