flutter使用firebase_analytics时报错firebase_analytics:uploadArchives解决方法

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就成功了,好像是对映射的优先级进行调整,有兴趣可以看一下原答案

好的,下面是使用Firebase_Messaging接收推送通知的完整Flutter iOS代码: ```dart import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance; @override void initState() { super.initState(); _firebaseMessaging.requestPermission(); _firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) async { print("onMessage: $message"); showDialog( context: context, builder: (context) { return AlertDialog( content: ListTile( title: Text(message['notification']['title']), subtitle: Text(message['notification']['body']), ), actions: <Widget>[ FlatButton( child: Text('Ok'), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); }, onResume: (Map<String, dynamic> message) async { print("onResume: $message"); }, onLaunch: (Map<String, dynamic> message) async { print("onLaunch: $message"); }, ); } @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Firebase Messaging', home: Scaffold( appBar: AppBar( title: Text('Flutter Firebase Messaging'), ), body: Center( child: Text('Welcome to Flutter Firebase Messaging'), ), ), ); } } ``` 在这个例子中,我们首先使用Firebase.initializeApp()初始化Firebase应用程序,然后创建FirebaseMessaging实例并调用requestPermission()请求权限。接下来,我们使用configure()方法配置FirebaseMessaging以处理不同的消息事件。当应用程序处于前台,onMessage()方法被调用,我们在这里创建一个AlertDialog来显示推送通知的标题和正文。当应用程序处于后台或未运行,onResume()和onLaunch()方法被调用,我们可以在这里处理推送通知。 希望这个例子能够帮助你使用Firebase_Messaging接收推送通知。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值