出现的错误:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.app_myapp-1/base.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.app_myapp-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.app_myapp-1/lib/arm, /vendor/lib, /system/lib]]] couldn't find "libHardControl.so"
原因:
我将android studio 3.4.0中的HardControl.java的如下代码中的“hardcontrol”写成了 “HardControl”,从而导至了如上的错误。
package com.example.hardlibrary;
public class HardControl{
public static native int ledCtrl(int which,int status);
public static native int ledOpen();
public static native void ledClose();
static{
try {
System.loadLibrary("hardcontrol");
} catch (Exception e) {
e.printStackTrace();
}
}
}
E:\and_learning\Myled\app\libs\armeabi\ 目录下的 libhardcontrol.so,这里的hardcontrol要与上文中的System.loadLibrary("hardcontrol");大小写要一至,
E:\and_learning\Myled\app\src\main\java\com\example\hardlibrary目录下的HardControl.java的名字大小要与下文中的HardControl名称的大小写一致。
/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
jclass cls;
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
return JNI_ERR; /* JNI version not supported */
}
cls = (*env)->FindClass(env, "com/example/hardlibrary/HardControl");
if (cls == NULL) {
return JNI_ERR;
}
/* 2. map java hello <-->c c_hello */
if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)
return JNI_ERR;
return JNI_VERSION_1_4;
}
因为上面的错误,拆磨我好久,现在记下来,希望你们少走弯路。
=============================如下是gradle APP完整的正确代码==============================
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.app_myapp"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets{
main{
jniLibs.srcDirs=['libs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}