Android Studio集成PAHO/Eclipse MQTT Android Service报错咨询
Android Studio 引入Paho/Eclipse MQTT Android Service构建失败问题
项目构建时抛出错误提示:A problem occurred evaluating project ':app',已提前在模块级build.gradle文件中添加所需依赖,相关配置与报错信息如下:
模块级app/build.gradle完整配置
plugins { id 'com.android.application' } android { compileSdk 32 defaultConfig { applicationId "com.example.mqqttestapp" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } repositories { maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' }
完整报错信息
Build file 'C:\Users\*****\MQQTtestApp\app\build.gradle' line: 30 A problem occurred evaluating project ':app'. > Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app\build.gradle'
报错原因
新版Android Studio默认在项目根目录的settings.gradle中开启了仓库配置优先级规则,要求所有第三方依赖的仓库地址统一在settings层配置,禁止在模块级build.gradle中单独添加repositories仓库配置,上述配置把Paho的Maven仓库错加在了模块级build.gradle里,触发规则校验导致报错。
解决方法
- 打开项目根目录下的
settings.gradle文件,找到dependencyResolutionManagement代码块,将Paho依赖所需的Eclipse仓库地址添加到该代码块下的repositories列表中 - 回到模块级
app/build.gradle文件,删除手动添加的整段repositories配置块(即包含Eclipse快照仓库地址的那段代码) - 点击Gradle同步按钮,等待依赖拉取完成即可正常构建
内容的提问来源于stack exchange,提问作者asapsonter
相关产品推荐
相关产品推荐

