目录
只想看方法,直接看成功步骤!!!!
新建Flutter插件项目,打开Android工程报红,无法应用io.flutter.xxx
这是因为android项目中没有依赖Flutter库,经过对比Flutter工程Android下的gardle配置,发现需要应用Flutter SDK下的flutter.gradle,
仿造配置如下
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
结果不行,提示没有:app模块,应为我新建的Flutter插件项目,不是FlutterAPP项目
成功步骤
打开"$flutterRoot/packages/flutter_tools/gradle/“目录发现有个resolve_dependencies.gradle文件,看名子应该是应用依赖,直接将flutter.gradle换成resolve_dependencies.gradle还是失败,最终我将resolve_dependencies.gradle复制到项目中,经过修改再应用到项目中成功运行
resolve_dependencies.gradle内容
import java.nio.file.Paths
//获取local.properties配置文件
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') {
reader -> localProperties.load(reader)
}
}
//获取flutter的sdk路径
def flutterRootPath = localProperties.getProperty('flutter.sdk')
if (flutterRootPath == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
String storageUrl = System.getenv('FLUTTER_STORAGE_BASE_URL') ?: "https://storage.googleapis.com"
repositories {
google()
mavenCentral()
maven {
url "$storageUrl/download.flutter.io"
}
}
File flutterRoot = projectDir.parentFile.parentFile.parentFile
assert flutterRoot.isDirectory()
String engineVersion = Paths.get(flutterRootPath, "bin", "internal", "engine.version")
.toFile().text.trim()
configurations {
flutterRelease.extendsFrom releaseImplementation
flutterDebug.extendsFrom debugImplementation
flutterProfile.extendsFrom debugImplementation
}
dependencies {
flutterRelease "io.flutter:flutter_embedding_release:1.0.0-$engineVersion"
flutterRelease "io.flutter:armeabi_v7a_release:1.0.0-$engineVersion"
flutterRelease "io.flutter:arm64_v8a_release:1.0.0-$engineVersion"
flutterProfile "io.flutter:flutter_embedding_profile:1.0.0-$engineVersion"
flutterProfile "io.flutter:armeabi_v7a_profile:1.0.0-$engineVersion"
flutterProfile "io.flutter:arm64_v8a_profile:1.0.0-$engineVersion"
compileOnly "io.flutter:flutter_embedding_debug:1.0.0-$engineVersion"
compileOnly "io.flutter:armeabi_v7a_debug:1.0.0-$engineVersion"
compileOnly "io.flutter:arm64_v8a_debug:1.0.0-$engineVersion"
compileOnly "io.flutter:x86_debug:1.0.0-$engineVersion"
compileOnly "io.flutter:x86_64_debug:1.0.0-$engineVersion"
}
切记不要保留task resolveDependencies...,否则会应用失败