如何在Android Studio中添加Paho-MQTT

5
我将尝试在Android Studio中使用Paho-MQTT。我参考了这个链接,并应该在gradle文件中添加以下内容。 链接要求添加以下内容:
repositories {
  maven {
    url "https://repo.eclipse.org/content/repositories/paho-releases/"
  }
}

dependencies {
  compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
    exclude module: 'support-v4'
  }
}

这段文字没有指定我应该使用哪个gradle文件,是用"gradle-proj"还是"gradle-app",所以我尝试了两种情况,在任何一种情况下都会收到类似以下错误的错误信息:

Error:(14, 0) Could not find method compile() for arguments [org.eclipse.paho:org.eclipse.paho.android.service:1.0.2, build_9fu4g5nmegp97bvhjazm7s8o8$_run_closure1$_closure3$_closure5@6dff2815] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
<a href="openFile:C:\Users\aba\AndroidStudioProjects\Test-PahoMQTT-1\build.gradle">Open File</a>

请告诉我应该使用哪个gradle文件“proj”还是“app”?如何将之前的代码正确添加到gradle中?

build.gradle app:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
    applicationId "com.example.alten.test_pahomqtt_1"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

//compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
//compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
//provided 'com.google.android.things:androidthings:0.2-devpreview'
//provided 'com.google.android.things:androidthings:0.1-devpreview'

//compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { exclude module: 'support-v4' }
compile files('libs/org.eclipse.paho.android.service-1.0.2.jar')
compile files('libs/org.eclipse.paho.client.mqttv3-1.0.2.jar')
}

build.gradle 项目:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    jcenter()

    maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" }
    maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()

    maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" }
    maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

当前错误

在此输入图片描述


你搞定了吗? - anoop4real
1个回答

6
在你的应用程序中你应该添加:
dependencies {
    . . .
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}

在你的项目中:

 buildscript {
     repositories {
         . . .
         maven {
             url "https://repo.eclipse.org/content/repositories/paho-releases/"
         }
     }
 }

不要忘记在application标签下添加服务到你的清单中:
<service
     android:name="org.eclipse.paho.android.service.MqttService"
     android:exported="false" />

那两行。
compile files('libs/org.eclipse.paho.android.service-1.0.2.jar')
compile files('libs/org.eclipse.paho.client.mqttv3-1.0.2.jar')

在libs文件夹中不包含这些jar文件之前,它将无法正常工作。如果您想坚持这种方法(复制jar文件),您可以在此处找到它们:

https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.android.service/ https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.client.mqttv3/


我修改了帖子,我在build.gradle(app)中添加的内容与您发布的完全相同,但是我添加了扩展名.jar。如果我不使用.jar,则无法正常运行相同的命令。此外,请查看附加的屏幕截图,它显示我无法使用MqttService..也许是因为我的自定义包..请告诉我如何解决这些错误。 - user2121
我看到你正在尝试从libs文件夹中添加JAR文件 compile files('libs/org.eclipse.paho.android.service-1.0.2.jar') 所以它会在你的libs文件夹中寻找文件 org.eclipse.paho.android.service-1.0.2.jar'。我发布的代码将在项目gradle中定义的存储库中查找此文件,它应该会在其中一个存储库中找到 maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" },我们手动添加了它。 - RadekJ
对于您发布的两个依赖项,我收到以下错误消息:Error:(40, 13) Failed to resolve: org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0 - user2121
如果您已经检查了我添加到帖子中的修改,您应该已经意识到我已经尝试过您提供的url。但无论如何,它都不起作用。 - user2121
如果您取消注释这两行代码//compile 'org.eclipse.paho: ...并删除compile files('libs/org.eclipse.paho ...这两行代码,会出现什么错误? - RadekJ
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接