使用安卓编写Mqtt程序时,Mqtt一连接服务器就报错,错误信息
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager;
显示如图
可是我的整个工程一点红色的报错都没有
调试发现程序卡在了 Mqtt要连接服务器的时候。调试也没出什么结果。查网上也没找到什么解决方法,讲得最多的就是缺少了依赖。
后来经过我多次修改依赖的情况下,程序终于对了,没有闪退了。
可是我至始至终没用到 gridlayout。查询官网给出的demo,依赖如下:
也没找出原因。
不过,如果你也遇到了跟我同样的问题,你不妨尝试也加一下这个依赖
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
}
最后一句是关键。
当然了,如果你知道问题出在哪里,欢迎告诉我一下。