1. Android project integration Flutter module project:
1. Use the command to create the Flutter module project lib_flutter (which belongs to the same level directory as the Android project):
flutter create -t module --org com.yyh lib_flutter
2. Update the Android project configuration:
(1) Replace the Android project Support V4/V7 package with the AndroidX package, right-click the project, select Refactor > Migrate to AndroidX... in the pop-up menu, and then click Do Refactor in the box that pops up in the lower left corner, and then wait for the automatic The replacement is done:
(2) Modify compileSdkVersion and targetSdkVersion to 31, in Android project/app/build.gradle:
android {
compileSdkVersion 31 //修改为31,否则报错
buildToolsVersion '30.0.2'
defaultConfig {
minSdkVersion 23
targetSdkVersion 31 //修改为31,否则报错
//...省略其他配置
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
(3) Modify kotlin_version, in Android project/build.gradle:
buildscript {
ext.kotlin_version = '1.6.10'
...
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
(4) Handle Android12 error reporting in AndroidManifest.xml:
<application tools:node="replace"> <!-- 替换三方库中的相关属性,以此处为准 -->
<activity android:exported="true"> <!-- 为含<intent-filter>的节点增加android:exported属性(Android12要求) -->
<intent-filter>...</intent-filter>
</activity>
<receiver android:exported="true">
<intent-filter>...</intent-filter>
</receiver>
<service android:exported="true">
<intent-filter>...</intent-filter>
</service>
</application>
3. Import the Flutter module:
(1) Load lib_flutter module project, add in Android project/settings.gradle:
... #省略其他配置
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, 'lib_flutter/.android/include_flutter.groovy'))
#以下2行Sync后自动产生,无需添加(如果不能生成,则手动添加)
include ':lib_flutter'
project(':lib_flutter').projectDir = new File('../lib_flutter')
(3) Add Flutter dependency, in Android project/app/build.gradle:
dependencies {
//...省略其他配置
implementation project(':flutter') //添加Flutter依赖
}
(4) Click Sync Project With... to synchronize, and you can see the lib_flutter module in Android Studio after completion.
4. Jump to the Flutter page:
(1) Add FlutterActivity to the <application> node in AndroidManifest.xml:
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:theme="@style/FlutterPageTheme"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
/>
(2) Jump to the Flutter entry home page (cold start):
startActivity(FlutterActivity.createDefaultIntent(this));
(3) Jump to the Flutter custom page:
Method 1 (cold start):
startActivity(FlutterActivity.withNewEngine()
.initialRoute("路由id") //要跳转的页面路由id,需要在Flutter中注册此路由id
//.backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent) //设置透明背景
.build(this));
Method 2 (hot start):
//初始化FlutterEngine
public class AFApplication extends Application {
public FlutterEngine mFlutterEngine; //预热方式启动的FlutterEngine
@Override
public void onCreate() {
super.onCreate();
initFlutterEngine();
...
}
void initFlutterEngine(){//初始化FlutterEngine
mFlutterEngine = new FlutterEngine(this);
mFlutterEngine.getNavigationChannel().setInitialRoute("路由id"); //设置初始跳转页路由id,需要在Flutter中注册此路由id
mFlutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault()); //开始预热FlutterEngine
FlutterEngineCache.getInstance().put("engine_id", mFlutterEngine); //缓存engine_id
}
}
//跳转Flutter指定页(热启动,低延迟)
startActivity(FlutterActivity.withCachedEngine("engine_id") //使用已缓存的engine_id启动Flutter自定义页
//.backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent) //设置透明背景
.build(this));
5. See the official documentation for Flutter Fragment and Flutter View:
https://flutter.cn/docs/development/add-to-app/android/add-flutter-fragment
2. iOS project integration Flutter module project:
1. Use the command to create the Flutter module project lib_flutter (which belongs to the same level directory as the iOS project):
flutter create --template module lib_flutter
2. Configure the iOS project and add it to the project/Podfile (execute the command after completion: pod install):
#导入lib_flutter模块工程
flutter_application_path = '../lib_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'iOSP_FlutterM' do
use_frameworks!
install_all_flutter_pods(flutter_application_path) #flutter相关
end
#flutter相关
post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end
3. Jump to the Flutter page:
(1) Mode 1 (cold start):
- (void)startFlutterPageByCold{
//跳转Flutter入口首页
FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
//跳转Flutter指定页,路由id需要在Flutter中注册
//FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithProject:nil initialRoute:@"路由id" nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
}
(2) Mode 2 (hot start):
AppDelegate.h:
@import Flutter;
//...省略其他无关的
@interface AppDelegate : FlutterAppDelegate
@property (nonatomic,strong) FlutterEngine *flutterEngine; //定义FlutterEngine
//...省略其他无关的
@end
AppDelegate.m:
#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
//...省略其他无关的
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"engine_id"];
[self.flutterEngine run]; //跳转Flutter入口首页
//[self.flutterEngine runWithEntrypoint:FlutterDefaultDartEntrypoint initialRoute:@"路由id"]; //跳转Flutter指定页面,路由id需要在Flutter中注册
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine]; //连接插件(仅带有iOS平台的插件时需要)
return YES;
}
//...省略其他无关的
Jump code:
- (void)startFlutterPageByHot{
FlutterEngine *flutterEngine = ((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;//获取AppDelegate中的FlutterEngine
FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
}