0

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.

Sujith S Manjavana
  • 1,417
  • 6
  • 33
  • 60

1 Answers1

2

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.

Mukund Jogi
  • 1,329
  • 5
  • 18
  • But I want to use R8. – Sujith S Manjavana Jun 28 '22 at 14:48
  • I have made changes to the answer hope it will help you. – Mukund Jogi Jun 29 '22 at 05:00
  • I believe R8 is enabled by default in newer version of android studio. And as I mentioned in my question, When I build the project, the mapping.txt and full-r8-config.txt are getting generated. This means it is enabled in my project as well. So, the problem is something else. Could you please explain the proguard rules you added? – Sujith S Manjavana Jun 29 '22 at 06:51
  • Can you please share which command do you use to create build? Did you try adding --release arguments? – Mukund Jogi Jun 29 '22 at 13:27
  • I want my ```debug``` version to be obfuscated. This is my code ```debug { minifyEnabled true//todo add proguard rules for every dependencies before setting this to true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' }``` – Sujith S Manjavana Jun 29 '22 at 16:43
  • Yes that you already mentioned in your question. I am asking about have you developed your build in release version? Can you share which command do you use to create build? – Mukund Jogi Jun 29 '22 at 17:26