SharedLibraryLoader: iterate through all apks for bundles

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-10-13 10:21:24 +02:00
parent 3af2420da9
commit 749efcde21

View File

@ -13,6 +13,9 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -32,40 +35,48 @@ public final class SharedLibraryLoader {
noAbiException = e; noAbiException = e;
} }
final ZipFile zipFile; final Collection<String> apks = new HashSet<>();
try { if (context.getApplicationInfo().sourceDir != null)
zipFile = new ZipFile(new File(context.getApplicationInfo().sourceDir), ZipFile.OPEN_READ); apks.add(context.getApplicationInfo().sourceDir);
} catch (final IOException e) { if (context.getApplicationInfo().splitSourceDirs != null)
throw new RuntimeException(e); apks.addAll(Arrays.asList(context.getApplicationInfo().splitSourceDirs));
}
final String mappedLibName = System.mapLibraryName(libName); for (final String apk : apks) {
final byte[] buffer = new byte[1024 * 32]; final ZipFile zipFile;
for (final String abi : Build.SUPPORTED_ABIS) {
final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName;
final ZipEntry zipEntry = zipFile.getEntry(libZipPath);
if (zipEntry == null)
continue;
File f = null;
try { try {
f = File.createTempFile("lib", ".so", context.getCacheDir()); zipFile = new ZipFile(new File(apk), ZipFile.OPEN_READ);
Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + f.getAbsolutePath() + " and loading"); } catch (final IOException e) {
try (final FileOutputStream out = new FileOutputStream(f); throw new RuntimeException(e);
final InputStream in = zipFile.getInputStream(zipEntry)) { }
int len;
while ((len = in.read(buffer)) != -1) { final String mappedLibName = System.mapLibraryName(libName);
out.write(buffer, 0, len); final byte[] buffer = new byte[1024 * 32];
for (final String abi : Build.SUPPORTED_ABIS) {
final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName;
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);
}
} }
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();
} }
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();
} }
} }
if (noAbiException instanceof RuntimeException) if (noAbiException instanceof RuntimeException)