From 7a52e3968f0f7a8647e324f82492e6d05b1f3675 Mon Sep 17 00:00:00 2001 From: Stanislav Baranov Date: Fri, 22 Feb 2019 14:18:47 -0800 Subject: [PATCH] Dynamic patching support for native code libraries. (flutter/engine#7927) --- .../android/io/flutter/view/FlutterMain.java | 19 +++++- .../io/flutter/view/ResourceExtractor.java | 63 ++++++++++++++++--- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java index c59e9ab1fb..ae71270857 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java @@ -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(); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/ResourceExtractor.java b/engine/src/flutter/shell/platform/android/io/flutter/view/ResourceExtractor.java index 5bad736061..40fc75982a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/ResourceExtractor.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/ResourceExtractor.java @@ -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; + } + } }