Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix android dynamic libraray not found on some devices #91

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 72 additions & 59 deletions webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}