Flutter中如何在iOS端排除仅支持Android的sms_maintained包?
Got it, let's tackle this problem step by step. The core issue here is keeping sms_maintained in your pubspec but preventing iOS from trying to use it (since it's Android-only) while avoiding conflicts with the auto-generated GeneratedPluginRegistrant. Here are a few reliable approaches:
1. Platform-Conditional Imports + Manual Plugin Registration
This is the cleanest, Dart-native way to handle platform-specific dependencies without messing too much with native build files.
Step 1: Keep the dependency in pubspec.yaml
Leave sms_maintained in your dependencies list—no need to comment it out:
dependencies: flutter: sdk: flutter sms_maintained: ^0.2.0 # Keep this line as-is
Step 2: Create a placeholder file for iOS
Make a new file (e.g., lib/sms_placeholder.dart) with empty implementations of the classes/methods you use from sms_maintained. For example:
// Empty placeholder to avoid iOS import errors class SmsSender { Future<void> sendSms({required String address, required String message}) async {} } // Add any other classes/methods you use from the package here
Step 3: Use conditional imports in your Dart code
Instead of directly importing sms_maintained, use a platform-specific import to switch between the real package and your placeholder:
// Import the real package on Android, placeholder on iOS import 'package:sms_maintained/sms_maintained.dart' if (dart.library.io) 'sms_placeholder.dart';
This way, iOS will use the empty placeholder (no native dependencies), while Android uses the full functionality of the package.
Step 4: Override auto-generated plugin registration
The auto-generated GeneratedPluginRegistrant.m will still try to register sms_maintained for iOS. Fix this by taking manual control:
- In your iOS
Runnerproject, copyGeneratedPluginRegistrant.mand rename it toManualPluginRegistrant.m. - Open the new file and delete any lines related to
sms_maintained(look forregisterWithRegistry:calls for that plugin). - In your
AppDelegate.swiftorAppDelegate.m, replace the line:
with:[GeneratedPluginRegistrant registerWithRegistry:self];[ManualPluginRegistrant registerWithRegistry:self]; - Optional: Add
GeneratedPluginRegistrant.mto your.gitignoreto prevent Flutter from overwriting your manual registrant.
2. Podfile Tweaks to Exclude the Package
If you prefer handling it at the native iOS build level, force the Podfile to skip sms_maintained:
Step 1: Update your Podfile
Add this pre-install hook inside the target 'Runner' do block:
pre_install do |installer| # Skip sms_maintained since it's Android-only installer.pod_targets.each do |pod| if pod.name == 'sms_maintained' puts "Skipping sms_maintained for iOS build" pod.remove_from_project end end end
Step 2: Fix the GeneratedPluginRegistrant error
Even with the Podfile fix, Flutter will still generate registration code for sms_maintained. Follow the same manual registration steps from Solution 1, Step 4 to create a custom registrant without the sms_maintained entry.
3. Pubspec Platform Exclusion (Experimental)
Newer Flutter versions support platform exclusions in your pubspec. This tells Flutter to ignore the dependency for iOS builds:
dependencies: sms_maintained: ^0.2.0 platforms: android: ios: exclude: true
Test this first, as some older packages might not respect this flag fully. If it works, it will prevent Flutter from generating registration code and adding the package to the Podfile automatically.
Any of these methods should let you keep sms_maintained in your pubspec while building iOS without issues. I'd recommend starting with Solution 1—it's the most maintainable long-term since it keeps platform logic in Dart.
内容的提问来源于stack exchange,提问作者Benedictgeek

