HiveMQ MQTT Client Features: Android Support

Written by Silvio Giebl

Category: HiveMQ MQTT Client MQTT Client

Published: July 23, 2019


Welcome to the HiveMQ MQTT Client Features blog post series.

Last time we looked at the Fluent API style. Today we will take a closer look at the Android support of the HiveMQ MQTT Client library.


In general, the HiveMQ MQTT Client library is supported on Android 4.4 (API level 19) and higher versions. According to Google’s statistics this covers more than 95% of all running Android devices.

Independently of the supported API level, you have to grant your app the permission to use internet communication. Here is how you configure the permission in the AndroidManifest.xml:

1
2
3
4
5
<manifest>
    ...
    <uses-permission android:name="android.permission.INTERNET"/>
    ...
</manifest>

Support for Android API levels 24+

As the HiveMQ MQTT Client uses Java 8 language features, you have to specify the following in the app’s build.gradle file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
...
android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }
}
...

Additionally you have to set some proguard rules in the app’s proguard-rules.pro file:

1
2
3
4
...
-keepclassmembernames class io.netty.** { *; }
-keepclassmembernames class org.jctools.** { *; }
...

Please make sure that the proguard-rules.pro file is referenced in the app’s build.gradle file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
...
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        ...
    }
    ...
}
...

Support for Android API levels 19+

The above configuration is enough if your minimum supported Android API level is at least 24 (7.0 Nougat). Targeting lower Android versions requires additional configuration as Android still lacks support for Java 8 APIs like java.util.Optional, java.util.function.* and java.util.concurrent.CompletableFuture on versions below 24.

You can use the Android RetroFix gradle plugin to backport these APIs automatically. This plugin enables you to use the Java 8 APIs even if you have to support lower Android versions.

The following shows how to configure the Android RetroFix plugin in the app’s build.gradle file. You have to add the plugin and the two backport dependencies android-retrostreams and android-retrofuture:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
buildscript {
    repositories {
        google()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.2.1'
    }
}

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

...

dependencies {
    implementation 'com.hivemq:hivemq-mqtt-client:1.1.3'
    
    implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.1'
    implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.1'
    ...
}

When you increase the Android API level to 24+ in the future, you will only need to remove the plugin and the backport dependencies. You do not have to change your code.



For more information regarding Android support of the HiveMQ MQTT Client library, please refer to the project documentation.

If you have not already done so, check out the project on GitHub.

Have a great day, Silvio from the HiveMQ Team

About Silvio Giebl

Silvio Giebl is a software developer at HiveMQ and maintainer of the open source library “HiveMQ MQTT Client”. He is interested in high-performance applications on the JVM and reactive programming.
What's New in HiveMQ 4.2?
A Better Solution for IoT Security and MQTT