首页 > 解决方案 > 如何将 PAHO/Eclipse MQTT Android 服务带入 Android Studio 项目

问题描述

我根本无法将 PAHO / Eclipse MQTT Android 服务带入 Android Studio 项目。

PAHO 文档建议您将此行添加到 app/build.gradle compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2')

但最终我发现它现在已经过时了,那行实际上应该是: api('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2')

完整的 app/build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.rlasater.mqttsubscriber"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
    //implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'

    //This is the problematic line:
    api('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2')
}

我尝试构建时的结果:

Build failed
:app:compileDebugKotlin
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class androidx.appcompat.app.AppCompatActivity, unresolved supertypes: androidx.core.app.TaskStackBuilder.SupportParentable

app/src/main/java/com/rlasater/mqttsubscriber/MainActivity.kt
Cannot access 'androidx.core.app.TaskStackBuilder.SupportParentable' which is a supertype of 'com.rlasater.mqttsubscriber.MainActivity'. Check your module classpath for missing or conflicting dependencies

<Previous line repeated several times ...>

Compilation error

需要做什么才能使 Android Studio 项目包含 MQTT Android 客户端?

为什么那条有问题的行会破坏基本的 Android 类 AppCompatActivity?这完全没有意义。

任何建议将不胜感激。我在 PAHO MQTT Java/Kotlin 上找不到比几年前更近的任何文档。我已经能够让更简单的类 (MqttClient) 在 Studio 中工作。显然,让这项服务工作将是最有益的。

标签: javaandroidbuild.gradlemqttpaho

解决方案


在 build.gradle 中使用以下依赖项并检查:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.2'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

推荐阅读