Pro-guard 混淆在 android 工作室中不起作用

Pro-guard Obfuscation not working in android studio

使用最新的 Android Studio 并更新了所有平台(OsX):

Build.gradle :

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.xxxxxxlxxxxxx.apps.firebase"
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Proguard-rules.pro :

-keep public class OpenSource

如您所见,proguard 已启用,并且还给出了仅保留 OpenSource class 的规则。仍然在我构建它的 apk/签名 apk 时。并在 apk 文件上使用以下命令:

  1. 解压缩 apk
  2. sh Dex2jar.sh classes.dex
  3. 然后用 Jd-GUI 打开输出 jar 文件
  4. 我所有的 classes 都是从 MainActivity 中获取的,然后按原样休息。没有看到混淆工作

很高兴收到您的建议。谢谢

在 Android Studio 中启用 ProGuard。下面是如何在 Android Studio 中启用默认 ProGuard 的示例。

1) 转到应用

build.gradle 文件

2) 启用混淆器 minifyEnabled trueuseProguard true

3) 启用 shrinkResources true 通过缩减资源来减小 APK 大小。

4) proguardFiles getDefaultProguardFile('proguard-android.txt') 启用默认值。如果您想使用自己的混淆器文件,请使用以下规则。

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

    debug {
        debuggable true
        useProguard true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

较新的 gradle 插件版本不使用混淆器。它改用 R8 编译器。

When you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler to handle the following compile-time tasks:

https://developer.android.com/studio/build/shrink-code

如果您想使用混淆器(在发布版本中),请在下面添加到 gradle.properties

android.enableR8=false

并在 build.gradle.

中使用以下内容
buildTypes {
    release {
        debuggable false
        useProguard true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}