toString()始终返回空的Json对象,并启用了proguard

问题描述 投票:0回答:2

我的专业保护文件如下

# 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
2个回答
0
投票

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

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

0
投票

在我看来,这与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;
© www.soinside.com 2019 - 2024. All rights reserved.