【问题标题】:toString() always returns empty Json object with proguard enabledtoString() 始终返回启用 proguard 的空 Json 对象
【发布时间】:2017-02-08 03:23:04
【问题描述】:

我的proguard文件如下

# We only want obfuscation
-keepattributes InnerClasses,Signature

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify

# If you want to enable optimization, you should include the
# following:
#-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
#-optimizationpasses 5
#-allowaccessmodification

-libraryjars libs

-dontwarn bk.app.**
-keep class bk.app.com.** { *; }

-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.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.DialogFragment
-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService
-keep public interface com.android.vending.licensing.ILicensingService


# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
 native <methods>;
}

-keep public class * extends android.view.View {
 public <init>(android.content.Context);
 public <init>(android.content.Context, android.util.AttributeSet);
 public <init>(android.content.Context, android.util.AttributeSet, int);
 public void set*(...);
}

-keepclasseswithmembers class * {
 public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
 public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
 public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
 public static **[] values();
 public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
 public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator *;
   }

-keepclassmembers class **.R$* {
 public static <fields>;
}

-dontwarn android.support.**
-dontwarn com.google.ads.**

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }

-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

# Serilaizable
-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# APP DYNAMICS
-keep class com.appdynamics.eumagent.runtime.DontObfuscate
-keep @com.appdynamics.eumagent.runtime.DontObfuscate class * { *; }

# GOOGLE MAPS
-keep class com.google.android.gms.maps.** { *; }
-keep interface com.google.android.gms.maps.** { *; }

# TESTFAIRY
-keep class com.testfairy.** { *; }
-dontwarn com.testfairy.**

# Joda Time 2.3
-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }

# OTTO RULES
-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

# Butter knife
-keep @interface butterknife.*

-keepclasseswithmembers class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembers class * {
    @butterknife.* <methods>;
}

-keepclasseswithmembers class * {
    @butterknife.On* <methods>;
}

-keep class **$$ViewInjector {
    public static void inject(...);
    public static void reset(...);
}

-keep class **$$ViewBinder {
    public static void bind(...);
    public static void unbind(...);
}

# VIEWPAGER
-dontwarn com.viewpagerindicator.**

# OKHTTP
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**

#GLIDE
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# RETRO LAMBDA
-dontwarn java.lang.invoke.*

# RETROFIT RULES
-keep class com.squareup.** { *; }
-keep interface com.squareup.** { *; }
-dontwarn com.squareup.okhttp.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {@retrofit.http.* <methods>;}
-keep interface retrofit.** { *;}
-keep interface com.squareup.** { *; }
-dontwarn rx.**
-dontwarn retrofit.**

-keep class com.google.gson.* { *; }
-keep class com.google.inject.* { *; }
-keep class org.apache.http.* { *; }
-keep class org.apache.james.mime4j.* { *; }
-keep class javax.inject.* { *; }
-keep class retrofit.* { *; }

-keepattributes Signature
-keepattributes *Annotation*

-keepattributes EnclosingMethod

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

# 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

# Application classes that will be serialized/deserialized over Gson
-keep class bk.app.com.models.** { *; }
-keep class bk.app.com.models.** { <fields>; }

# keep enum so gson can deserialize it
-keepclassmembers enum * { *; }

模型是

import android.support.annotation.Keep;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.parceler.Parcel;

@Parcel @Keep
public class LoginRequest {

static Gson gson = new GsonBuilder().setPrettyPrinting().create();

@SerializedName("username")
@Expose
String username;
@SerializedName("password")
@Expose
String password;

public LoginRequest(){

}

/**
 *
 * @return
 * The username
 */
public String getUsername() {
    return username;
}

/**
 *
 * @param username
 * The username
 */
public void setUsername(String username) {
    this.username = username;
}

/**
 *
 * @return
 * The password
 */
public String getPassword() {
    return password;
}

/**
 *
 * @param password
 * The password
 */
public void setPassword(String password) {
    this.password = password;
}

@Override
public String toString() {
    return gson.toJson(this);
}
}

如果我的 gradle 文件中有 minifyEnabled true,当我调用该类的 toString() 方法时,我会得到一个空字符串的请求,例如 { }。我尝试了所有可能的解决方案,但没有成功。

【问题讨论】:

  • 您找到发生这种情况的原因了吗?
  • 不,还在等待有人提供反馈!

标签: android proguard android-proguard gson


【解决方案1】:

尝试将以下行添加到您的 proguard 规则中:

-keep class com.google.gson.** { *; }

【讨论】:

    【解决方案2】:

    在我看来它与 GSON 无关。如果您使用 proguard,请记住 proguard 可以从类中删除某些字段,例如,如果应用程序不使用类的 getter。因此,要解决此问题,只需将规则添加到您的 proguard 配置中,例如:

    # Classes that will be serialized/deserialized over Gson
     -keep class com.myclass.data.model.** { *; }
    

    或者更容易将注释 @Keep 添加到您的字段中,例如:

    @SerializedName("username")
    @Expose
    @Keep
    String username;
    @SerializedName("password")
    @Expose
    @Keep
    String password;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2017-08-31
      • 2020-03-14
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      相关资源
      最近更新 更多