How to keep fragment names in Firebase Analytics with Proguard? - android

I am using default Firebase Analytics implementation described here:
https://firebase.google.com/docs/analytics/get-started?platform=android
But when I check results in Firebase, name of fragments are "g,d,e,b". How can I fix this without hardcoding fragment names?

To keep the fragment names when using ProGuards, you should use -keepnames.
Lets assume you have a fragment with class identifier
com.example.app.fragments.frag1
To exclude that frag, you should add
-keepnames com.example.app.fragments.frag1 {}
To exclude all fragments including those that are in subfolders, use
-keepnames com.example.app.fragments.** {}
-keepnames is short for -keep,allowshrinking. If you use -keep only, you lose all ProGuards optimizations.
If you want properties to maintain their name, use
-keepnames com.example.app.fragments.** {*;}
For more information, you can check this.

Based on answer from #Abkarino, I came up with follwing solution:
-keep class com.packagename.fragments** { *; }
Where "com.packagename.fragments" is path to root of all fragments in project.

Related

How can I disable renaming names of fragments classes during obfuscation?

Which ProGuard/R8 rules to use?
Currently only Activites aren't renamed
I need to keep names of Fragment for screens logging as well but the code itself in fragments should be obfuscated
You can say proguard to keep names for all classes that extend androidx.fragment.app.Fragment (or another base Fragment class that you use)
-keepnames class * extends androidx.fragment.app.Fragment
I would highly suggest you to read this documentation:
https://www.guardsquare.com/manual/configuration/usage#keepoptions
Also use the -keepnames option in your proguard.cfg
I'll give you an example:
-keepnames class_specification
You could also use this in order to avoid obuscation of any class name:
-keepnames class ** { *; }
Simply add proguardrules to keep your directory which contains all the fragments so write below proguardrules
-keep public class com.your_app_name.app.view.fragments.** {*;}
Ex: My package name is com.tdscalculator.app and all my fragments are inside com -> tdscalculator -> app -> view -> fragments so for this example I have written above proguardrule so modify this rule accordingly
If you want to keep fragments of different directories then mention all fragments using below proguardrules
-keepnames class com.your_app_name.app.view.fragments.HomeFragment{}
Happy Coding :)

How to keep method names in Android Proguard R8?

I'm developing an Android Library and I'd like to have this result for Proguard.
I want to keep all package names, which I'm using "-keeppackagenames" to achieve.
I want to keep all class names.
I want to keep method names and method argument names for all public methods.
Other than that, I'd like to obfuscate and shrink everything else, including but not limited to all of non-public methods and even the body of the public methods.
How can I achieve that result?
Thank you very much beforehand for your help!
R8 itself is distributes as a library which has been run through R8. For that library we want to achieve exactly what you ask to ensure that developers using the library will not by accident use any APIs which are not supposed to be public (this has nothing to do with protecting IP as the project is open source).
The rules can be found here and I will explain the rationale for them below.
We try to be indirect and use annotations, so the main rules are:
-keep #com.android.tools.r8.Keep class * { public *; }
-keep #com.android.tools.r8.KeepForSubclassing class * { public *; protected *; }
All API classes are then annotated with #Keep or #KeepForSubclassing.
Then a number of attrubutes are kept
-keepattributes SourceFile, LineNumberTable, InnerClasses, EnclosingMethod, Exceptions, Signature
SourceFile and LineNumberTable are kept to get retracable stack traces. The rest for javac compilation and IDE integration for library users.
Then
-keepparameternames
is there for IDE integration getting the names of arguments in the API, and
-keeppackagenames com.android.tools.r8
-repackageclasses com.android.tools.r8.internal
to move as many classes as possible into the internal name space so the renamed classes does not show up in IDE's.
There are a few additional rules in the file to handle cases not covered by the annotation based approach.

Android Progaurd rules package names regex

I need to add my Json Model data classes to progaurd rules.
But my model classes are located in several packages.
Eg., assume model classes are present in the following locations
com.app.user.model.User
com.app.user.model.Profile
com.app.music.model.Song
com.app.music.model.Playlist
com.app.social.model.Media
.
.
.
I need to exclude all of the model class files alone
all model class resembles a pattern com.app.*.model.**
Is there any way to exclude all these files in a single rule?
We can use the same pattern as I mentioned in the question.
-keep class com.app.*.model.** { *; }

What does -keep class do in ProGuard

I know that keep class is suppose to keep the class.
But let's say if I keep class on a specific class, will it not obfuscate the said class?
-keep class com.myproject.activities.**
What is the difference between keep class vs keep public class
I know this is too late to answer but this may helpfull for someone.
From Guard Square documentation about -keep
-keep Specifies classes and class members (fields and methods) to be preserved as entry points to your code. For example, in order to keep an application, you can specify the main class along with its main method. In order to process a library, you should specify all publicly accessible elements.
You can check below links for more details about ProGuard specifires.
https://www.guardsquare.com/en/proguard/manual/usage
http://omgitsmgp.com/2013/09/09/a-conservative-guide-to-proguard-for-android/

How to make Proguard keep the class method names and attribute names as it is?

As above.
My ProGuard config is
-keepclassmembers public class myApp.interfaces.**
-keepclasseswithmembernames public class myApp.interfaces.**
-keepattributes Signature
-keepparameternames
Really new to this and no real idea what I'm doing.
What I'm trying to achieve is to make Proguard not change the method and variable names of all Classes inside myApp.interfaces.*. If they want to rearrange some algo stuff to optimise it or whatever, it's fine. As long as the names are not changed.
This must be done because of the Serializable issue between server and client.
Simply use:
-keep class myApp.interfaces.** { *; }
It will not touch the class or its members. If you get errors / warnings, Rebuild the project.

Categories

Resources