简体   繁体   English

Android:Proguard配置,用于混淆类名和方法

[英]Android: Proguard configuration to obfuscate class names and methods

As I know there are some classes which should not be obfuscated and also their names must persisted like 'Activities'. 据我所知,有些类不应该被混淆,而且它们的名称必须像“活动”一样持久化。 However I want other classes and packages inside my code be renamed. 但是我希望我的代码中的其他类和包被重命名。 here is build.gradle inside app folder: apply plugin: 'com.android.application' 这里是build.gradle里面的app文件夹:apply plugin:'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "apt.eve.good.morning"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

I use this proguard configuration for my application ( app\\proguard-rules.pro ): 我将这个proguard配置用于我的应用程序( app\\proguard-rules.pro ):

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-useuniqueclassmembernames
-verbose

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

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

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}
-allowobfuscations class *
# We want to keep methods in Activity that could be used in the XML attribute onClick
-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);
}

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

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

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

It do optimization but as I checked inside my classses.dex all class names remained unchanged. 它做了优化,但是当我在classses.dex中查看时,所有类名都保持不变。 So I want to know what I missed in my configuration file which is not obfuscating class/method names? 所以我想知道我在配置文件中遗漏了哪些不是混淆类/方法名称?

PS1 I have searched several questions but I can't imagine what's wrong here. PS1我搜索了几个问题,但我无法想象这里有什么问题。

PS2 I have properly configured my android studio and changes on proguard configuration applies without no problem on my released .apk file. PS2我已经正确配置了我的android工作室,对proguard配置的更改适用于我发布的.apk文件没有任何问题。

Make sure you check the obfuscated .dex files. 确保检查混淆的.dex文件。

With common gradle build script the build/** folders may contain several unobfuscated versions of .dex/.class files. 使用公共gradle构建脚本, build/**文件夹可能包含几个未混淆的.dex / .class文件版本。

Ultimately the resulting .apk/.aar should be obfuscated, so if you are unzipping that one, and the the classes are not obfuscated, then something doesn't work as expected. 最终产生的.apk / .aar应该被混淆,所以如果你解压缩那个,并且类没有被混淆,那么某些东西不能按预期工作。

As OP noted, it's also important to verify your [disassembly] tools work correctly. 正如OP所指出的,验证[反汇编]工具是否正常工作也很重要。

Viewing binary form of .dex from .apk is usually enough to spot [un]obfuscated symbols (try with unobfuscated .class, the symbols are easily readable even in text editor, in obfuscated .dex the chain of symbols like "aa", "ab", ... is often well visible too). 从.apk查看.dex的二进制形式通常足以发现[un]混淆的符号(尝试使用未混淆的.class,即使在文本编辑器中,符号也很容易读取,在混淆的.dex中,符号链如“aa”,“ ab“,......通常也很明显。”

Also running gradle proguard task manually with verbose option on may help to identify if the proguard was run and upon what files. 同时使用详细选项手动运行gradle proguard任务可能有助于识别是否运行了proguard以及运行了什么文件。

Based on your gradle file ( minifyEnabled=true ) and proguard config. 基于您的gradle文件( minifyEnabled=true )和proguard配置。 You appear to be in good shape already. 你似乎已经处于良好状态。

To quickly confirm that your app obfuscating your classes, check the resulting mapping.txt file when you make release builds. 要快速确认您的应用程序是否对您的类进行了模糊处理,请在创建发布版本时检查生成的mapping.txt文件。 This file "provides a translation between the original and obfuscated class, method, and field names." 此文件“提供原始和混淆的类,方法和字段名称之间的转换。”

Here is an example of a mapping.txt that obsfucates a hockeyapp library : 这是一个阻止hockeyapp库的mapping.txt示例:

net.hockeyapp.android.tasks.AttachmentDownloader -> net.hockeyapp.android.d.a:
    java.util.Queue queue -> a
    boolean downloadRunning -> b
    67:67:net.hockeyapp.android.tasks.AttachmentDownloader getInstance() -> a

Lots more info can be found in the "Shrink Your Code and Resources" article here : https://developer.android.com/studio/build/shrink-code.html 有关更多信息,请参阅此处的“收缩代码和资源”一文: https//developer.android.com/studio/build/shrink-code.html

Do you already changed 你已经改变了吗?

minifyEnabled=true

Inside app -> build.gradle? 内部app - > build.gradle?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM