4

Using Latest Android Studio and having updated all platform ( 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

As you can see proguard is enabled and rules also given for just keeping OpenSource class. Still when i build its apk / signed apk. And uses following commands on apk file :

  1. Unzip apk
  2. sh Dex2jar.sh classes.dex
  3. And by opening output jar file with Jd-GUI
  4. I get all all my classes right from MainActivity and rest all, as it is. There is no Obfuscation seen working

It would be very nice to have your suggestions on it. Thanks.

sandhya sasane
  • 1,334
  • 12
  • 19

3 Answers3

12

To enable ProGuard in Android Studio. Below is the sample how to enable default ProGuard in Android Studio.

1) Go to the build.gradle file of app

2) enable the proguard minifyEnabled true and useProguard true

3) enable shrinkResources true to reduce the APK size by shrinking resources.

4) proguardFiles getDefaultProguardFile('proguard-android.txt') to enable the default one. If you want to use your own proguard file then use the below rules.

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'
    }
}
Aditya
  • 3,525
  • 1
  • 31
  • 38
  • 3
    Yes that helped and worked too. I was missing 'useProguard true'. Accepted as answer and Up voted as it helped and a lot thanks too.. – sandhya sasane Aug 07 '18 at 12:08
  • @sandhyasasane I need your suggestions or help regarding foreground&JobIntentService. Can you please comment on this https://stackoverflow.com/questions/51627968/android-oreo-jobintentservice-keep-running-in-background-for-android-7-below-an/51710204?noredirect=1#comment90401799_51710204 – Kalai.G Aug 08 '18 at 10:04
  • Now android studio default is ```R8```. Is this answer still relevant? Can I use ```Proguard``` instead of ```R8```? – Sujith S Manjavana Jul 08 '22 at 14:46
0

Newer gradle plugin versions don't use proguard. It uses R8 compiler instead.

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

If you want to use proguard (in release build), add below to gradle.properties

android.enableR8=false

and use following in build.gradle.

buildTypes {
    release {
        debuggable false
        useProguard true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • This will cause `The option setting 'android.enableR8=false' is deprecated.` error – AgentP Jul 11 '20 at 17:08
  • A problem occurred evaluating project ':app'. > No signature of method: build_bqunwevzizsj6khnvnypa8ix8.android() this error occurs when i write userProguard true – Abdullah Programmer Oct 21 '22 at 16:24
0

In case you think you are doing everything right (default proguard-rules, minifyEnabled = true, etc.) and yet classes and methods are not being obfuscated, please check that the build variant you are trying to obfuscate is NOT DEBUGGABLE (apparently debuggable builds are not obfuscated properly)

DaMat
  • 11
  • 1
  • 2