How to add Paho-MQTT to android studio

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

the link requires 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 does not indicate which of the gradle files to use I am using "gradle -proj or gradle -app" so I tried both in both cases, I got errors like

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>

      

tell me which gradle file "proj or app" should use? and how to add the previous code to gradle correctly?

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

+3


source to share


1 answer


In your application, you must 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 project:

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

      

Don't forget to add the service to your manifest in the tag application

:



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

      

These 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 the libs folder contains these jars. If you want to follow this approach (copy 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 /

+3


source







All Articles