Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plugin development: Use flutter package plugin for specific platform only (e.g. Android but not iOS)?

Tags:

flutter

Is there a way to use a package plugin for only 1 (or specific) platform, e.g. "iOS, but not Android or Web"? For example, firebase_messaging supports Android, iOS, macOS and Web, but I only want it to be installed on Android where FCM is required. I will support other platforms with my own code/ other libraries.

I do not want the plugin to be registered on iOS because it may be causing a crash/ interfering with my application because it does Objective-C method swizzling. I guess I don't want it's registerWithRegistrar method called.

Note: this is different to supporting specific platforms. i.e. I still support Android and iOS, I just don't want one plugin to register on iOS. Having thought about this more, I don't just want to prevent plugin registration, but also compilation. This is because the compilation step can affect the build/ other plugins.

like image 752
Ben Butterworth Avatar asked May 07 '26 23:05

Ben Butterworth


1 Answers

Background

GeneratedPluginRegistrant

A class is autogenerated to register all plugins:

Android, GeneratedPluginRegistrant.java

@Keep
public final class GeneratedPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    flutterEngine.getPlugins().add(new io.ably.flutter.plugin.AblyFlutterPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin());
    flutterEngine.getPlugins().add(new com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin());
  }
}

iOS, GeneratedPluginRegistrant.m

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
  [AblyFlutterPlugin registerWithRegistrar:[registry registrarForPlugin:@"AblyFlutterPlugin"]];
  [FLTFirebaseCorePlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseCorePlugin"]];
  [FLTFirebaseMessagingPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseMessagingPlugin"]];
  [FlutterApnsPlugin registerWithRegistrar:[registry registrarForPlugin:@"FlutterApnsPlugin"]];
  [FlutterLocalNotificationsPlugin registerWithRegistrar:[registry registrarForPlugin:@"FlutterLocalNotificationsPlugin"]];
}

@end

Usage

You actually call that method, registerWithRegistry: in your AppDelegate, e.g.

Android

In your Android app's MainActivity:

public class MainActivity extends FlutterActivity {
  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
  }
}

iOS

In your iOS app's application:didFinishLaunchingWithOptions::

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    if (@available(iOS 10.0, *)) {
      [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
    }
    
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

Avoiding registration:

  • Stop using GeneratedPluginRegistrant.
  • Caveat: From now on, you'll need to remember to update the generated code into new class whenever you add or remove package plugins.
  • Caveat: Unfortunately this doesn’t actually avoid the compilation/ build step. For example, the plugin's AndroidManifest.xml will still be merged into your applications Manifest.

Android

Implement PluginRegistrant.java by copying the contents from GeneratedPluginRegistrant.java. Call PluginRegistrant.registerWith(flutterEngine); instead.

iOS

Implement PluginRegistrant.m by copying the contents from GeneratedPluginRegistrant.m. Call [PluginRegistrant registerWithRegistry:self]; instead.

Further questions

Not necessarily hard questions, but I'm leaving this for later. :)

  • How exactly does GeneratedPluginRegistrant know which plugins to register? (read the code generator)
like image 194
Ben Butterworth Avatar answered May 09 '26 15:05

Ben Butterworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!