无法使用Proguard模糊类和方法名称

时间:2017-10-30 07:59:30

标签: android proguard android-studio-3.0

启用了proguard的Android Studio中的CrashLytics测试项目不会混淆类和方法名称。正如您在提供的规则文件中看到的那样,我没有使用任何规则" proguard-rules.pro"。

  1. 我做错了什么?
  2. 是否有用于解释Proguard规则的参考文档或教程?
  3. 的build.gradle

    buildscript {
        repositories {
            maven { url 'https://maven.fabric.io/public' }
        }
    
        dependencies {
            // These docs use an open ended version so that our plugin
            // can be updated quickly in response to Android tooling updates
    
            // We recommend changing it to the latest version from our changelog:
            // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
            classpath 'io.fabric.tools:gradle:1.22.1'
        }
        }
    
    apply plugin: 'com.android.application'
    
    apply plugin: 'io.fabric'
    
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    
    android {
        signingConfigs {
            config {
                keyAlias 'key0'
                keyPassword 'A123456789'
                storeFile file('D:/Android/KeyStorePath/key1.jks')
                storePassword 'A123456'
            }
        }
        compileSdkVersion 26
        buildToolsVersion "26.0.0"
        defaultConfig {
            applicationId "com.example.ba.autocompletetextcustomadapter"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner 
     "android.support.test.runner.AndroidJUnitRunner"
            signingConfig signingConfigs.config
        }
        buildTypes {
            release {
                debuggable true
                minifyEnabled true
                shrinkResources false
                proguardFiles 'proguard-rules.pro'
            }
            debug {
                minifyEnabled true
                proguardFiles 'proguard-rules.pro'
            }
        }
        productFlavors {
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-
       core:3.0.1'
        compile('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') {
            transitive = true
        }
    }
    

    proguard-rules.pro

     # Add project specific ProGuard rules here.
    # By default, the flags in this file are appended to flags specified
    # in D:\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 *;
    #}
    
    # Uncomment this to preserve the line number information for
    # debugging stack traces.
    #-keepattributes SourceFile,LineNumberTable
    
    # If you keep the line number information, uncomment this to
    # hide the original source file name.
    

    apk分析器显示类和方法名称完全清晰

    apk analyser shows that Class and method names are completely clear

    截图下方的

    显示proguard正在运行

    below screenshot shows that proguard is working 更新

    好的,我不能混淆Activity名称,因为它应该通过Manifest文件引用,但是onCreate和forceCrash的方法名称怎么样,为什么它们仍然是可读的?

    import io.fabric.sdk.android.Fabric;
    import com.crashlytics.android.Crashlytics;
    
    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_main);
    }
    
    public void forceCrash(View view) {
        throw new RuntimeException("This is a crash");
    }
    
    }
    

3 个答案:

答案 0 :(得分:1)

替换

proguardFiles 'proguard-rules.pro'

对齐

getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

答案 1 :(得分:1)

  1. 你没有做错任何事。您的大多数代码都显然是混淆的 - 屏幕截图中的b,a和c类是。您的构建中会生成默认的ProGuard规则 - 您可以在项目的build \ intermediates \ proguard-files \文件夹中找到它们。由于此答案中解释的原因,活动名称未被混淆ProGuard Still Displays Full Activity Name
      

    活动名称永远不会被混淆,因为它们在manifest.xml中被引用。和android通过反射访问这些活动,因此无法更改其名称)

  2. 使用ProGuard规则在Android文档中进行了说明 - https://developer.android.com/studio/build/shrink-code.html,在ProGuard手册中https://www.guardsquare.com/en/proguard/manual/usage,最近有一篇关于ProGuard疑难解答的好文章 - https://medium.com/google-developers/troubleshooting-proguard-issues-on-android-bce9de4f8a74

答案 2 :(得分:0)

显然,实际上没有人混淆活动名称。 但是,您可以手动更改它。 只需将其名称修改为所需的名称即可。

对于混淆的方法和类,只需使用:

-obfuscationdictionary file.txt
-classobfuscationdictionary file.txt