将Flutter Module集成到Android原生中
2022-09-01 本文已影响0人
谢尔顿
国内Flutter的官网集成,官网的描述比较模糊,按照官网集成出现好多问题,下面会具体展开说说。
我用的工具和环境:
- Android Studio: 2021.1.1 Patch 1(俗称小蜜蜂)
- gradle:gradle-7.2-bin.zip
- Flutter:3.3.0
- Dart:2.18.0
集成的步骤:
第一步:
创建一个Android原生项目MyAndroidApp(File->New->New Project...)
第二步:
创建一个Flutter Module,名为my_flutter,(File ->New->New Flutter Project...)
25403057b17d3c8a9dc9340a889146a.png
注意点:Project type要选择Module
第三步:
在原生项目的MyAndroidApp下的settings.gradle下面添加如下代码:
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'my_flutter/.android/include_flutter.groovy'
))
注意点:
- Binding会报错,但是不用管,运行的时候不会报错,如果导入了下面代码
import javax.naming.Binding
Sync Now的时候会报错 Could not find which method <init>() to invoke from this list: - 修改MyAndroidApp下的settings.gradle里的代码
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
为
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
}
}
- 修改MyAndroidApp下的build.gradle代码为
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
}
}
第四步:
在MyAndroidApp->app->build.gradle下面添加如下依赖:
implementation project(':flutter')
最后集成完的项目目录:
239c19dbcdfa3a9839a46b909b4382f.png