What are the proguard rules for SignalR android client?
Asked Answered
M

1

5

I have a chat application using SignalR. Recently I have enabled PROGUARD. Gson proguard rules are OK. For debug apk, chat message sending to server OK. Server pushes the message to chat_Receiver but I can see the response only on below "SignalR LOG" for chat_Receiver.

Platform.loadPlatformComponent(new AndroidPlatformComponent());
    String serverUrl = getString(R.string.BaseUrl) + "/signalr";
    printLog = true;
    mHubConnection = new HubConnection(serverUrl, "MobileNumber=" + sender_mobile_numberEn, false, new Logger() {
        @Override
        public void log(String s, LogLevel logLevel) {
            if (printLog) {
                Log.d(TAG, "SignalR LOG:" + s);
            }

        }
    });
    mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT);

But I don,t get the response on below "ChatMessageResponse LOG" for release apk.

private void registerReceiver() {
    Log.d(TAG, "registerReceiver called");

    mHubProxy.on("PushMessageToClient", new SubscriptionHandler1<ChatMessageResponsePOCO[]>() {
        @Override
        public void run(final ChatMessageResponsePOCO[] msgs) {

            Gson gsonForArray = new GsonBuilder().create();
            JsonArray jsonArray = gsonForArray.toJsonTree(msgs).getAsJsonArray();

            Log.d(TAG, "ChatMessageResponse LOG: " + jsonArray);

            processChatMessaages(jsonArray);

        }

    }, ChatMessageResponsePOCO[].class);
}

proguard-rules.pro file is as below:

# Add project specific ProGuard rules here.
            # By default, the flags in this file are appended to flags specified
# in F:\AndroidStudio/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
        # For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

            # Add any project specific keep options here:

            # If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
            #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

-dontwarn **
            -target 1.7
            -dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose

#############################################
        -injars      libs
-outjars     bin/classes-processed.jar

-repackageclasses ''
            -allowaccessmodification
-optimizations !code/simplification/arithmetic

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.preference.Preference
-keep public class * implements java.io.Serializable
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.ListFragment

-keep public class org.apache.** {
  <fields>;
  <methods>;
    }

#Don't obfuscate the model classes. 
            -keep class aerotxt.model.** { *; }


##---------------Begin: proguard configuration for Gson  ----------
            # Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
            -keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

            # Gson specific classes
-dontwarn sun.misc.**
            #-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson  ----------
            #############################################

            # ----------------------------------------
            # Android Support Library
# ----------------------------------------
        -dontwarn android.support.**
            -keep class android.support.** { *; }

#Below 2 attributes are used for otto library
-keepattributes *Annotation*
            -keepclassmembers class ** {
        @com.squareup.otto.Subscribe public *;
        @com.squareup.otto.Produce public *;
    }

Everything works fine for debug apk even after enabling Proguard. Problem is for only release apk. What will be the solution or the proguard rules for this.? 

Marsiella answered 31/7, 2018 at 9:58 Comment(2)
Are you sure this is a Proguard issue ? If yes there should be some error containing ClassNotFoundException . Check logs . Otherwise check the debug and release config . Maybe some Key is missing .Rondi
No error is found. but after disabling proguard everything works fine for release apk.Marsiella
E
9

Add the following lines in your proguard rules:

 -keep class com.microsoft.signalr.** { *; }
 -keep interface com.microsoft.signalr.** { *; }

This work for me!

Earp answered 15/4, 2019 at 18:38 Comment(2)
In my case it was so that in release build WebSocket stopped automatically right after establishing a connection with WebSocket connection stopping with code 1000 and reason ' '. Adding the rules did the trick. Much appreciate!Corrigendum
Don't forget to add your custom model in proguard rules, that using while receive response in .on<CustomModel>() method’s.Conductor

© 2022 - 2024 — McMap. All rights reserved.