diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-10-13 11:05:03 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-10-13 11:46:41 +0200 |
commit | 3c8fef2655e6a1375625e625339e29f8f7da967a (patch) | |
tree | f2a6b438735b93b0a50473500969a3ed8b8abb9d /app/src | |
parent | 21af2f2f6246565563b4c84059e0d1940b0b662d (diff) |
SharedLibraryLoader: separate out extraction
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java b/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java index 50026614..e3923d19 100644 --- a/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java +++ b/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java @@ -25,16 +25,7 @@ public final class SharedLibraryLoader { private SharedLibraryLoader() { } - public static void loadSharedLibrary(final Context context, final String libName) { - Throwable noAbiException; - try { - System.loadLibrary(libName); - return; - } catch (final UnsatisfiedLinkError e) { - Log.d(TAG, "Failed to load library normally, so attempting to extract from apk", e); - noAbiException = e; - } - + public static boolean extractLibrary(final Context context, final String libName, final File destination) throws IOException { final Collection<String> apks = new HashSet<>(); if (context.getApplicationInfo().sourceDir != null) apks.add(context.getApplicationInfo().sourceDir); @@ -56,28 +47,43 @@ public final class SharedLibraryLoader { final ZipEntry zipEntry = zipFile.getEntry(libZipPath); if (zipEntry == null) continue; - File f = null; - try { - f = File.createTempFile("lib", ".so", context.getCacheDir()); - Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + f.getAbsolutePath() + " and loading"); - try (final FileOutputStream out = new FileOutputStream(f); - final InputStream in = zipFile.getInputStream(zipEntry)) { - int len; - while ((len = in.read(buffer)) != -1) { - out.write(buffer, 0, len); - } + Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + destination.getAbsolutePath()); + try (final FileOutputStream out = new FileOutputStream(destination); + final InputStream in = zipFile.getInputStream(zipEntry)) { + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); } - System.load(f.getAbsolutePath()); - return; - } catch (final Exception e) { - Log.d(TAG, "Failed to load library apk:/" + libZipPath, e); - noAbiException = e; - } finally { - if (f != null) - // noinspection ResultOfMethodCallIgnored - f.delete(); } + return true; + } + } + return false; + } + + public static void loadSharedLibrary(final Context context, final String libName) { + Throwable noAbiException; + try { + System.loadLibrary(libName); + return; + } catch (final UnsatisfiedLinkError e) { + Log.d(TAG, "Failed to load library normally, so attempting to extract from apk", e); + noAbiException = e; + } + File f = null; + try { + f = File.createTempFile("lib", ".so", context.getCodeCacheDir()); + if (extractLibrary(context, libName, f)) { + System.load(f.getAbsolutePath()); + return; } + } catch (final Exception e) { + Log.d(TAG, "Failed to load library apk:/" + libName, e); + noAbiException = e; + } finally { + if (f != null) + // noinspection ResultOfMethodCallIgnored + f.delete(); } if (noAbiException instanceof RuntimeException) throw (RuntimeException) noAbiException; |