Proguard not minifying when debuggable set to true - android

I am trying to test my app which has passed the 64K limit.
I have set minify on but it doesn't work when debuggable is enabled. However, if I build a version with debuggable off then it shrinks it by 2/3!
Am I missing something? This is the relevant part of my Gradle file:
buildTypes {
release {
minifyEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
debuggable true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
proguard-rules.pro:
-keep class .R
-keep class **.R$* {
<fields>;
}
-keepattributes *Annotation*
-keepclassmembers class ** {
#org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
-dontwarn com.google.**
-dontwarn com.squareup.picasso.**
-dontnote com.google.**
-dontnote com.squareup.picasso.**

With proguard enabled your code gets obfuscated and debugging will not be possible. That is why when you want to debug and set the flag true, proguard does not activate and that is why you do not see the minification of your apk.
Check this post. It has a similar question and will help you understand the conditions better.

Related

apache commons ftp doesn't work on release build apk

I'm able to upload files to an FTP Server when I'm using a debug build, but it fails in the release build:
it fails regardless if minify is enabled or not
it fails even when I remove proguard settings
I am able to generate the release build apk with no errors. The upload-to-ftp feature in the app just won't work.
build.gradle
implementation 'commons-net:commons-net:3.6'
....
minifyEnabled false
shrinkResources false
// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable true
signingConfig signingConfigs.release
proguard-rules.pro
-keepclasseswithmembernames class * {
native <methods>;
}
-keep class com.integratedbiometrics.** { *;}
-keep class org.libusb.** { *;}
-keep class com.futronictech.** { *;}
-keep class com.smufsbio.btsdk.** { *;}
-keep class org.apache.http.** { *; }
-keep class org.apache.commons.codec.** { *; }
-keep class org.apache.commons.logging.** { *; }
-keep class android.net.compatibility.** { *; }
-keep class android.net.http.** { *; }
-dontwarn org.apache.http.**
-dontwarn android.webkit.**
What am I missing?
I got it working already. I found out it was not the shrinking and obfuscating at all because setting minifyEnabled and shrinkResources to true in both release and debug build still gave me an issue with the release build while the debug build worked well.
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
The only difference then was the signingConfig, and when I set
signingConfig signingConfigs.release
in the debug build, the problem surfaced in the debug build.
Apparently, there was something wrong with the security certificate I was signing the apk with. Creating a new certificate fixed the issue for me. It's the first release so there's no issue in doing so.

After set minifyEnabled true in debug mode api call not working properly

After minifyEnabled true in build.gradle
Like below
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
When i make api call while minifyEnabled true i got success but some Parameters value change which is different from expected value.
And
When i make api call while **minifyEnabled false ** i got actual value which comes from back-end side.
(i.e.) i got isValidUser parameter in api response which is boolean
actual value is isValidUser=true which is expected but i got isValidUser=false.
help me what is the problem with my build.gradle file?
Please specify exactly what part is not working, but generally its from the models, so exclude them in the rules file as:
# Models
-keepclassmembers class com.example.models.** {*;}
# GSON
-keepattributes Signature
-keepattributes *Annotation*
-dontwarn sun.misc.**
-keep class com.google.gson.examples.android.model.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
with the help of Payam Kokabi,
issue is solved minifyEnabled true is will not change the value when we declare
-keep class YOUR CLASS ** { *; }
in your proguard-rules.pro file.
#Arbaz.in if you use #SerializedName and keep Gson you wouldnt need to keep your classes althought in some rare cases you need to do both
you can use these proguard rules for your GSON it should work:
GSON Proguard

FCM doesn't work after enabling Proguard in gradle

I connected FCM and tested before. And it worked fine.
However, I am ready to publish my app and enabled proguard in build.gradle
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt')
proguardFile 'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt')
proguardFile 'proguard-rules.pro'
// proguardFile 'proguard_debug.pro'
}
}
And applied this in proguard-rules.pro:
-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**
-dontwarn com.firebase.**
-dontnote com.firebase.client.core.GaePlatform
Everyone get push messages but just my test phone doesn't receive them which is proguard enabled. I converted my data models to Serialize format. But now this problem is happening.
How can I use FCM or other Firebase features such as Remote Config or Crashlystics?
Obfuscation renames variables in Kotlin data class without #SerializedName and as result Json not parsing to model class.
To prevent this you can use #Keep annotation
#Keep
data class MyClass(...)
or set #SerializedName to all values

How to enable proguard for only one module

I have a payment application with payumoney integration. It was working fine until i added the proguard. Recently I have added Proguard to my build.gradle file (Module: app)
` buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
}`
After this App gets closed when i proceed to payment. It is working fine when i Changed minifyEnabled to false.
Following is my Module:PayuMoneySdk Build.gradle File
`buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}`
How can I solve this issue? I can't disable proguard.
Can I enable proguard to only Module:app? And will it solve the issue?
I am new to programming. Please help!!
The solution is to look up for the methods and classess that needs to be exempted and add them to the proguard rules as follows..
-keep class com.mm.** {*;}
-keep class com.company.** {*;}
-keepclassmembers class com.mm.** {*;}
-keepclassmembers class com.company.** {*;}
add below lines in proguard-rule.pro file
-dontwarn okio.**
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
-keep class com.** { *; }

How to obfuscate UiAutomator code?

I have a test project developed in UiAutomator, which is not specific for one app, i.e., I don't even have any application associated to my test. My main folder only has the AndroidManifest, my code is all in the test folder.
I want to make my code available but ofuscated. So I want to obtain an APK of my code obfuscated.
My build.gradle contains
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFiles('proguard-rules.pro')
}
debug{
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFiles('proguard-rules.pro')
debuggable true
}
}
and proguard-rules.pro file is:
-keep public class * extends android.test.InstrumentationTestCase
-keep public class * extends junit.framework.TestCase
-keep public class * implements junit.framework.TestCase
-keep class android.support.** { *;}
-keep interface android.support.** { *;}
-keep public class junit.framework.** { *;}
-keep class android.uiautomator.** { *;}
-keep class com.impactToolTester.utility.Constants
-keepclasseswithmembernames class *{
public <init>(java.lang.Class,android.support.test.internal.util.AndroidRunnerParams);
}
-dontnote android.support.test.internal.runner.listener.CoverageListener
-dontwarn android.test.**
-dontnote org.junit.**
-dontnote junit.runner.*
-dontnote junit.framework.*
-dontnote android.net.**
-dontnote org.apache.http.**
When I run gradlew assembleAndroidTest the app-debug-androidTest-unaligned.apk is created in build/outputs/apk.
However, when I decompile the apk I realise it's not obfuscated at all. I know the proguard file is read because I started with several errors when I first run it.
I have the minifyEnabled in both debug and release because I'm not sure which is run with assembleAndroidTest.
I'm a real newbie when it comes to proguard and obfuscation in general so any help would be much appreciated.

Categories

Resources