Davoud
Davoud

Reputation: 2993

Cannot obfuscate class and method names with Proguard

CrashLytics test project in Android Studio with proguard enabled do not obfuscate Class and method names. As you see in provided rules file, I don't use any rules "proguard-rules.pro".

  1. Where I am doing wrong?
  2. Is there a reference document or tutorial for explaining Proguard rules?

build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // These docs use an open ended version so that our plugin
        // can be updated quickly in response to Android tooling updates

        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.22.1'
    }
    }

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    signingConfigs {
        config {
            keyAlias 'key0'
            keyPassword 'A123456789'
            storeFile file('D:/Android/KeyStorePath/key1.jks')
            storePassword 'A123456'
        }
    }
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.ba.autocompletetextcustomadapter"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 
 "android.support.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.config
    }
    buildTypes {
        release {
            debuggable true
            minifyEnabled true
            shrinkResources false
            proguardFiles 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-
   core:3.0.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') {
        transitive = true
    }
}

proguard-rules.pro

 # Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   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.

apk analyser shows that Class and method names are completely clear

apk analyser shows that Class and method names are completely clear

below screenshot shows that proguard is working

below screenshot shows that proguard is working Updated

Ok, I cannot obfuscate Activity names because it should be referable via Manifest file, but how about method names onCreate and forceCrash, why they are still readble?

import io.fabric.sdk.android.Fabric;
import com.crashlytics.android.Crashlytics;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());
    setContentView(R.layout.activity_main);
}

public void forceCrash(View view) {
    throw new RuntimeException("This is a crash");
}

}

Upvotes: 0

Views: 3271

Answers (3)

vdTOG
vdTOG

Reputation: 21

Apparentely, actually no one thing obfuscates activities names. But, you can change it manually. Just modify its name to some name you want.

For methods and classes obfuscating, just use the:

-obfuscationdictionary file.txt
-classobfuscationdictionary file.txt

Upvotes: 0

Lukáš Kutner
Lukáš Kutner

Reputation: 306

  1. You are doing nothing wrong. Most of your code is appparently obfuscated - the b, a and c classes in your screenshot are. There are default ProGuard rules being generated in your build - you can find them in build\intermediates\proguard-files\ folder of your project. Activity names are not obfuscated for reason explained in this answer ProGuard Still Displays Full Activity Name

    Activity names are never obfuscated because these are referenced in manifest.xml. and android access these activities via reflection so their names cannot be changed)

  2. Using ProGuard rules is explained in the Android docs - https://developer.android.com/studio/build/shrink-code.html, in ProGuard manual - https://www.guardsquare.com/en/proguard/manual/usage and recently there has been a nice article on troubleshooting ProGuard - https://medium.com/google-developers/troubleshooting-proguard-issues-on-android-bce9de4f8a74

Upvotes: 1

Rahul
Rahul

Reputation: 5049

replace

proguardFiles 'proguard-rules.pro'

line with

getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Upvotes: 1

Related Questions