如何在Android Studio中正确混淆代码?

发布于 2025-02-09 10:32:52 字数 3721 浏览 2 评论 0原文

我正在尝试混淆debug在Android Studio Chipmunk中构建(buildToolSversion“ 30.0.3”,CompilesDkversion 32,Targetsdkversion 32,Gradle:7.2.1.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'

当我构建项目时,生成了映射txt和full-r8-config.txt。但是,当我将应用程序与Showjava Android应用程序进行反编译时,我仍然可以在代码中看到所有内容。根本没有变化。我想保留所有第三方库,并仅混淆我的代码。我该如何实现?谢谢。

I am trying to obfuscate the debug build in android studio Chipmunk (buildToolsVersion "30.0.3", compileSdkVersion 32, targetSdkVersion 32, gradle:7.2.1). I have the following code in my build.gradle file,

    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'
        }
    }

Here is my proguard-rules.pro file,

# 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** { *; }

My project contains the following libraries,

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'

When I build the project, the mapping.txt and full-r8-config.txt are generated. But when I decompile the app with the ShowJava android app, I can still see everything in my code. No changes at all. I want to keep all the third-party libraries and obfuscate only my code. How can I achieve this? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-02-16 10:32:52

如果您要创建签名的APK构建,您将不会面临此类问题,但是您的代码也需要更新

use 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

希望这对您有帮助。

If you are creating a signed APK build you won't face such issues but your code needs updates too

useProguard 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'
  }
}

Add this to proguard-rules.pro

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

Also add this line to gradle.properties

android.enableR8 = true

Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文