flutter使用firebase_analytics时报错firebase_analytics:uploadArchives解决方法
项目中使用firebase_analytics库时能正常运行,可是打包aar时就报错了
Note: /Users/home/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.3.0/android/src/main/java/io/flutter/plugins/firebase/core/FlutterFirebaseCorePlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/home/.pub-cache/hosted/pub.dartlang.org/firebase_analytics-8.1.2/android/src/main/java/io/flutter/plugins/firebaseanalytics/FirebaseAnalyticsPlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/home/.pub-cache/hosted/pub.dartlang.org/firebase_analytics-8.1.2/android/src/main/java/io/flutter/plugins/firebaseanalytics/FirebaseAnalyticsPlugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':firebase_analytics:uploadArchives'.
> Could not publish configuration 'archives'
> Could not write to file '/Users/home/.pub-cache/hosted/pub.dartlang.org/firebase_analytics-8.1.2/android/build/poms/pom-default.xml'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 5s
Gradle task assembleAarDebug failed with exit code 0.
奇怪的是在同事电脑上是可以正常打包的,可能的原因是jdk版本等配置环境导致的错误,不过我试过多个版本的jdk设置都还是报错。
最后是根据这个回答: https://github.com/firebase/flutterfire/issues/3978 解决的。
修改flutter的 F:\flutter\flutter\packages\flutter_tools\gradle 目录下的 aar_init_script.gradle 文件,在最后面加上下面这个方法
void overrideDefaultPublishConfig(Project project, variant) {
String variantName = variant.name.capitalize()
Task bundle = project.tasks.findByName("bundle${variantName}Aar") // gradle:3.2.0
if (bundle == null) {
bundle = project.tasks.findByName("bundle${variantName}") // gradle:3.1.0
}
if (bundle == null) {
throw new GradleException("Can't generate AAR for variant ${variantName}.");
}
// Clear the current archive artifacts since the artifacts are based on android.defaultPublishConfig.
project.configurations["archives"].artifacts.clear()
// Add the artifact that will be published.
project.artifacts.add("archives", bundle)
def scopeMappings = project.uploadArchives.repositories.mavenDeployer.pom.scopeMappings
// Clear the scope mappings added by AGP since they are based on the current android.defaultPublishConfig.
scopeMappings.mappings.clear()
// Add the new mappings.
int priority = 300
for (Configuration configuration : flattenConfiguration(variant.runtimeConfiguration)) {
// scopeMappings.addMapping(/* priority = */ 300, configuration, "compile")
//firebase_analytics库报错,做这个修改
scopeMappings.addMapping(/* priority = */ priority, configuration, "compile")
priority = priority + 100
}
}
我的 aar_init_script.gradle 文件中其实已经有了这个方法,只是最后面做了修改,如果原来文件中有这个方法,注释后直接复制进去就可以了,主要是做了这个修改
int priority = 300
for (Configuration configuration : flattenConfiguration(variant.runtimeConfiguration)) {
// scopeMappings.addMapping(/* priority = */ 300, configuration, "compile")
//firebase_analytics库报错,做这个修改
scopeMappings.addMapping(/* priority = */ priority, configuration, "compile")
priority = priority + 100
}
保存后重新打包aar就成功了,好像是对映射的优先级进行调整,有兴趣可以看一下原答案