From 7d3211695500fc1bdd23cfd7d2277a16d62e4bb2 Mon Sep 17 00:00:00 2001 From: andycall Date: Thu, 27 Oct 2022 21:53:25 +0800 Subject: [PATCH] fix: fix android dynamic libraray not found on some devices --- .../java/com/openwebf/webf/WebFPlugin.java | 131 ++++++++++-------- 1 file changed, 72 insertions(+), 59 deletions(-) diff --git a/webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java b/webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java index 3395693682..9aa765e085 100644 --- a/webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java +++ b/webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java @@ -14,72 +14,85 @@ * WebFPlugin */ public class WebFPlugin implements FlutterPlugin, MethodCallHandler { - /// The MethodChannel that will the communication between Flutter and native Android - /// - /// This local reference serves to register the plugin with the Flutter Engine and unregister it - /// when the Flutter Engine is detached from the Activity - public MethodChannel channel; - private FlutterEngine flutterEngine; - private Context mContext; - private WebF mWebF; + /// The MethodChannel that will the communication between Flutter and native Android + /// + /// This local reference serves to register the plugin with the Flutter Engine and unregister it + /// when the Flutter Engine is detached from the Activity + public MethodChannel channel; + private FlutterEngine flutterEngine; + private Context mContext; + private WebF mWebF; - // This static function is optional and equivalent to onAttachedToEngine. It supports the old - // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting - // plugin registration via this function while apps migrate to use the new Android APIs - // post-flutter-1.12 via https://flutter.dev/go/android-project-migration. - // - // It is encouraged to share logic between onAttachedToEngine and registerWith to keep - // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called - // depending on the user's project. onAttachedToEngine or registerWith must both be defined - // in the same class. - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "webf"); - WebFPlugin plugin = new WebFPlugin(); - plugin.mContext = registrar.context(); - channel.setMethodCallHandler(plugin); - } + // This static function is optional and equivalent to onAttachedToEngine. It supports the old + // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting + // plugin registration via this function while apps migrate to use the new Android APIs + // post-flutter-1.12 via https://flutter.dev/go/android-project-migration. + // + // It is encouraged to share logic between onAttachedToEngine and registerWith to keep + // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called + // depending on the user's project. onAttachedToEngine or registerWith must both be defined + // in the same class. + public static void registerWith(Registrar registrar) { + final MethodChannel channel = new MethodChannel(registrar.messenger(), "webf"); + WebFPlugin plugin = new WebFPlugin(); + plugin.mContext = registrar.context(); + channel.setMethodCallHandler(plugin); + } - @Override - public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) { - mContext = flutterPluginBinding.getApplicationContext(); - channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "webf"); - flutterEngine = flutterPluginBinding.getFlutterEngine(); - channel.setMethodCallHandler(this); - } + @Override + public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) { + loadLibrary(); + mContext = flutterPluginBinding.getApplicationContext(); + channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "webf"); + flutterEngine = flutterPluginBinding.getFlutterEngine(); + channel.setMethodCallHandler(this); + } - WebF getWebF() { - if (mWebF == null) { - mWebF = WebF.get(flutterEngine); - } - return mWebF; + WebF getWebF() { + if (mWebF == null) { + mWebF = WebF.get(flutterEngine); } + return mWebF; + } - @Override - public void onMethodCall(MethodCall call, Result result) { - switch (call.method) { - case "getDynamicLibraryPath": { - WebF webf = getWebF(); - result.success(webf == null ? "" : webf.getDynamicLibraryPath()); - break; - } - case "getTemporaryDirectory": - result.success(getTemporaryDirectory()); - break; - default: - result.notImplemented(); - } + private static boolean isLibraryLoaded = false; + private static void loadLibrary() { + if (isLibraryLoaded) { + return; } + // Loads `libwebf.so`. + System.loadLibrary("webf"); + // Loads `libquickjs.so`. + System.loadLibrary("quickjs"); + isLibraryLoaded = true; + } - @Override - public void onDetachedFromEngine(FlutterPluginBinding binding) { - channel.setMethodCallHandler(null); - WebF webf = WebF.get(flutterEngine); - if (webf == null) return; - webf.destroy(); - flutterEngine = null; + @Override + public void onMethodCall(MethodCall call, Result result) { + switch (call.method) { + case "getDynamicLibraryPath": { + WebF webf = getWebF(); + result.success(webf == null ? "" : webf.getDynamicLibraryPath()); + break; + } + case "getTemporaryDirectory": + result.success(getTemporaryDirectory()); + break; + default: + result.notImplemented(); } + } - private String getTemporaryDirectory() { - return mContext.getCacheDir().getPath() + "/WebF"; - } + @Override + public void onDetachedFromEngine(FlutterPluginBinding binding) { + channel.setMethodCallHandler(null); + WebF webf = WebF.get(flutterEngine); + if (webf == null) return; + webf.destroy(); + flutterEngine = null; + } + + private String getTemporaryDirectory() { + return mContext.getCacheDir().getPath() + "/WebF"; + } }