1.直接在build.gradle文件内追加:
// 读取 local.properties 文件
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localProperties.load(new FileInputStream(localPropertiesFile))
}
// 获取 Flutter SDK 路径
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.")
}
// 定义一个变量来存储 Flutter 引擎库路径
def flutterEnginePath = "$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar"
// 确保 Flutter 引擎库存在
if (!file(flutterEnginePath).exists()) {
throw new GradleException("Flutter engine library not found at: $flutterEnginePath")
}
dependencies {
compileOnly files(flutterEnginePath)
compileOnly 'androidx.annotation:annotation:1.1.0'
}
2.目前我是用的flutter sdk版本为3.24.4,发现 "$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar"是不存在的,于是我在".gradle\caches\modules-2\files-2.1\io.flutter\flutter_embedding_release"路径下找到了jar包,手动创建了flutterEnginePath
重新构建项目,大功告成!
不知道有没有更好的引用依赖方式,感觉这样有点傻!