hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
824 stars 153 forks source link

Unable to connect over IOT for minSDK version 19 #598

Open haransanjay opened 8 months ago

haransanjay commented 8 months ago

🐛 Bug Report

My app is connected to AWS IoT using aws-android-sdk-core and aws-android-sdk-iot. I am using hivemq-mqtt-client for the client side. However, when using minSdkVersion lower than 26(in my case it is 19), I am unable to connect over IOT. It is working fine if set to minsdkversion 26

🔬 How To Reproduce

Steps to reproduce the behavior:

1 I have attached sample code to reproduce the issue

Code sample

(1) here is the gradle file

 plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")

// id ("com.google.gms.google-services") id ("kotlin-kapt") // id ("com.google.firebase.crashlytics") id("com.apollographql.apollo3") version "4.0.0-alpha.1" id("com.github.sgtsilvio.gradle.android-retrofix") version "0.5.0" }

android { namespace 'com.eprintitsaas.avision.app' compileSdk 33

defaultConfig {
    applicationId "com.eprintitsaas.avision.app"
    minSdk 19
    targetSdk 29
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    configurations.all {
        resolutionStrategy {
            force 'androidx.preference:preference:1.1.1'
            force 'com.android.support:support-v4:25.1.1'
            exclude group: "com.android.support", module: "support-core-utils"
            exclude group: "com.android.support", module: "support-core-ui"
            exclude group: 'com.google.code.gson', module: 'gson'
        }
    }
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    // Support for Java 8 features

// coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } packagingOptions { resources { excludes += ['META-INF/INDEX.LIST', 'META-INF/io.netty.versions.properties'] } } kotlinOptions { jvmTarget = '1.8' } apollo { service("service") { packageName.set("com.eprintitsaas.avision.app") } } }

dependencies { implementation(files("libs/commons.jar")) implementation files('libs/WorkpathLib.aar') implementation files('libs/WorkpathLib-javadoc.jar')

implementation project(path: ':aws-android-sdk-core')
implementation project(path: ':aws-android-sdk-iot')

//Old before updating to api level 16

// implementation 'androidx.core:core-ktx:1.11.0-beta02' // implementation 'androidx.appcompat:appcompat:1.6.1' // implementation 'com.google.android.material:material:1.9.0'

//to support api level 16
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")

//firebase
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
implementation("com.google.firebase:firebase-crashlytics-ktx")
implementation("com.google.firebase:firebase-analytics-ktx")

/*RETROFIT 2.9.0*/
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2")

// implementation("com.squareup.okhttp3:logging-interceptor:3.0.0-RC1") // implementation ("com.squareup.okhttp3:okhttp:4.9.1"){ // force = true //API 19 support // } implementation("com.squareup.retrofit2:converter-scalars:2.6.4")

// implementation ("com.squareup.okhttp3:okhttp:3.12.13"){ // force = true //API 19 support // }

//butter knife
implementation("com.jakewharton:butterknife:10.2.3")
kapt ("com.jakewharton:butterknife-compiler:10.2.3")

//swipe to refresh
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")

//apollo
implementation("com.apollographql.apollo3:apollo-runtime:4.0.0-alpha.1")
implementation("com.apollographql.apollo:apollo-coroutines-support:2.5.14")

//change svg image color
implementation("com.github.devsideal:VectorChildFinder:1.0.0")

//multidex
implementation("androidx.multidex:multidex:2.0.1")

implementation ("androidx.appcompat:appcompat:1.6.1")
implementation ("com.google.android.material:material:1.9.0")
implementation ("androidx.constraintlayout:constraintlayout:2.1.4")

//noinspection GradleCompatible
implementation ("com.android.support:support-core-utils:28.0.0")

//dropbox
implementation ("com.dropbox.core:dropbox-core-sdk:3.1.5")

//gson
implementation ("com.google.code.gson:gson:2.10.1")

implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
implementation ("com.intuit.sdp:sdp-android:1.1.0")
implementation 'org.bouncycastle:bcpkix-jdk15on:1.67'
implementation "org.minidns:minidns-hla:0.3.2"

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'androidx.preference:preference:1.1.1'

implementation 'com.github.bumptech.glide:glide:4.15.1'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'

// implementation 'com.amazonaws:aws-android-sdk-iot:2.73.0' // implementation 'com.amazonaws:aws-android-sdk-core:2.73.0'

// coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.3' // coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.8' // implementation 'org.conscrypt:conscrypt-android:2.5.2'

retrofix("net.sourceforge.streamsupport:android-retrostreams:1.7.4") // for backporting streams
retrofix("net.sourceforge.streamsupport:android-retrofuture:1.7.4") // for backporting future
retrofix("org.threeten:threetenbp:1.6.5") // for backporting time

implementation 'com.hivemq:hivemq-mqtt-client:1.3.0'

}

(2) client code to connect over IOT

fun createClient(
        clientKey: String,
        clientId: String,
        brokerUrl: String,
        privateKey: String
    ): Mqtt5AsyncClient? {
        val keyManagerFactory: KeyManagerFactory = createKeyManagerFactory(privateKey)
        val trustManagerFactory: TrustManagerFactory = createTrustManagerFactory(clientKey)
        return MqttClient.builder()
            .useMqttVersion5()
            .identifier(clientId)
            .serverHost(brokerUrl)
            .serverPort(443)
            .sslConfig().protocols(Arrays.asList("TLSv1.2"))
            .keyManagerFactory(keyManagerFactory)
            .trustManagerFactory(trustManagerFactory)
            .applySslConfig()
            .buildAsync()
    }

    fun createKeyManagerFactory(privateKey: String): KeyManagerFactory {
        try {
            val keyStoreTemp = keystore

// val keyInputStream = ByteArrayInputStream(privateKey.toByteArray()) // keyStoreTemp.load(keyInputStream, null) val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()) keyManagerFactory.init(keyStoreTemp,"password".toCharArray()) return keyManagerFactory } catch (e: java.lang.Exception) { e.printStackTrace() throw IllegalArgumentException( "Failed to create KeyManagerFactory for MQTT Client connection.", e ) } }

    fun createTrustManagerFactory(certificate: String): TrustManagerFactory {
        try {
            val keyStoreTemp = keystore

// val certInputStream = ByteArrayInputStream(certificate.toByteArray()) // keyStore.load(certInputStream, null) val trustManagerFactory: TrustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()) trustManagerFactory.init(keyStoreTemp) return trustManagerFactory } catch (e: java.lang.Exception) { e.printStackTrace() throw IllegalArgumentException( "Failed to create TrustManagerFactory for MQTT Client connection.", e ) } }

Environment

Where are you running/using this client?

Android studio emulator Nexus 5 API 19

What version of this client are you using?

implementation 'com.hivemq:hivemq-mqtt-client:1.3.0'

JVM version? compileOptions { // Support for Java 8 features // coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } Operating System? Android kitkat 4.4 MAC os Which MQTT protocol version is being used? Mqtt5AsyncClient Which MQTT broker (name and version)? Please check gradle code and client code

Screenshots

Screenshot 2023-11-08 at 7 26 33 PM
shanjunmei commented 7 months ago

It can't even run on Android 6, api level 23, there are a lot of lambda usage, Android 6 is not compatible