Android studio版本:
flutter版本:2.10.5
Java版本:Java8
一、创建一个Android工程
这里就不说了
2、创建一个flutter module
方法一:和加入普通的module一样,File -> New -> New Module,然后选择flutter module即可。
方法二:我的Android studio中找不到flutter module,所以我只能手动创建,终端切到Android功能根目录下,执行
flutter create -t module --org com.example my_flutter
这里就创建了一个名字为my_flutter的工程了,使用命令flutter run运行一下,它是能跑起来的。
在setting.gradle中,配置module,加入
include ':my_flutter'
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir,
'my_flutter/.android/include_flutter.groovy'
)
)
这里的路径“my_flutter/.android/include_flutter.groovy”是一个相对路径,my_flutter是我们创建的flutter工程,路径不要填错了。
还要继续改一下dependencyResolutionManagement,原来是
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
repositories {
google()
mavenCentral()
}
}
改为
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) // edit this
repositories {
google()
mavenCentral()
String flutterStorageUrl = System.getenv("FLUTTER_STORAGE_BASE_URL") ?: "https://storage.googleapis.com"
maven {
url flutterStorageUrl + "/download.flutter.io"
}
}
}
最后在app/build.gradle中加入flutter module依赖
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation project(path: ':flutter')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
细心的你可能注意到了,我们的flutter module名字不是my_flutter吗,怎么这里是flutter了,其实这个我也不清楚,如果写成my_flutter的话会有找不到的错误,最好的办法是点击 File -> project structure -> Dependencies手动的加进去。