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
832 stars 153 forks source link

Proguard rules not enough #545

Closed Canato closed 1 year ago

Canato commented 1 year ago

Expected behavior

Using the provide proguard settings the client should build without issues

Actual behavior

Using the provide rules:

-keepclassmembernames class io.netty.** { *; }
-keepclassmembers class org.jctools.** { *; }

or

-keep class io.netty.** { *; }
-keep class org.jctools.** { *; }

We get the exception ExceptionInInitializerError this is related to com.hivemq.client.mqtt.MqttClientState.<clinit> (MqttClientState.java:74 where we have the exception NoSuchMethodException because the values inside AtomicReference class used on MqttClientConfig are empty. a.k.a values[]

Logs:

com.hivemq.client.internal.mqtt.MqttClientConfig.<init> (MqttClientConfig.java:95)
com.hivemq.client.internal.mqtt.MqttRxClientBuilderBase.buildClientConfig (MqttRxClientBuilderBase.java:213)
com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.buildClientConfig (Mqtt3RxClientViewBuilder.java:112)
com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.buildRxDelegate (Mqtt3RxClientViewBuilder.java:100)
com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.buildAsyncDelegate (Mqtt3RxClientViewBuilder.java:104)
com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.buildAsync (Mqtt3RxClientViewBuilder.java:91)
com.hivemq.client.internal.mqtt.mqtt3.Mqtt3RxClientViewBuilder.buildAsync (Mqtt3RxClientViewBuilder.java:38)

As can be seen we do call state = new AtomicReference<>(MqttClientState.DISCONNECTED); line 95 on MqttClientConfig but on line 74 at MqttClientState when we call the value:

private static final @NotNull EnumSet<MqttClientState> CONNECTED_OR_RECONNECT = EnumSet.of(CONNECTED, DISCONNECTED_RECONNECT, CONNECTING_RECONNECT);

It crashes.

To Reproduce

Not sure

Adding the pro guard rules

-keep class com.hivemq.client.mqtt.MqttClientState, !com.hivemq.client.mqtt.** { *; }
-keep class com.hivemq.client.internal.mqtt.MqttClientConfig, !com.hivemq.client.internal.mqtt.** { *; }

solve the issue.

Should update the documentation? didn't find it in this repo, can open a PR if helps.

Details

SgtSilvio commented 1 year ago

Hi @Canato It seems that you are missing the proguard-android-optimize.txt in your proguardFiles configuration.

android {
    buildTypes {
        release {
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

The file proguard-android-optimize.txt configures necessary rules for enum classes as you can see here: https://android.googlesource.com/platform/sdk/+/master/files/proguard-android-optimize.txt#48

Please comment if this helps.

Canato commented 1 year ago

Indeed we don't use this file on the project, never had the need before! Thanks @SgtSilvio