1、新建flutter-mofule项目,注意须与Android项目在同一级目录
flutter create --template module flutter_module
2、在flutter项目根目录执行如下命令
flutter pub get
或者在pubspec.yaml中点击pub get
3、在原生Android项目中引入flutter
include ':flutter_module'
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'flutter_module/.android/include_flutter.groovy'
))
//可选,主要作用是可以在当前AS的Project下显示flutter_module以方便查看和编写Dart代码
project(':flutter_module').projectDir = new File('../flutter_module')
4、报错修改
Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by plugin class 'FlutterPlugin'
解决办法
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
改为
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
5、build信息重复冲突
A build operation failed.
Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
this and base files have different roots: D:\Code\Gamification\questpx_flutter_module\.android\plugins_build_output\path_provider_android and C:\Users\dell\AppData\Local\Pub\Cache\hosted\pub.flutter-io.cn\path_provider_android-2.1.0\android.
原因:flutter相关缓存保存在C:\Users\dell\AppData\Local\Pub\Cache,需要项目的缓存目录和系统缓存保持在同一盘符中
设置方法:
1.环境变量添加PUB_CACHE,指定缓存存放位置
https://dart.cn/tools/pub/environment-variables
2.将项目建在该目录下
6、如果出现namespace问题, 可以去对应的module中修改build.gradle文件,这个问题是因为gradle升级后,去除了AndroidManifest.xml中的package,需要在build.gradle中定义命名空间
android {
if (project.android.hasProperty("namespace")) {
namespace 'com.idlefish.flutterboost'
}
}