Dynamic patching support for native code libraries. (flutter/engine#7927)

This commit is contained in:
Stanislav Baranov
2019-02-22 14:18:47 -08:00
committed by GitHub
parent 8485c02dc1
commit 7a52e3968f
2 changed files with 73 additions and 9 deletions

View File

@@ -59,6 +59,7 @@ public class FlutterMain {
private static final String DEFAULT_AOT_ISOLATE_SNAPSHOT_DATA = "isolate_snapshot_data";
private static final String DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR = "isolate_snapshot_instr";
private static final String DEFAULT_FLX = "app.flx";
private static final String DEFAULT_LIBRARY = "libflutter.so";
private static final String DEFAULT_KERNEL_BLOB = "kernel_blob.bin";
private static final String DEFAULT_FLUTTER_ASSETS_DIR = "flutter_assets";
@@ -153,7 +154,18 @@ public class FlutterMain {
initConfig(applicationContext);
initAot(applicationContext);
initResources(applicationContext);
System.loadLibrary("flutter");
if (sResourceUpdater == null) {
System.loadLibrary("flutter");
} else {
sResourceExtractor.waitForCompletion();
File lib = new File(PathUtils.getDataDirectory(applicationContext), DEFAULT_LIBRARY);
if (lib.exists()) {
System.load(lib.getAbsolutePath());
} else {
System.loadLibrary("flutter");
}
}
// We record the initialization time using SystemClock because at the start of the
// initialization we have not yet loaded the native library to call into dart_tools_api.h.
@@ -299,6 +311,11 @@ public class FlutterMain {
.addResource(sAotIsolateSnapshotInstr);
}
if (sResourceUpdater != null) {
sResourceExtractor
.addResource(DEFAULT_LIBRARY);
}
sResourceExtractor.start();
}

View File

@@ -240,12 +240,33 @@ class ResourceExtractor {
}
for (String asset : mResources) {
final String resource = "assets/" + asset;
ZipEntry entry = zipFile.getEntry(resource);
String resource = null;
ZipEntry entry = null;
if (asset.endsWith(".so")) {
// Replicate library lookup logic.
for (String abi : Build.SUPPORTED_ABIS) {
resource = "lib/" + abi + "/" + asset;
entry = zipFile.getEntry(resource);
if (entry == null) {
entry = zipFile.getEntry(resource + ".bzdiff40");
if (entry == null) {
continue;
}
}
// Stop after the first match.
break;
}
}
if (entry == null) {
entry = zipFile.getEntry(resource + ".bzdiff40");
resource = "assets/" + asset;
entry = zipFile.getEntry(resource);
if (entry == null) {
continue;
entry = zipFile.getEntry(resource + ".bzdiff40");
if (entry == null) {
continue;
}
}
}
@@ -265,10 +286,27 @@ class ResourceExtractor {
}
ByteArrayOutputStream orig = new ByteArrayOutputStream();
try (InputStream is = manager.open(asset)) {
copy(is, orig);
} catch (FileNotFoundException e) {
throw new IOException("Could not find APK resource " + resource);
if (asset.endsWith(".so")) {
ZipFile apkFile = new ZipFile(getAPKPath());
if (apkFile == null) {
throw new IOException("Could not find APK");
}
ZipEntry origEntry = apkFile.getEntry(resource);
if (origEntry == null) {
throw new IOException("Could not find APK resource " + resource);
}
try (InputStream is = apkFile.getInputStream(origEntry)) {
copy(is, orig);
}
} else {
try (InputStream is = manager.open(asset)) {
copy(is, orig);
} catch (FileNotFoundException e) {
throw new IOException("Could not find APK resource " + resource);
}
}
try (OutputStream os = new FileOutputStream(output)) {
@@ -356,4 +394,13 @@ class ResourceExtractor {
out.write(buf, 0, i);
}
}
private String getAPKPath() {
try {
return mContext.getPackageManager().getApplicationInfo(
mContext.getPackageName(), 0).publicSourceDir;
} catch (Exception e) {
return null;
}
}
}