8

I'm trying to obfuscate code with proguard, so I enabled minify in release build types:

buildTypes {
    debug {
        minifyEnabled false
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
}

But when I generate 'release apk' and after I install it, application run slows (lag)..why this happen with minify enabled? This is my dependencies:

dependencies {
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:palette-v7:25.3.1'
    compile 'com.google.protobuf.nano:protobuf-javanano:3.0.0-alpha-2'
    compile 'com.jrummyapps:colorpicker:2.1.6'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile project(':library')

    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    androidTestCompile 'com.android.support:support-annotations:25.3.1'
}

This is my proguard-android.txt

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

I've try to add:

-keep class com.mylibrary.**
-keep interface com.mylibrary.**
-keep enum com.mylibrary.**

My library dependencies:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:palette-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'org.jetbrains:annotations-java5:15.0'
}
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • have you turned on the instant run please turn it off delete your build folder in the project build it again and let me know I don't think there is some issue with progaurd. – Reyansh Mishra May 13 '17 at 09:40
  • It doesn't happens in debug mode, only when I sign apk (release version) – Michele Lacorte May 13 '17 at 10:27
  • yeah did u try disabling instant run? – Reyansh Mishra May 13 '17 at 15:11
  • @ReyanshMishra tried but nothing... – Michele Lacorte May 13 '17 at 20:09
  • Could you post the ```proguard-android.txt``` file? You're probably obfuscating libraries that shouldn't be... Do you notice a lag when a certain library is being used or is it just in general? – ahasbini May 15 '17 at 08:55
  • @ahasbini done! check update – Michele Lacorte May 15 '17 at 08:58
  • What if you build the apk in debug mode and install manually (not using run in Android Studio) ?? May be something related with running with or without Android Studio instead of debug vs release mode. – Gabriel May 18 '17 at 16:42
  • @Gabriel This is what I do: generate apk release mode (signed) then copy it to my device and install it, than app not working correctly – Michele Lacorte May 18 '17 at 16:51
  • I understand that part. What I am saying is do the same with the debug apk. Take the debug apk from your app\build\outputs\apk folder then copy to your device manually and install it. Using Android Studio just for build, not for push and install. – Gabriel May 18 '17 at 17:04
  • @Gabriel debug apk works correctly, the strange think appears only on signed apk – Michele Lacorte May 18 '17 at 17:14
  • I'm really surprised this didn't get more down than up votes. Obfuscation will get rid of some hacks but the really secret stuff has to be done on your server. – danny117 May 18 '17 at 19:40
  • @danny117,so your saying the meat of the matter only lies on the server logic, am i correct? – Remario May 19 '17 at 10:58
  • @MicheleLacorte how bad at lag are we talking when the application is launch, can you instinctively time the lag reactions? – Remario May 19 '17 at 11:07
  • What does your application do at start-up?. If you have a custom `Application` class can you show us what it does? Also, which dependencies does your `compile project(':library')` pull in? – Rob Meeuwisse May 19 '17 at 13:15
  • @RobMeeuwisse my app is a fork of launcher3, otherwise I've added library dependencies – Michele Lacorte May 19 '17 at 13:28

4 Answers4

1

Although the comments within the proguard-android.txt specify some reasons, I'm surprised to see flags that disables certain optimizations as the default auto-generated proguard-android.txt file does not contain such flags when creating a new Android project. Try removing the following lines and see if there is an improvement:

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontoptimize
-dontpreverify

The rest of the file seems fine, although I haven't checked out all of the libraries that are being used but I've checked a couple and realized that you've added the necessary exceptions for them.

If that didn't work out, you'll have to make sure that exceptions for the libraries that are being used are added correctly, and also check proguard-android.txt inside your library module since it is referenced within the dependencies.

ahasbini
  • 6,761
  • 2
  • 29
  • 45
0

ProGuard does not causes lag of application at run time . It may be due to one of your library included in gradle file .

Amit Sharma
  • 645
  • 5
  • 13
0

I would strongly suggest that you use Android studio profiler to see what and how the app behaves. By this I mean the memory, CPU, networking etc. Save the results for comparison and then try the same app version just with proguard changes. It is very unlikely that the performance hit comes from proguard. If possible pass the results amd your observations here for further analysis. Also do note that there is also a difference of build type, do you have different code in debug/release flavors?

originx
  • 496
  • 6
  • 16
-1

Java always runs slow the first time out. Because it has to be loaded and interpreted. Run the test again. Look at this question.

Java program runs slower when code that is never executed is commented out

Community
  • 1
  • 1
danny117
  • 5,581
  • 1
  • 26
  • 35