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

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 :)

Related

Meaning of -if in Proguard

For the following Proguard rule(take Kotlin Serialization)
-if #kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
static <1>$Companion Companion;
}
Here what is the meaning of -if in context with the above rule? I tried looking into official proguard documentation but couldn't find easy to any info around that
I wrote those ProGuard rules. :) The pull request discussion about these changes may provide relevant background.
I understand your confusion, the ProGuard rules documentation is quite sparse.
-if class_specification
Specifies classes and class members that must be present to activate the subsequent keep option (-keep,
-keepclassmembers,...). The condition and the subsequent keep option can share wildcards and references to wildcards. For example, you can
keep classes on the condition that classes with related names exist in
your project, with frameworks like Dagger and Butterknife.
As written in the comments of the rules you copied this from:
# Keep `Companion` object fields of serializable classes.
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
-if #kotlinx.serialization.Serializable class **
...
So, -if #kotlinx.serialization.Serializable class ** should be read as: for all classes which have the #Serializable annotation applied (i.e., "serializable classes"). The <1> in -keepmembers subsequently refers to the ** wildcard, i.e., the fully-qualified class name.

How to keep fragment names in Firebase Analytics with Proguard?

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.

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.

How to tell Proguard to obfuscate class names

I would like proguard to obfuscate classnames. I have this line in Proguard.cfg
-keepclasseswithmembers class * {
public static <fields>;
}
-keepnames class * implements java.io.Serializable
-keep public class com.google.**
And I notice that what is not obfuscated is the class names. So running jdgui i see
com/test/abcd/ActualClass.java
public class ActualClassName extends Activity etc
moreover I see methods returning real classnames. like
ActualClassname aa();
and imports statements like
import com.abcd.ActualClassName
How do I get Proguard to obfuscate the classname itself. Its not just for Activities that I see this my Adapters are not being obfuscated. Well there is obfuscation going on but not class names.
Is the rules above what prevents the class names from being obfuscated?
Update: i have since removed the rules above and a Utility class that does not extend anything from Android is not being obfuscated. I'm now wondering if there is some implicit rule about keeping class names of classes that are referenced from classes that are being kept like Activity derivied classes? The classes whose names are not being obfuscated have a few things in common:
1) Static methods
2) Import of other types which are kept like those deriving from activity or serializable.
3) They have methods with parameters of other classes (Some of which might need to be kept).
However there is no where that I am specifically requesting that these utility classes should be kept.
There are several class that appear in your code that must retain the same fully qualified class name in order for android to be able to find them. One example as above are all Activity classes, as they are defined in the manifest by their full name as a String. If proguard were to rename the class then Android would no longer be able to find them.
The typical Proguard config will refer to the Proguard config in the Android SDK which will include several important lines like this one:
-keep public class * extends android.app.Activity
I think your problem is coming from your first line: '-keepclasseswithmembers' includes the class name, so in your case any class with a static field will keep its name. Changing it to simply '-keepclassmembers' will obfuscate the class names while leaving the static fields intact(which I'm assuming you want to do).
That said, I'm not sure why you want to do this, though. If you're trying to preserve the static variable names, shouldn't you want to preserve the class names as well, seeing as how that's how you'll be accessing the static fields? Why are you trying to preserve all of your static variable names?

Categories

Resources