Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio 1.1.0 setting minifyEnabled true causing issues with app

Here's my gradle.build file

defaultConfig {

    minSdkVersion 15
    targetSdkVersion 21
    versionCode 2
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Proguard-rules.pro file

-keepclassmembers class * extends de.greenrobot.dao.AbstractDao {
    public static java.lang.String TABLENAME;
}
-keep class **$Properties

-dontwarn com.squareup.**
-dontwarn okio.**
-dontwarn retrofit.**
-dontwarn org.joda.time.**

I have one of the java class as

public class Endpoints {
    public final static String GET_ENDPOINT = "MY_ENDPOINT";
}

which I use in my retrofit restadapter as

 RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(Endpoints.GET_ENDPOINT)
            .setLogLevel(RestAdapter.LogLevel.NONE)
            .setConverter(new GsonConverter(gson))
            .setClient(new OkClient(BusProvider.getClientInstance()))
            .build();

Now when minifiyEnabled is false, the entire code works just fine but I set minifyEnabled true, the network call doesn't happen. My app calls this endpoint as soon as it is launched but the network logs dont show the network request being made. Can someone tell me whats wrong here?

like image 788
Pdksock Avatar asked Apr 25 '15 09:04

Pdksock


People also ask

What is minifyEnabled in Android Studio?

minifyEnabled true. // Enables resource shrinking, which is performed by the. // Android Gradle plugin. shrinkResources true. // Includes the default ProGuard rules files that are packaged with.

How do you obfuscate apps on Android?

To create an obfuscated application package (. apk) file in IBM MobileFirst Platform Studio, you edit the Android native project properties file, then build your application in release mode. You can create an obfuscated Android application package with command-line tools.

How do I enable R8 on my Android?

To enable R8, open build. gradle module app file and add this piece of code inside the buildTypes . The code inside the release{} block means that this will be applied to the release build version of your application. If you launch the app in the emulator, this code is not executed.

What is ProGuard rule in Android?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.


1 Answers

Proguard doesn't play well with many of the libraries I used in my project.

For gson I added the proguard rules given by the gson team at http://google-gson.googlecode.com/svn/trunk/examples/android-proguard-example/proguard.cfg

You need to change

-keep class com.google.gson.examples.android.model.** { *; }

to

-keep class com.your.package.name.your.models.** { *; }

For retrofit you need to add

-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

Taken from here https://github.com/square/retrofit/issues/117

For joda library I added

-keep class org.joda.time.** { *; }
-dontwarn org.joda.time.**

For otto you need to add

-dontwarn com.squareup.**
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

Taken from here https://github.com/StephenAsherson/Android-OttoSample/blob/master/proguard-project.txt

I also added

-keep class com.squareup.okhttp.** { *; }

Before using these configuration changes proguard trimmed my app from 3.4 mb to 2 mb. After using these changes it shrinks it to 3.2 mb so I am just going to go with minifyEnabled false.

like image 98
Pdksock Avatar answered Sep 19 '22 14:09

Pdksock