java.lang.UnsatisfiedLinkError: No implementation found (NDK, Kotlin) - Fixed

java.lang.UnsatisfiedLinkError: No implementation found (NDK, Kotlin) - Fixed

我在 C 中使用 NDK 的文件如下,我查看了 Kotlin 中的包名称和 class C (NDK) 中的包名称,我没有发现任何区别:

 jstring Java_com_test1_app1_ui_BaseActivity_getLocalKeyOld(JNIEnv* env, jobject obj, jstring id) {
    const char *nativeString = (*env)->GetStringUTFChars(env, id, 0);

    char* deviceIdEncode = base64encode(nativeString);
    int lenght = strlen(deviceIdEncode);

    jstring result;
    if(lenght < 16) {
        result = (*env)->NewStringUTF(env,"+-0wef2ccfeqa0-+");
    } else if(lenght > 16) {
        char subbuff[17];
        memcpy( subbuff, &deviceIdEncode[0], 16 );
        subbuff[16] = '[=10=]';
        result = (*env)->NewStringUTF(env,subbuff);
    } else {
        result = (*env)->NewStringUTF(env,deviceIdEncode);
    }
    return result;
}


jstring Java_com_test1_app1_ui_BaseActivity_getLocalKey(JNIEnv* env, jobject obj, jstring id) {
    const char *nativeString = (*env)->GetStringUTFChars(env, id, 0);

    char* deviceIdEncode = base64encodeNew(nativeString);
    int lenght = strlen(deviceIdEncode);

    jstring result;
    if(lenght < 16) {
        result = (*env)->NewStringUTF(env,"+-0wef2ccfeqa0-+");
    } else if(lenght > 16) {
        char subbuff[17];
        memcpy( subbuff, &deviceIdEncode[0], 16 );
        subbuff[16] = '[=10=]';
        result = (*env)->NewStringUTF(env,subbuff);
    } else {
        result = (*env)->NewStringUTF(env,deviceIdEncode);
    }
    return result;
}

我的 Kotlin class 包是包 com.test1.app1.ui

我使用这些行加载 lib 库:

abstract class BaseActivity : AppCompatActivity() {

    companion object {
        @JvmStatic
        external fun getLocalKey(id: String): String

        @JvmStatic
        external fun getLocalKeyOld(id: String): String
    }

我正在使用 NDK 和 Kotlin。该应用程序启动良好,但是当我将其置于后台时,打开其他应用程序并尝试转到我的应用程序时它崩溃了。我可以得到下一个信息:

E/zygote: No implementation found for java.lang.String com.test1.app1.ui.BaseActivity.getLocalKey(java.lang.String) 
    (tried Java_com_test1_app1_ui_BaseActivity_getLocalKey and Java_com_test1_app1_ui_BaseActivity_getLocalKey__Ljava_lang_String_2)




    Process: com.testLab.app1, PID: 31409
        java.lang.UnsatisfiedLinkError: No implementation found 
        for java.lang.String com.test1.app1.ui.BaseActivity.getLocalKey(java.lang.String) (tried Java_com_test1_app1_ui_BaseActivity_getLocalKey and Java_com_test1_app1_ui_BaseActivity_getLocalKey__Ljava_lang_String_2)
            at com_test1_app1_ui_BaseActivity.getLocalKey(Native Method)
            at com.test1.app1.ui.BaseActivity$Companion.getLocalKey(BaseActivity.kt:1)
            at com.test1.app1.services.Utils.decryptLocal(Utils.kt:620)
            at com.test1.app1.services.Utils.getUserPhone(Utils.kt:352)
            at com.test1.app1.ui.login.LoginActivity.initActivity(LoginActivity.kt:77)
            at com.test1.app1.ui.login.WelcomeActivity.onCreate(WelcomeActivity.kt:24)
            at android.app.Activity.performCreate(Activity.java:7032)
            at android.app.Activity.performCreate(Activity.java:7023)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6626)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

FIX(对我有用)

我通知我在 SplashActivity 调用 System.loadLibrary("nameofyourCFile")(SplashActivity 扩展了 BaseActivity),但其余活动不会加载 C 文件,因为当我去后台和回来时 SplashActivity 没有再次启动,所以我的库没有从任何地方加载。我将 System.loadLibrary() 放在我的 BaseActivity 中并且它有效!

P.S。谢谢@GenoChen先生的帮助和时间!

System.loadLibrary()(可能在 companion object {} 块中的 init {} 块内)对于 Java 运行时查找本机库(包含 "core" 的功能) 是。仅 @JvmStatic external fun(即 "surface" 或 "interface")是不够的。

如果您在加载该库之前调用这些外部函数,您将得到这样的异常。

可能是正确的方法(直接从新 Android Studio 项目的模板复制):

class MainActivity : Activity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Example of a call to a native method
        sample_text.text = stringFromJNI()
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object
    {

        // Used to load the 'native-lib' library on application startup.
        init
        {
            System.loadLibrary("native-lib")
        }
    }
}

external fun 移动到 companion object {} 中,就像原来的问题一样。