无法找到" libmynative.so"即使它复制到libs文件夹(Android NDK)

时间:2016-02-19 19:52:50

标签: android android-ndk

我成功复制了" libmynative.so"文件夹到armeabi文件夹

[armeabi] Compile++ thumb: mynative <= helloworld.cpp
[armeabi] Compile++ thumb: mynative <= jni_entry.cpp
[armeabi] SharedLibrary  : libmynative.so
[armeabi] Install        : libmynative.so =>libs/armeabi/libmynative.so

我仍然收到以下错误

02-20 01:01:22.949 10851-10851/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.lol.jnihelloworld, PID: 10851
                                               java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.lol.jnihelloworld-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libmynative.so"
                                                   at java.lang.Runtime.loadLibrary(Runtime.java:366)
                                                   at java.lang.System.loadLibrary(System.java:988)
                                                   at com.lol.jnihelloworld.MainActivity.<clinit>(MainActivity.java:19)
                                                   at java.lang.reflect.Constructor.newInstance(Native Method)
                                                   at java.lang.Class.newInstance(Class.java:1606)
                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

这是我的MainActivity

package com.lol.jnihelloworld;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;


public class MainActivity extends Activity {


public native String getNativeMessage();


static {
    System.loadLibrary("mynative");
}


Button btnInvokeJNI;
TextView tvNativeMsg;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    btnInvokeJNI = (Button) findViewById(R.id.btnInvokeJni);
    tvNativeMsg = (TextView) findViewById(R.id.txtJniText);


    btnInvokeJNI.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tvNativeMsg.setText(getNativeMessage());
        }
    });
}

}

错误指示System.loadLibrary(&#34; mynative&#34;); 我无法解决它。

Snapshot

2 个答案:

答案 0 :(得分:1)

如果您使用的是Android Studio,请确保您的.so位于项目中app / src / main /的“jniLibs”文件夹中。

所以在你的项目中它会是这样的。

└── ProjectRoot
    └── app
        └── src
            └── main
                └── jniLibs
                    └── armebi
                          └── lib.so

答案 1 :(得分:1)

如果您使用的是Android工作室,则默认的jni libraries目录不是libs,您必须明确地在gradle构建脚本下指定它。

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = []
}

此外,在构建完成后,您可以检查本地库是否内置在apk中。

$ unzip -l app-debug.apk |grep mynative