3

私は自分のアプリで単純なXMLシリアライザーを使用していますが、このSOの質問のおかげで、proguard.cfgの次の行を使用して、Proguardの難読化から正常に除外したようです。

-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; } 

apkに署名してエクスポートすることはできますが、アプリを実行しようとすると、res / rawディレクトリ(R.raw.home_screen_menu)のXMLファイルにアクセスしようとするとNullPointerExceptionが発生してクラッシュします。

私はこれでR.javaを除外しています:

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

私は一日中この設定ファイルで遊んでいますが、運がありません。誰かがこの問題またはそのようなものに遭遇しましたか?私が試した最新のことは、以下に示すような「adaptresource」オプションを使用することです。

-adaptresourcefilenames    **.xml
-adaptresourcefilecontents **.xml

参考までに、私のproguard.cfgの内容は次のとおりです。

#Use 5 step of optimization 
-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. This tool will unpack your processed jars,(if using windows you should then use):
-dontusemixedcaseclassnames

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting
-dontskipnonpubliclibraryclasses

#Preverification is irrelevant for the dex compiler and the Dalvik VM, so we can switch it off with the -dontpreverify option.
-dontpreverify

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields).
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-dump ../bin/class_files.txt
-printseeds ../bin/seeds.txt
-printusage ../bin/unused.txt
-printmapping ../bin/mapping.txt 

-dontskipnonpubliclibraryclassmembers

#Ignore warnings for roboguice.activity.RoboAccountAuthenticatorActivity
-dontwarn roboguice.activity.RoboAccountAuthenticatorActivity

-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.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.google.inject.Binder

# Keep annotations
-keepattributes *Annotation*
-keepattributes Signature

#To remove debug logs:
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** w(...);
}

# Roboguice
-keepclassmembers class * {
    @com.google.inject.Inject <init>(...);
}

# SimpleXML
-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; } 
-dontwarn javax.xml.stream.**
-dontwarn javax.xml.namespace.**

-keep public class roboguice.**

# There's no way to keep all @Observes methods, so use the On*Event convention to identify event handlers
-keepclassmembers class * { 
    void *(**On*Event); 
}

-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 * {
    native <methods>;
}

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

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

-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 {
    static android.os.Parcelable$Creator CREATOR;
}

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

2 に答える 2

6

私はまったく同じ問題を抱えています。最後に、この質問と回答のおかげでそれを解決します。私はここに最終的な構成を残しました:

build.gradle:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

dependencies {
    compile('org.simpleframework:simple-xml:2.7.1') {
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'
    }
}

proguard-rules.pro(デフォルトのproguard構成への追加)

-dontwarn javax.xml.**

-keep public class org.simpleframework.**{ *; }
-keep class org.simpleframework.xml.**{ *; }
-keep class org.simpleframework.xml.core.**{ *; }
-keep class org.simpleframework.xml.util.**{ *; }

-keepattributes Signature
-keepattributes *Annotation*

# Ignore our XML Serialization classes
-keep public class your.annotated.pojo.models.*{
  public protected private *;
}

それが誰かに役立つことを願っています

于 2015-02-17T09:27:11.913 に答える
4

生のファイルはそのままにして大丈夫であることがわかりました。問題は、SimpleXMLシリアライザーのアノテーションを使用したシリアル化に使用されるクラスでした。アプリにログステートメントを追加しましたが、問題のある行で次のエラーが見つかりました:「属性'id'は2行目のクラスcom.xxx.ListViewItemsに一致しません」。これにより、プライベートの「id」変数が難読化されており、SimpleXMLシリアライザーによって認識されていないことに気付きました。ListViewItems.javaのコードは次のとおりです。

@Attribute(required=false)
private String id;  // This was being obfuscated and thus unrecognizable!

クラスの難読化を完全にスキップするためにProguardに追加したものは次のとおりです(おそらくより良い方法があります)。

# Ignore our XML Serialization classes
-keep public class com.xxx.ListViewItems {
  public protected private *;
}
于 2012-04-17T21:59:32.027 に答える