【问题标题】:What are the proguard rules for SignalR android client?SignalR android客户端的proguard规则是什么?
【发布时间】:2018-07-31 09:58:21
【问题描述】:

我有一个使用 SignalR 的聊天应用程序。最近我启用了 PROGUARD。 Gson proguard 规则没问题。对于调试 apk,聊天消息发送到服务器 OK。服务器将消息推送到 chat_Receiver 但 我只能在 chat_Receiver 的“SignalR LOG”下方看到响应。

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);

但我没有在“ChatMessageResponse LOG”下方获得发布 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 文件如下:

# 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 *;
    }

即使在启用 Proguard 之后,调试 apk 也一切正常。问题仅适用于发布 apk。这将是什么解决方案或proguard规则。?

【问题讨论】:

  • 您确定这是Proguard 问题吗?如果是,应该有一些包含 ClassNotFoundException 的错误。检查日志。否则检查调试和发布配置。可能缺少一些 Key。
  • 没有发现错误。但是在禁用 proguard 之后,发布 apk 一切正常。

标签: android proguard signalr-hub android-proguard signalr.client


【解决方案1】:

在您的 proguard 规则中添加以下行:

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

这对我有用!

【讨论】:

  • 在我的情况下,在发布版本WebSocketWebSocket connection stopping with code 1000 and reason ' ' 建立连接后立即自动停止。添加规则就可以了。非常感谢!
  • 不要忘记在 proguard 规则中添加您的自定义模型,使用 while 在 .on() 方法中接收响应。
猜你喜欢
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2019-01-15
  • 2015-04-14
  • 2014-03-10
相关资源
最近更新 更多