• Android13集成paho.mqtt.android启动异常


    项目中原依赖是:

    implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.1.1') {
        exclude module: 'support-v4'
        transitive = true
    }
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
    

    在Android10系统运行正常,能够连接服务端,在Android13中出错,log如下:

    E/AndroidRuntime: FATAL EXCEPTION: MQTT Rec: paho4782357715513
        Process: com.xxx.xxxx, PID: 24859
        java.lang.IllegalArgumentException: com.xxx.xxxx: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
        Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
            at android.app.PendingIntent.checkFlags(PendingIntent.java:401)
            at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)
            at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)
            at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
            at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214)
            at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050)
            at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)
            at java.lang.Thread.run(Thread.java:1012)

    查询系统中使用:PendingIntent的地方做了版本判断,

    PendingIntent piResult;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        piResult = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_IMMUTABLE);
    } else {
        piResult = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);
    }
    

    替换后依然启动报错,错误如上面相同,然后查询方法(mqtt库无法兼容高版本android13的问题),给与帮助, 使用

    paho.mqtt.android 3.3.0 的版本可以正常运行,
    下载 serviceLibrary-release.aar导入项目libs中,dependencies中添加依赖

           implementation files('libs/serviceLibrary-release.aar')

    移除依赖: implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.1.1')

    原先所有导入import org.eclipse.paho.android.service.MqttAndroidClient 替换成:import info.mqtt.android.service.MqttAndroidClient ;

    在MQTT android客户端,添加最后一个Ack.AUTO_ACK参数

    client = new MqttAndroidClient(context, serverURI, clientId, Ack.AUTO_ACK);

    替换后运行出现的错误是:

    E/AndroidRuntime: FATAL EXCEPTION: MQTT Rec: paho5848521383808
        Process: com.xxx.xxx, PID: 29345
        java.lang.NoClassDefFoundError: Failed resolution of: Ltimber/log/Timber;
            at info.mqtt.android.service.ping.AlarmPingSender.start(AlarmPingSender.kt:57)
            at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214)
            at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050)
            at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)
            at java.lang.Thread.run(Thread.java:1012)
         Caused by: java.lang.ClassNotFoundException: Didn't find class "timber.log.Timber" on path: DexPathList[[zip file "/data/app/~~3Ta3tpvBlIuf4vOif31Pcg==/com.zpd.mdm-07aOyZj6lephuXGMHk0ADA==/base.apk"],nativeLibraryDirectories=[/data/app/~~3Ta3tpvBlIuf4vOif31Pcg==/com.zpd.mdm-07aOyZj6lephuXGMHk0ADA==/lib/arm64, /data/app/~~3Ta3tpvBlIuf4vOif31Pcg==/com.zpd.mdm-07aOyZj6lephuXGMHk0ADA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            at info.mqtt.android.service.ping.AlarmPingSender.start(AlarmPingSender.kt:57) 
            at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214) 
            at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050) 
            at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151) 
            at java.lang.Thread.run(Thread.java:1012) 

    然后添加timber导入:

    implementation 'com.jakewharton.timber:timber:4.7.1' 
    

    编译后重新运行正常了

  • 相关阅读:
    这个岗位年薪几十万,为何各大科技企业仍招不到人?
    期货开户服从命令不抱幻想
    单例模式--Java
    linux安装mysql 8.0.20
    ElasticSearch系列——分词器
    【Node】Mac多版本Node切换
    python-后门脚本编写
    Git使用简明教程
    LeetCode 2656. K 个元素的最大和:一次遍历(附Python一行版代码)
    附录9-模板引擎
  • 原文地址:https://blog.csdn.net/yfy1907/article/details/134464713