System.loadLibrary崩溃了

时间:2014-01-08 15:36:32

标签: android android-ndk java-native-interface

我有一个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:=所有在Application.mk中)。

当我尝试使用System.loadLibrary(“myjpegjni”)加载库时崩溃;

任何想法?

2 个答案:

答案 0 :(得分:1)

你说你是为所有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) {}
}

答案 1 :(得分:0)

您的本机库不在/data/data/com.oso.ono/lib中,或者它不是名为libmyjpegjni.so。