2017-07-18 148 views
0

我想在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

回答

2

在你的應用程序,你應該加上:

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

相關問題