Minifying my App strips essential classes - android

My app runs fine when launched via USB in Android Studio. Though when I create an apk file and install that, the app crashes immediately on launch with a ClassNotFoundException. Investigating further I disabled minifying, changing
minifyEnabled true
to
minifyEnabled false
in build.gradle.
Without minifying, the apk file works. However, I don't want to refrain from minifying my app. How does the minifier work? Why does it strip essential classes of my app? How can I make the minifier to include all my classes?
My proguard-rules.pro file is empty except for comments.

Adding
-keep public class MyClass
to proguard-rules.pro did the trick. In the end I had to keep two classes this way. They both have something in common: They're both added via
<androidx.fragment.app.FragmentContainerView
in xml layout, not in code with FragmentManager. Those are the only classes added this way.

Related

How to ignore specific package in proguard

I am trying to build (release) an app and the app crashes due to the fact that there are generated methods that are being obfuscated by ProGuard. How can I ignore all of them in the release build?
I have tried to add the following in the proguard-rules.pro:
-keep public class !*Service_Impl.kt
but seems that it doesnt work. The classes that I want to ignore are named as
C1Service_Impl
C2Service_Impl
and so on and so forth, located in the generated java folder
com.application.myapp.persistence.services.C1Service
com.application.myapp.persistence.services.C2Service
Thanks in advance.

R8 error with 'com.google.android.play.core.assestpacks.' on androidTest builds

When building the androidTest apk I get the multiple of the follow error:
'com.google.android.play.core.assetpacks.c' cannot be mapped to 'Lcom/google/android/play/core/assetpacks/c;' because it is in conflict with an existing class with the same name. This usually happens when compiling a test application against a source application and having short generic names in the test application. Try giving 'Lcom/google/android/play/core/assetpacks/c;' a more specific name or add a keep rule to keep 'com.google.android.play.core.assetpacks.c'.
However my other builds work fine. Disabling minify works of course, but I want to test against a minify build.
I had the same problem and "android.enableR8=false" helps me because I use proguard at the moment.
gradle.properties file,
android.enableR8=false

Class name is not appearing in error Android Studio

In below error you can see RegisterActivity.a the class name is converted in some other characters. Can someone suggest what should I do in such a situation, when class name is not appearing in error logs.
at com.abc.angpau.appClasses.activities.RegisterActivity.a(:53)
at com.abc.angpau.appClasses.activities.c.a(Unknown Source:4)
You might have built a release version and proguard is active.
Look at the gradle files.
When you have minifyEnabled true proguard is active.
Usually you only want to enable it in release mode, so you can debug with all the original names if a crash occurs. But you might want to enable it in debug for a moment to test everything works as expected when proguard is enabled.
Proguard obfuscates the code changing the name of vars, methods... It also generates a mapping file so that you can then do the opposite translation to read stack traces. This mapping is used by some services that capture crashes like firebase to desimbolicate them.
And there is a config file so that if some classes/methods should not be obfuscated you should use it, usually when a lib requires this it provides you with the proguard config to keep names in some specific classes.

Using minifyEnabled true to avoid multidex disables breakpoints - How to fix?

I am working an a existing Android app which includes the Dropbox SDK. The SDK includes a ton of new classes which makes it necessary to use minifyEnabled true in buildsettings to avoid multidex.
While this is no problem in my release config it has a major downside when debugging the app: Most of the breakpoint do not work any more.
Even if a breakpoint works and I would like to use the "step into" feature to step through the code it happens that this is not possible. Instead of stepping to the code, the call stack shows obfuscated instead of the real code:
a.c:111,f (com.my.app.a)
xb:106,f (com.my.app.a)
onCreate:62, MyApp (com.my.app.TheClass)
...
So, I cannot set minifyEnabled false. Is there anything else I can do to still use my breakpoints?
If you only want to use ProGuard (minifyEnabled true) in order to prevent the app from requiring multidex, you can disable obfuscation and only use the shrinking feature of ProGuard (assuming that you dont use optimization e.g. when using proguard-android.txt as default config).
This should not create obfuscated stacktraces and allow you to debug the application.
Edit:
In order to disable obfuscation, just add
-dontobfuscate
to your configuration file.

Using proguard in android apps

I have been working on android app development from past 4 months and now I have developed my first app and as it is easy to decompile a apk so we should use dex or proguard for shrinking and protection.The problem is I have read in an article that proguard may change the code so sometimes a app may misbehave ,this is my first app and I don't want to mess up.So before using proguard in my app I have few questions -
1.What are the points to keep in mind before using proguard.
2.I read you can use keep command but proguard will not obfuscate that code and it will remain same,so I want my all code but as I will use keep it won't do anything.
3.How to make sure that the after functioning of app is same as before after using proguard.
4.Is is necessary to sign app or make key for using proguard?
Question1. What to keep in mind!
The docs state that there may be unintended events that occur from using proguard
Be aware that code shrinking slows down the build time, so you should
avoid using it on your debug build if possible. However, it's
important that you do enable code shrinking on your final APK used for
testing.
After ProGuard shrinks your code, reading a stack trace is difficult (if not impossible) because the method names are obfuscated.
I believe this answers question 3
The key word here is test, test, test! The moment you create your release apk. Test the functionality against your use cases to see if the application is still running the way it should.
If you don't have tests yet I would recommend write some at least unit tests before you release and test the proguard app against that.
Question 4: No you do not need a key to use proguard. I have used it on my debug builds before.
So your typical release build variant could look something like this:
//AndroidStudio3.0.1Canary
release {
postprocessing {
removeUnusedCode true
removeUnusedResources true
obfuscate true
optimizeCode true
proguardFile 'proguard-rules.pro'
}
}

Categories

Resources