Sujith S Manjavana
Sujith S Manjavana

Reputation: 1596

How to properly obfuscate code in android studio?

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.

Upvotes: 0

Views: 3063

Answers (1)

Mukund Jogi
Mukund Jogi

Reputation: 1395

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.

Upvotes: 3

Related Questions