ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何将Paho-MQTT添加到android studio

2019-06-27 17:14:17  阅读:1079  来源: 互联网

标签:android mqtt paho iot hivemq


我想在android studio中使用Paho-MQTT.我提到了this link
我应该将以下内容添加到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
}

当前错误

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

不要忘记在应用程序标记下向您的清单添加服务:

<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之前将无法工作.如果你想坚持这种方法(复制罐子),你可以在这里找到它们:

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/

标签:android,mqtt,paho,iot,hivemq
来源: https://codeday.me/bug/20190627/1306736.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有