java.lang.UnsatisfiedLinkError:找不到实现(NDK,Kotlin)-已修复

时间:2018-12-26 15:37:36

标签: android string kotlin android-ndk unsatisfiedlinkerror

我使用NDK在C中保存的文件如下,我已经查看了Kotlin中的程序包名称和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] = '\0';
        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] = '\0';
        result = (*env)->NewStringUTF(env,subbuff);
    } else {
        result = (*env)->NewStringUTF(env,deviceIdEncode);
    }
    return result;
}

我的Kotlin类软件包是软件包 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(SplashActivity扩展了BaseActivity)上正在调用System.loadLibrary(“ nameofyourCFile”),但是其余活动不会加载C文件,因为当我准备回到后台并返回时,SplashActivity并没有再次启动,因此我的库没有从任何地方加载。我将System.loadLibrary()放入BaseActivity内,并且可以正常工作!

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

1 个答案:

答案 0 :(得分:1)

Java运行时必须使用System.loadLibrary()(可能在init {}块中的companion object {}块中)才能找到本机库(包含函数的“核心”)的位置。仅仅一个@JvmStatic external fun(仅仅是一个“表面”或“界面”)是不够的。

如果您在加载该库之前 调用了这些外部函数,则会收到此类异常。

也许是正确的方法(直接从新的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 {}内。