how to add Paho-MQTT to android studio
Asked Answered
K

1

5

I am trying to use Paho-MQTT in android studio. I referred to this link and I should add the following to gradle files

the link demands adding the following:

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'
  }
}

the text did not specify which gradle file I use use "gradle-proj or gradle-app", so I tried both and in either cases i received errors such as

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>

please let me know which gradle file i should use "proj or app"? and how to add the previous code correctly to 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 project:

// 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
}

current error

enter image description here

Kellykellyann answered 18/7, 2017 at 12:28 Comment(1)
did you get it working?Persons
P
6

In your app you should add:

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'
}

In your proj:

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

dont forget about adding a service to your manifest under application tag:

<service
     android:name="org.eclipse.paho.android.service.MqttService"
     android:exported="false" />

Those two lines

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

will not work until libs folder dont contains this jars. If you want to stick to this approach (copying jars) you can find them here:

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/

Pathy answered 18/7, 2017 at 13:36 Comment(7)
i modified the post, what i added to build.gradle(app) is exactly as what you posted BUT i added the extension .jar when i use the same command without .jari doesnt work. moreover, please have a look at the attched screen shot , it shows that I can not use MqttService..maybe because my own package..please let me know how to fix these errorsKellykellyann
as I see you are trying to add jars from libs folder compile files('libs/org.eclipse.paho.android.service-1.0.2.jar') so it will look for file org.eclipse.paho.android.service-1.0.2.jar' in your libs folder. The code which I have posted will look for this file in repositories (definied in project gradle), and it should find it in one of them maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } which we manually addedPathy
for both dependencies you posted i receive the following:Error:(40, 13) Failed to resolve: org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0Kellykellyann
updated my answer: in proj gradle try url https://repo.eclipse.org/content/repositories/paho-releases/ instead of url "https://repo.eclipse.org/content/repositories/paho-snapshotsPathy
if you have checked the modification i added to the post, you would have realised that i already tried urlyou provided..anyway, it does not workKellykellyann
and what error will you get if you uncommnent those two lines //compile 'org.eclipse.paho: ... and remove compile files('libs/org.eclipse.paho ... lines ?Pathy
it is : Error:(40, 13) Failed to resolve: org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0Kellykellyann

© 2022 - 2024 — McMap. All rights reserved.