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
Deduplicate plugin registration logic and make error logs visible - take 2 #25395
Changes from all commits
4859b9f
a52d4d3
2e6d28b
a709c21
e4490ca
30f8332
1db4752
2b4227d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -580,12 +580,21 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) { | |
* <p>This method is called after {@link #provideFlutterEngine(Context)}. | ||
* | ||
* <p>All plugins listed in the app's pubspec are registered in the base implementation of this | ||
* method. To avoid automatic plugin registration, override this method without invoking super(). | ||
* To keep automatic plugin registration and further configure the flutterEngine, override this | ||
* method, invoke super(), and then configure the flutterEngine as desired. | ||
* method unless the FlutterEngine for this activity was externally created. To avoid the | ||
* automatic plugin registration for implicitly created FlutterEngines, override this method | ||
* without invoking super(). To keep automatic plugin registration and further configure the | ||
* FlutterEngine, override this method, invoke super(), and then configure the FlutterEngine as | ||
* desired. | ||
*/ | ||
@Override | ||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | ||
if (flutterFragment.isFlutterEngineInjected()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key bit that is different from the previous PR. It seems like there was a long-standing bug where even if the engine says don't auto-register, the activity registers it anyway, which makes it hard to test in while avoiding annoying registration errors. Comically, this change makes everything harder to test since only a non-injected engine has the auto-registering behavior but a non-injected engine is harder to prevent real JNI calls on. Add more plumbing to make a default constructed engine testable. |
||
// If the FlutterEngine was explicitly built and injected into this FlutterActivity, the | ||
// builder should explicitly decide whether to automatically register plugins via the | ||
// FlutterEngine's construction parameter or via the AndroidManifest metadata. | ||
return; | ||
} | ||
|
||
GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,6 +282,7 @@ public static native void nativeOnVsync( | |
|
||
@NonNull private final Looper mainLooper; // cached to avoid synchronization on repeat access. | ||
|
||
// Prefer using the FlutterJNI.Factory so it's easier to test. | ||
public FlutterJNI() { | ||
// We cache the main looper so that we can ensure calls are made on the main thread | ||
// without consistently paying the synchronization cost of getMainLooper(). | ||
|
@@ -1258,4 +1259,15 @@ public interface AccessibilityDelegate { | |
public interface AsyncWaitForVsyncDelegate { | ||
void asyncWaitForVsync(final long cookie); | ||
} | ||
|
||
/** | ||
* A factory for creating {@code FlutterJNI} instances. Useful for FlutterJNI injections during | ||
* tests. | ||
*/ | ||
public static class Factory { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The pattern should have the Factory being an interface. It's not a huge deal here, but helps if anyone tries to add methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it creates a bit more indirection to be able to instantiate one if it was an interface though |
||
/** @return a {@link FlutterJNI} instance. */ | ||
public FlutterJNI provideFlutterJNI() { | ||
return new FlutterJNI(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, should we report this? Potentially with an exception or a return value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both the engine registration and the activity registration paths are called 99% of the time. We should just ignore the second call.