如何在android studio中正确混淆代码?

问题描述 投票:0回答:2

我正在尝试混淆 android studio Chipmunk (

debug
) 中的
buildToolsVersion "30.0.3", compileSdkVersion 32, targetSdkVersion 32, gradle:7.2.1
构建。我的 build.gradle 文件中有以下代码,

    buildTypes {
        release {
            minifyEnabled false//todo add proguard rules for every dependencies before setting this to true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true//todo add proguard rules for every dependencies before setting this to true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

这是我的 proguard-rules.pro 文件,

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
-keepclassmembers class com.mypackagename.helpers.JavascriptInterface {
   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.
#-renamesourcefileattribute SourceFile


-printconfiguration /Users/sujith/Desktop/R8/full-r8-config.txt

-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }

我的项目包含以下库,

implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    //added by me
    implementation 'androidx.preference:preference-ktx:1.2.0'
    implementation "androidx.viewpager2:viewpager2:1.0.0"
    implementation 'com.google.android.gms:play-services-auth:20.2.0'
    implementation 'com.google.android.gms:play-services-ads:21.0.0'
    implementation 'com.google.firebase:firebase-iid:21.1.0'
    implementation 'com.google.firebase:firebase-messaging:23.0.5'
    implementation "androidx.cardview:cardview:1.0.0"
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation('io.socket:socket.io-client:2.0.0') {
        exclude group: 'org.json', module: 'json'
    }
    implementation 'com.github.ismaeldivita:chip-navigation-bar:1.3.4'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    implementation 'androidx.core:core-splashscreen:1.0.0-rc01'
    implementation 'com.hbb20:ccp:2.6.0'//country code picker
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    implementation 'com.github.yalantis:ucrop:2.2.6-native'
    implementation 'com.github.chrisbanes:PhotoView:2.0.0'
//
classpath 'io.realm:realm-gradle-plugin:10.10.1'

当我构建项目时,会生成mapping.txt和full-r8-config.txt。但是当我使用 ShowJava android 应用程序反编译该应用程序时,我仍然可以看到代码中的所有内容。完全没有变化。我想保留所有第三方库并仅混淆我的代码。我怎样才能实现这个目标?谢谢你。

android-studio proguard android-r8
2个回答
2
投票

如果您正在创建签名的 APK 版本,您不会遇到此类问题,但您的代码也需要更新

使用Proguard true

buildTypes {
  release {
     minifyEnabled false//todo add proguard rules for every dependencies 
     before setting this to true
     useProguard true
     proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
    'proguard-rules.pro'
  }
  debug {
    minifyEnabled true//todo add proguard rules for every dependencies 
    before setting this to true
    useProguard true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
    'proguard-rules.pro'
  }
}

将此添加到

proguard-rules.pro

-keep public class ** {
    public *;
    protected *;
}

将此行添加到

gradle.properties

android.enableR8 = true

希望这对您有帮助。


0
投票

如果您将调试版本的标志

minifyEnabled
设置为
true
以在将其用于发布版本之前测试代码混淆,则还必须添加
debuggable false
标志:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    debug {
        // It's recommended to disable minify for debug build, it's only for testing purpose here
        debuggable false // Needed to force R8 to obfuscate the code for debug build
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.