我有一个android应用程序,它可以在大多数设备上很好地工作。应用程序在某些设备上崩溃,原因是
java.lang.UnsatisfiedLinkError: Couldn't load myjpegjni from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.oso.ono.app-2.apk,libraryPath=/data/data/com.oso.ono/lib]: findLibrary returned null我已经为所有架构编译了jni源代码(APP_ABI := all in Application.mk)。
当我尝试用System.loadLibrary("myjpegjni");加载库时,它崩溃了
有什么想法吗?
发布于 2016-05-28 18:19:33
您说您为所有ABI构建,但请仔细检查,在jniLibs文件夹中有所有子文件夹: armeabi,armeabi-v7a,x86,mips...或者至少它有与设备崩溃时相同的ABI的文件夹。
如果它们都存在,则匹配ABI。问题可能来自于安装应用程序时解压lib。
您可以尝试手动解压lib:
public static int loadLibrary(Context context, String library){
try {
System.loadLibrary(library);
return 0;
} catch (UnsatisfiedLinkError ignored) {
ignored.printStackTrace();
}
File workaroundFile;
try {
workaroundFile = getWorkaroundLibFile(context, library);
if (!workaroundFile.exists()) {
unpackLibrary(context, library, workaroundFile);
}
try {
System.load(workaroundFile.getAbsolutePath());
return 1;
} catch (Throwable e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public static File getWorkaroundLibFile(final Context context, final String library) throws PackageManager.NameNotFoundException {
final String libName = System.mapLibraryName(library);
return new File(context.getDir("my_libs", Context.MODE_PRIVATE), libName);
}
@TargetApi(Build.VERSION_CODES.DONUT)
private static void unpackLibrary(final Context context, final String library, final File outputFile) throws IOException {
ZipFile zipFile = null;
try {
final ApplicationInfo appInfo = context.getApplicationInfo();
zipFile = new ZipFile(new File(appInfo.sourceDir), ZipFile.OPEN_READ);
InputStream inputStream = null;
FileOutputStream fileOut = null;
try {
inputStream = getInputStreamFromApk(zipFile, library);
fileOut = new FileOutputStream(outputFile);
copyStream(inputStream, fileOut);
} finally {
closeSilently(inputStream);
closeSilently(fileOut);
}
if (Build.VERSION.SDK_INT >= 9) {
// Change permission to rwxr-xr-x
outputFile.setReadable(true, false);
outputFile.setExecutable(true, false);
outputFile.setWritable(true);
}
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ignored) {}
}
}
private static void copyStream(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[4096];
while (true) {
int read = in.read(buf);
if (read == -1) {
break;
}
out.write(buf, 0, read);
}
}
private static void closeSilently(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ignored) {}
}发布于 2014-04-08 06:31:40
您的本机库不在/data/data/com.oso.ono/lib中,或者不是libmyjpegjni.so。
https://stackoverflow.com/questions/20999975
复制相似问题