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
853 stars 158 forks source link

Exception while initiating HiveMQ library (java.lang.NoClassDefFoundError: com.hivemq.client.internal.mqtt.mqtt3.-$$Lambda$NjA186hnUe4q8vDQzVPSuYbvUdc) #412

Closed bbd025 closed 4 years ago

bbd025 commented 4 years ago

Expected behavior

Initiating the library as expected.

Actual behavior

app crash on Lolipop and Marshmallow devices. I am getting the exception in Mqtt3SimpleAuthViewBuilder when trying to use the Function object which is a Java 8 object. Apparently, using the 'com.github.sgtsilvio.gradle.android-retrofix' plugin does not help in my case. I tried to follow these steps: https://hivemq.github.io/hivemq-mqtt-client/docs/installation/android/ but no luck. can you please advise? see all the build.gradle details below.

stacktrace: java.lang.NoClassDefFoundError: com.hivemq.client.internal.mqtt.mqtt3.-$$Lambda$NjA186hnUe4q8vDQzVPSuYbvUdc 04-23 13:21:02.931 27278-27278/com.trax.retailexecution.dev A/art: sart/runtime/runtime.cc:292] at com.hivemq.client.internal.mqtt.message.auth.mqtt3.Mqtt3SimpleAuthViewBuilder$Nested com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.simpleAuth() (Mqtt3RxClientViewBuilder.java:59) ....

Steps

Just init the library from app, and try to connect using simple auth.

Reproducer code

build.gradle top level:

buildscript {

    repositories {
        google()
       ...
        **gradlePluginPortal()**
    }

    dependencies {
        ...
        **classpath 'gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5'**
    }
}

build.gradle - app level:

apply plugin: 'com.github.sgtsilvio.gradle.android-retrofix'

dependencies {
     // HiveMq - MQTT
    implementation 'com.hivemq:hivemq-mqtt-client:1.2.0'
    implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.1'
    implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.1'
}

Details

bbd025 commented 4 years ago

Extra details:

  1. I am using Multidex (MyApplication : Multidex) - androidx
  2. currently I am using r8 ("android.enableR8 = true" at Gradle.properties), but disabling it did Not solve the bug
  3. ProGuard: I kept the classes as suggested at the installation part of this library. -keepclassmembernames class io.netty.* { ; } -keepclassmembers class org.jctools.* { ; }
  4. device I currently using: Samsung SM-J120F (Samsung Galaxy J1), 5.1.1 (Lollipop)
fraschbi commented 4 years ago

Hi @bbd025, We will have a look and respond with steps for resolution shortly. Thank you for your patience, Florian

SgtSilvio commented 4 years ago

Hi @bbd025 Could you provide your whole gradle build files or at least the whole android {...} configuration?

The following build files work, maybe you can find the difference to your build files. build.gradle - top level

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle - app level

buildscript {
    repositories {
        google()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.github.sgtsilvio.gradle.android-retrofix'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.hivemq.client.demoapp"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }
}

dependencies {
    implementation 'com.hivemq:hivemq-mqtt-client:1.2.0'
    implementation 'com.hivemq:hivemq-mqtt-client-websocket:1.2.0'

    implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.1'
    implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.1'

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
bbd025 commented 4 years ago

// app level

buildscript {
    repositories {
        google()
        gradlePluginPortal()
    }
    dependencies {
        classpath '**gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix**:0.3.5'
    }
}

apply plugin: 'com.android.application'
apply plugin: '**com.github.sgtsilvio.gradle.android-retrofix**'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'com.appspector.sdk'

kapt {
    arguments {
        arg("realm.ignoreKotlinNullability", true)
    }
}

**android** {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 179
        versionName "1.33.0.99"
        multiDexEnabled true

        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }

        dexOptions {
            jumboMode true
            preDexLibraries = false
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        dataBinding {
            enabled = true
        }

        packagingOptions {
            exclude 'META-INF/rxjava.properties'
            exclude 'META-INF/INDEX.LIST'
            exclude 'META-INF/io.netty.versions.properties'
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            zipAlignEnabled true
            lintOptions {
                checkReleaseBuilds false
                abortOnError false
                disable 'MissingTranslation'
            }
        }
        debug {
            minifyEnabled false
            zipAlignEnabled false
            applicationIdSuffix ".dev"
        }
    }

    flavorDimensions "trax"

    testOptions {
        unitTests {
            returnDefaultValues = true
        }
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

realm {
    kotlinExtensionsEnabled = true
}

repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://maven.google.com' }
    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.appspector.com/artifactory/android-sdk' }
}

repositories {
    flatDir {
        dirs external_lib_prefix + '/aar'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    api project(':trax_core_event_service')
    // DI - Dagger2
    implementation "com.google.dagger:dagger:$dagger_version"
    implementation "com.google.dagger:dagger-android:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    kapt "com.google.dagger:dagger-android-processor:$dagger_version"

    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    // Support library
    implementation 'com.google.android.material:material:1.2.0-alpha05'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'

    // Jetpack -Lifecycle
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

    // Jetpack - Paging
    implementation "androidx.paging:paging-runtime:2.1.1"
    implementation "androidx.paging:paging-rxjava2:2.1.1"

    // Jetpack - KTX
...

    // Multidex
    implementation 'androidx.multidex:multidex:2.0.0'

    // Constraint Layout
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'

    // RX Java
..

    // JSON
    implementation 'com.google.code.gson:gson:2.8.5'

    // Realm
    implementation 'io.realm:android-adapters:3.1.0'

    // Retrofit
...

    // Google Play Services - location
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    apply plugin: 'com.google.gms.google-services'

    // **HiveMq - MQTT**
    implementation 'com.hivemq:hivemq-mqtt-client:1.2.0'
    implementation 'com.hivemq:hivemq-mqtt-client-websocket:1.2.0'

    implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.1'
    implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.1'

.....

}
bbd025 commented 4 years ago

and for the latest app level - I get this build error: A problem occurred configuring project ':app'.

Could not resolve all artifacts for configuration ':app:classpath'. Could not resolve org.javassist:javassist:3.25.0-GA. Required by: project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 Could not resolve org.javassist:javassist:3.25.0-GA. Could not get resource 'https://plugins.gradle.org/m2/org/javassist/javassist/3.25.0-GA/javassist-3.25.0-GA.pom'. Could not HEAD 'https://plugins.gradle.org/m2/org/javassist/javassist/3.25.0-GA/javassist-3.25.0-GA.pom'. Received status code 530 from server: Could not resolve org.jetbrains:annotations:17.0.0. Required by: project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 Could not resolve org.jetbrains:annotations:17.0.0. Could not get resource 'https://plugins.gradle.org/m2/org/jetbrains/annotations/17.0.0/annotations-17.0.0.pom'. Could not HEAD 'https://plugins.gradle.org/m2/org/jetbrains/annotations/17.0.0/annotations-17.0.0.pom'. Received status code 530 from server: Could not resolve org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31. Required by: project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle-api:3.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.analytics-library:shared:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.lint:lint-gradle-api:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > androidx.databinding:databinding-compiler-common:3.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 > com.android.tools:sdk-common:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 > com.android.tools:common:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 > com.android.tools.build:manifest-merger:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 > com.android.tools.analytics-library:tracker:26.4.1 project :app > gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.3.5 > com.android.tools.build:gradle:3.4.1 > com.android.tools.build:builder:3.4.1 > com.android.tools:sdklib:26.4.1 > com.android.tools:repository:26.4.1 Skipped due to earlier error Could not resolve org.ow2.asm:asm:6.0.

SgtSilvio commented 4 years ago

@bbd025 as your minSdkVersion is 21, you do not need multidex (https://developer.android.com/studio/build/multidex#mdex-on-l). Can you please try to remove and check if multidex is the problem here?

SgtSilvio commented 4 years ago

and for the latest app level - I get this build error: A problem occurred configuring project ':app'.

Try to add jcenter() to the buildscript repositories block.

bbd025 commented 4 years ago

@SgtSilvio thanks for the quick response. I have managed to take ur code to my app level and to build a valid debug build. Note: it involved updating the Realm to latest stable version (from 5.8.0 to 6.0.2). Otherwise, it did Not built.

However, when trying to connect to simpleAuth() - I get the same exception.

SgtSilvio commented 4 years ago

@bbd025 The exception means that the Android build does not desugar lambdas. Setting the following should enable desugaring of lambdas.

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

You actually set this in the defaultConfig block and not the android block. Please try moving it to the android block.

Reference: https://developer.android.com/studio/write/java8-support

SgtSilvio commented 4 years ago

@bbd025 did the last comment help you?

bbd025 commented 4 years ago

@SgtSilvio we solved the problem by using our own servers for Lollipop and Marshmallow. For Nougat and above we use HiveMQ library.

So, I didn't have the chance to test your suggestion. However, if simpleAuth() works with your build.gradle files then it means that it's ok, right?

SgtSilvio commented 4 years ago

@bbd025 It works in general, and I think my last suggestion would fix your build as well. I close this now as you have found a solution for you. Nevertheless if you try out the suggestion, it would be nice to comment here.

afunx commented 2 years ago

I am an Android developer. I encountered the same problem yesterday. I solved it by read official website: https://hivemq.github.io/hivemq-mqtt-client/docs/installation/android/