Search code examples
androidandroid-ndkjava-native-interface

Java functions, but without java (jni)?


I have one library 'libFOO.so ' which has java functions (JNI). Can I access these functions on a pure NDK?
I.e. for example we have a function (in this library, from IDA PRO):

void Java_com_Rubov_apptech_QueueActivity_jniAPKInit(
        JNIEnv_0 *env,
        jobject_0 clazz,
        jobject_0 assetManager)
{
  APKInit(assetManager);
}

can I access it using JNI methods? (Yes, I understand that this way I will have to rewrite a lot, but I need it)


Solution

  • The signature of the APKInit function implies that the library holding this function is getting JNIEnv on its own. Because the assetManager pointer is unusable without it. This pointer can be used only with a valid JNIEnv pointer. This means that the libFOO.so library most likely has the JNI_OnLoad function exported. And the library expects this function to be called on load. In such a way, the library can attach any thread to JavaVM and get the corresponding JNIEnv.

    So, unless you give more details about the libFOO library, I would say that it's not possible to use it from a purely native environment as is without exploring APKInit implementation to see how exactly it uses the assetManager pointer.

    But if we are talking about NDK and Android, then you can safely assume that all apps have a JavaVM instance. You just need to find it. Please provide more detail about your "pure NDK" setup. The purest NDK setup I'm aware of is NativeActivity. And it gives us JavaVM that can be passed into JNI_OnLoad of the libFOO.so library. After that, we would need to get a valid AssetManager jobject. This can be done without Java code by calling the getAssets method of ANativeActivity::clazz object.

    Also, it will be helpful if you give some insights about the root of the need to avoid using Java. Because the library obviously expects an instance of AssetManager that might exist only in an android application. And considering that you mentioned the NDK, I suppose you still target Android.