[Breaking change] project templates and plugin registration

1,229 views
Skip to first unread message

Mikkel Ravn

unread,
May 9, 2017, 10:27:52 AM5/9/17
to Flutter Dev
Plugin registration has been decoupled from direct use of FlutterXxx application entry points, allowing plugins to be consumed also by apps that for whatever reason cannot extend the provided base classes (FlutterActivity on Android, FlutterAppDelegate on iOS).

You may need to change your code, if you have been using a recent project template for writing plugins or apps that consume plugins.

Three roles have been defined: a plugin registry for collecting plugin registrations, a registrar to provide context for a single plugin's setup and registrations, and a registrant representing the plugins being registered. The registry and registrar are interfaces/protocols implemented by FlutterActivity/FlutterAppDelegate and friends, but app authors can provide their own implementations, if needed. A registrant aggregating declared plugins is auto-generated, as before, but is now called GeneratedPluginRegistrant.

What to change

If your app consumes plugins, your MainActivity/AppDelegate should involve a call to GeneratedPluginRegistrant to accept their registrations:

Android:

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

iOS:

@implementation AppDelegate
- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application
     didFinishLaunchingWithOptions:launchOptions];
}

In addition, if you overwrite any of the Activity/UIApplicationDelegate lifecycle methods now implemented by FlutterActivity/FlutterAppDelegate, you should invoke the super method to forward such calls to registered plugins.

If you are writing a plugin, it should involve a static method accepting a registrar:

Android example:

public class MyPlugin implements MethodCallHandler, NewIntentListener {
  public static void registerWith(Registrar registrar) {
    // Set up MyPlugin to receive method calls and Intents
    // using the registrar
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
...}

  @Override
  public bool onNewIntent(Intent intent) {...}
}

iOS example:

@implementation MyPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  // Set up MyPlugin to receive method calls and app lifecycle
  // events using the registrar
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { ... }
- (void)applicationWillEnterForeground:(UIApplication*)application
 { ... }
@end

Rationale for change

Published plugins need a stable API against which to register themselves. App developers need flexibility in how their app is initialized and how plugins take part in that. The registry/registrar roles provides an interface between the two. Later additions to the registrar interface should be expected, allowing plugins to do more. The implementations provided in FlutterActivity/FlutterAppDelegate will follow suit.

Please do not hesitate to contact me, if you need any help adjusting your code to this change.

chri...@lefty.io

unread,
May 10, 2017, 10:06:19 AM5/10/17
to Flutter Dev
Hi Mikkel,

I had this in the activity's onCreate, is it still relevant?

pluginRegistry = new PluginRegistry();
pluginRegistry.registerAll(this);

Mikkel Ravn

unread,
May 10, 2017, 11:59:49 AM5/10/17
to chri...@lefty.io, Flutter Dev
Hi Christian,

Sorry for the plugin reg havoc.

The answer is no. The previously generated io.flutter.plugins.PluginRegistry class is dead, and you should remove the java file. You should replace the code above with a call to GeneratedPluginRegistrant.registerWith(this) after a call to super.onCreate(...).

Similarly on the iOS side.

Let me know if you run into any problems with that.

Mikkel

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mikkel Nygaard Ravn
Software Engineer

Message has been deleted

Chris Bracken

unread,
May 10, 2017, 6:19:31 PM5/10/17
to a...@goposse.com, Flutter Dev
Just ran through this exact issue -- should be:
#include "GeneratedPluginRegistrant.h"

You'll also want to add these files to your Xcode project alongside the AppDelegate sources:
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m




On Wed, 10 May 2017 at 15:04 <a...@goposse.com> wrote:
Hi, what do I need to import into the AppDelegate to call GeneratedPluginRegistrant?

#include "AppDelegate.h"


#include "PluginRegistry.h"


@implementation AppDelegate




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [GeneratedPluginRegistrant registerWithRegistry:self];


    return YES;


}



--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages