【问题标题】:Code not getting obfuscated with Proguard?代码不会被 Proguard 混淆?
【发布时间】:2016-11-03 15:02:31
【问题描述】:

我在我的 android studio 中使用 proguard。我在我的 proguard-rules.pro 文件中实现了 Proguard。但是在发布 apk 之后,代码并没有被混淆。

这是 proguard-rules.pro 文件

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/chitra/Android/Sdk/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 *;
#}

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-printmapping out.map
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*,Signature,InnerClasses
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keepattributes InnerClasses
-keepattributes Exceptions
-dontoptimize
-repackageclasses ''
-dontwarn android.support.**
-dontwarn org.apache.harmony.awt.**
-dontwarn com.sun.mail.imap.protocol.**
-dontwarn javax.activation.CommandInfo
-dontwarn com.google.zxing.**
-dontwarn com.actionsmicro.androidkit.ezcast.imp.googlecast.**
-dontwarn org.apache.http.entity.mime.**
-dontwarn com.thetransactioncompany.jsonrpc2.server.MessageContext
-dontwarn com.actionsmicro.d.a.a
-dontwarn okio.Okio.**
-dontwarn okio.DeflaterSink.**
-dontwarn java.nio.file.**
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn android.support.design.**

# The official support library.
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v7.app.** { *; }
-keep interface android.support.v7.app.** { *; }
-keep class android.support.v7.widget.** { *; }
-keep interface android.support.v7.widget.** { *; }

# The official design library.
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

# Keep fragments
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

-keep public class * extends android.app.Activity
-keep public class * extends android.support.v7.app.ActionBarActivity
-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

# EventBus 3.0
-keep class de.greenrobot.event.** { *; }
-keep interface de.greenrobot.event.** { *; }

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

# crashlytics android
-keep class com.crashlytics.android.** { *; }
-keep interface com.crashlytics.android.** { *; }

# digits sdk android
-keep class com.digits.sdk.** { *; }
-keep interface com.digits.sdk.** { *; }

# firebase, google services and gson
-keep class com.google.** { *; }
-keep interface com.google.** { *; }

# square picasso
-keep class com.square.picasso.google.** { *; }
-keep interface com.square.picasso.** { *; }

-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 <fields>;
    !private <methods>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Native Methods

-keepclasseswithmembernames class * {
    native <methods>;
}

# Android Support Library

-keep class android.** {*;}

# Button methods

-keepclassmembers class * {

public void *ButtonClicked(android.view.View);

}

# Reflection

-keepclassmembers class com.elsinga.sample.proguard.SensorDescriptionFragment {

public void updateFields(com.elsinga.sample.proguard.SensorData);

}

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

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Remove Logging
-assumenosideeffects class android.util.Log {
    public static *** e(...);
    public static *** w(...);
    public static *** wtf(...);
    public static *** d(...);
    public static *** v(...);
}

这可能是什么问题?请告诉我。

【问题讨论】:

    标签: android proguard


    【解决方案1】:

    原因很可能是您在规则中排除了自己的代码。

    所以你的代码没有运行混淆。

    从您的问题来看,我不知道您的命名空间,但以下几行会影响一开始的 com. 的任何内容。

    因此,如果您的命名空间是 com.my.namespace,它将被排除在混淆之外。

    您应该考虑删除以下行,或完全量化它应该隐藏的命名空间,以免与您自己的冲突。

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

    更新:

    您还保留了所有扩展 Activity、Fragments 等的类。这很可能是您编写的每一页代码。

    # Keep fragments
    -keep public class * extends android.support.v4.app.Fragment
    -keep public class * extends android.app.Fragment
    
    -keep public class * extends android.app.Activity
    -keep public class * extends android.support.v7.app.ActionBarActivity
    -keep public class * extends android.app.Application 
    

    这不太可能有充分的理由。尝试删除上述内容。

    【讨论】:

    • 我已经删除了你提到的代码。但它仍然无法正常工作。
    • @Seenu69 你写了proguard文件吗?或从某处复制它?您是否在 gradle 构建文件中启用了它?你是使用它忽略的所有库,还是都是冗余代码。
    • @Doomskinght 我使用了我在 proguard 中提到的所有库。我也在 build.gradle 文件中启用了 proguard。
    • @Seenu69 查看编辑,看起来您还避免混淆所有活动和片段。
    • 我的包名以com.xxxx.xxxx开头。如果我使用 -keep class com.** { *; } 它不会混淆实际的代码。如何解决?
    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多