无法加载本机库jni_latinime.so Android

时间:2017-01-20 13:24:24

标签: android android-ndk android-gradle

它出错并无法加载.so文件。我在互联网上搜索并阅读了很多东西,但没有找到答案。我试图使用System.loadLibrary(“jni_latinime”),但它再次失败。请给我任何建议。感谢。

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.android.inputmethod.latin-1/base.apk"],nativeLibraryDirectories=[/data/app/com.android.inputmethod.latin-1/lib/arm, /vendor/lib, /system/lib]]] couldn't find "libjni_latinime.so"
                                                                         at java.lang.Runtime.loadLibrary(Runtime.java:367)
                                                                         at java.lang.System.loadLibrary(System.java:1076)
                                                                         at com.android.inputmethod.latin.utils.JniUtils.<clinit>(JniUtils.java:33)
                                                                         at com.android.inputmethod.latin.utils.JniUtils.loadNativeLibrary(JniUtils.java:46)
                                                                         at com.android.inputmethod.latin.LatinIME.<clinit>(LatinIME.java:599)
                                                                         at java.lang.Class.newInstance(Native Method)
                                                                         at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
                                                                         at android.app.ActivityThread.-wrap4(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

2 个答案:

答案 0 :(得分:0)

1.设置NDK路径

2.在build.gradle中添加

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

3.在SRC direactory

中创建一个Jni文件夹

4.Create- Android.mk - 在jni文件夹中 -

** 5. **添加Android.mk文件****

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := "module name"//added inside system.load(); ** i used addjni**

LOCAL_SRC_FILES := Native.c   //create a native.c class
LOCAL_LDLIBS := -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)

6.在jni文件夹中创建一个文件Application.mk --- ----------------     并写下

  APP_ABI := all

7.在MainActivity中添加

 public class MainActivity extends AppCompatActivity {

 static {
     System.loadLibrary("addjni");
 }
 TextView sum;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     sum=(TextView)findViewById(R.id.sum);
     int as=Native.sumoftwo(210,15);
     Log.e("sum",as+"");
     sum.setText(as+"");

 }

}

8. 在jni文件夹中创建一个新的NativeNDK.c //并添加它      // jave_packageName_Native(Classname)_(methodname)replace(.with - )

 include<string.h>
 include<jni.h>
 jint JNICALL Java_droider_Native_sumoftwo(JNIEnv*  env , jclass obj,jint   a,jint b)   

      {
        jint total=(a+b);
         return total;
       }
  1. 转到工作室终端和内部类型命令 cd app \ src \ main&gt; D:\ android-ndk-r10d \ android-ndk-r10d \ ndk-build APP_PLATFORM = android-8 enter

  2. main / src中的Native.c

    public class Native {        public native static int sumoftwo(int a,int b);         静态的 {                 的System.loadLibrary( “addjni”);         }     }

  3. 11.从libs中复制所有.so并粘贴到jinLibs中,如果存在则创建并粘贴

答案 1 :(得分:0)

转到LatinIME / native / jni并使用ndk-build ..

构建它

(如果它包含错误,只需打开Android.mk并评论此行:

    # LOCAL_CFLAGS += -Werror -Wall -Wextra -Weffc++ -Wformat=2 -Wcast-qual -Wcast-align \
    #     -Wwrite-strings -Wfloat-equal -Wpointer-arith -Winit-self -Wredundant-decls \
    #     -Woverloaded-virtual -Wsign-promo -Wno-system-headers

这将构建jni_latinime.so文件...... 您可以通过提供以下代码将其添加到build.gradle:

sourceSets {
    main {
        jniLibs.srcDirs = ['LOCATION OF .so File']
    }
}