1.创建原生项目
会出现如下问题:
A problem occurred evaluating project ':flutter'.
> Failed to apply plugin 'com.android.internal.library'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
解决办法:
修改对应jdk版本为11即可。
2.创建flutter module
// com.example 是包名 my_flutter是module名
flutter create -t module --org com.example my_flutter
3.配置原生项目
settings.gradle文件
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile, // 项目内的目录的File路径
'my_flutter/.android/include_flutter.groovy'
))
include ':my_flutter'
(1)编译会报如下错误:
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin class 'FlutterPlugin'.
解决办法:
settings.gradle文件中,FAIL_ON_PROJECT_REPOS 改为 PREFER_PROJECT
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
(2)编译会报如下错误:
Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
> Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21.
Searched in the following locations:
- https://storage.googleapis.com/download.flutter.io/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.6.21/kotlin-stdlib-jdk8-1.6.21.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
解决办法:
原生项目build.gradle文件中新增如下
allprojects {
repositories {
google()
mavenCentral()
}
}
4.原生跳转flutter module
(1)AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@style/Theme.AppCompat.DayNight"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
/>
(2)app 目录下 build.gradle
implementation project(':flutter')
(3)MainActivity.class
findViewById<TextView>(R.id.tv_jump).setOnClickListener {
startActivity(FlutterActivity.createDefaultIntent(this))
}
5.其他问题
(1)如果更改了flutter sdk 目录(项目环境改变,如换电脑、git clone 等等),会出现如下问题:
...\module_plugin_loader.gradle' as it does not exist.
解决办法:
在flutter module 中执行如下命令
flutter packages upgrade
flutter pub get
(2)这样跳转会很慢,大概3秒的延迟,所以先创建FlutterEngine缓存起来:
public class App extends Application {
private FlutterEngine flutterEngine;
@Override
public void onCreate() {
super.onCreate();
flutterEngine = new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine);
}
@Override
public void onTerminate() {
flutterEngine.destroy();
super.onTerminate();
}
}
跳转方式改为(直接调用缓存FlutterEngine):
startActivity(
FlutterActivity
.withCachedEngine("my_engine_id")
.build(this)
)