如何将Paho-MQTT添加到android studio

问题描述:

我想在android studio中使用Paho-MQTT。我提到this link 我应该添加下列到摇篮文件如何将Paho-MQTT添加到android studio

的链接要求添加以下

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这个应用程序内”,所以我都尝试在上述任何一种情况下,我收到诸如

Error:(14, 0) Could not find method compile() for arguments [org.eclipse.paho:org.eclipse.paho.android.service:1.0.2, bu[email protected]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我应该使用‘凸出或应用程序’?以及如何正确添加前面的代码到gradle?

的build.gradle应用

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 
} 

电流误差

enter image description here

在你的应用程序,你应该加上:

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文件夹不包含这个罐子。如果你想坚持这种方法(复制罐),你可以在这里找到他们:

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/

+0

我修改后,我加入什么的build.gradle(APP)正是为你发布什么,但我在没有'.jar'i的情况下使用相同的命令不起作用时,我添加了扩展名'.jar'。此外,请看看屏幕截图,它表明我不能使用'MqttService'..maybe,因为我自己的软件包..请让我知道如何解决这些错误 – user2121

+0

,因为我看到你正在尝试添加jar从libs文件夹'编译文件('libs/org.eclipse.paho.android.service-1.0.2.jar')',所以它会查找文件'org.eclipse.paho.android.service-1.0.2.jar '在你的libs文件夹中。我发布的代码将在存储库中查找此文件(在项目gradle中定义),并且它应该在其中一个中找到它'maven {url“https://repo.eclipse.org/content/repositories/paho-快照/“}'我们手动添加 – RadekJ

+0

为您发布的两个依赖项我收到以下内容:错误:(40,13)无法解决:org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0 – user2121