如何在一个android进程的动态库C++里获取到JavaVM:
1、参考:Get JavaVM and AAssetManager in native c++ Android Library - Stack Overflow
If you want to cover both cases in the same way ("typical" and "native" app) - the only way is next:
- Add
MyLibrary.javato your library. That file should declare native methodMyLibrary.init(AssetManager am);. Implementation of that method must be placed in your C++ code, where you get java reference to asset manager and then can obtainAAssetManager *- Add
JNI_OnLoad()to your library in order to obtainJavaVM*- Build your library as separate
.soto allow system to invoke yourJNI_OnLoad()Usage looks next:
- Client app declares custom
Applicationclass in its manifest- Implementation of this custom class overrides
onAttachBaseContext()method, which implementation invokesSystem.loadLibrary("mylibrary")and then yourMyLibrary.init(getAssets()).
2、通过java调用jni时传下来的JNIEnv
How to get a reference to the JVM from a native android executable? - Stack Overflow
官网:
Android JNI学习(三)——Java与Native相互调用 - 简书
https://github.com/android/ndk-samples/tree/main/hello-jni
https://github.com/android/ndk-samples/tree/main/hello-jniCallback
本文详细介绍了在Android环境中,如何在C++动态库中获取JavaVM和JNIEnv,以及如何实现JAVA调用C++和C++调用JAVA的交互。主要方法包括通过JNI_OnLoad获取JavaVM,以及在Java中声明并调用C++的native方法传递AssetManager。同时,文章引用了StackOverflow上的解答和官方文档作为参考资料。
810

被折叠的 条评论
为什么被折叠?



