Building with ProGuard ignores android.app.* classes - android

I'm using ProGuard for the first time. I have all my activities, fragments, list fragments, and the like inside the root package of my project (I tried to move them but made no difference). I'm using the standard proguard-android.txt plus the proguard-project.txt where I can put anything I want, it makes no difference to these classes, because they are just ignored.
They're not listed in usage.txt,seeds.txt, mapping.txt or dump.txt, and they're not in the dex (library classes seem to be correctly treated though).
I tried with -keep public class <my.project.rootpackage>.MyActivity one for every ignored class, and many other things, the last one is:
-dontshrink
-dontoptimize
-dontobfuscate
-keep public class * extends android.app.Application
-keep public class * extends android.support.v4.app.FragmentActivity
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.ListFragment
-keep public class * extends android.preference.PreferenceActivity
I could be using a simpler project while learning, but I don't see why it shouldn't work, after all ProGuard seems to handle well the libraries. Of course, the project works well when built in debug mode.
ProGuard updated to v4.8, Eclipse 3.7.2, ADT and SDK updated, project target API 15, no messages from ProGuard.
What am I missing? I admit I've never had clear the build process. Do I have to learn Ant?
EDIT: when it doesn't work (that is, ProGuard finishes with an error), I often get this kind of errors:
Warning: my.project.package.ClassA: can't find referenced class my.project.package.ClassB
Note that the two classes can also be in the same package.

So it seems that the ignored classes are due to the fact that the "Build Automatically" feature prevented ProGuard from doing its job. With that unchecked, the "export signed application package" procedure goes well, but ProGuard is not obfuscating/shrinking. But this makes for another question.
References: Why start using -libraryjars when I never needed it before?
...EDIT: actually it's just the apk in project's bin that is not shrinked. The exported APK is fine. What an odyssey.

Related

How can I tell multidex to keep a jar properties file?

I have an issue which I'm pretty sure is related to multidex. Basically one of the libraries I use has a .properties resource bundle in a jar. As my application started growing I started having issues with it. I've made a couple of posts about it in the past but never had any solutions (post 1, post 2). Post 2 actually has a lot more details about the issue.
Basically this resource is missing unless I force some of the code on that Jar to run on the Application onCreate method. At least that was the issue until yesterday.
Yesterday I update a jar that has nothing to do with this but is now larger than it used to be (which I'm assuming means it has more methods), and now the code fails again on this same issue java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale en_US but now it fails for everyone, not just some users.
I took apart the apks using apktool for one that works and one that doesn't (basically downgrading those unrelated jars) and there is an unknown folder in both apks but the one that works has those LocalStrings.properties in that folder and the one that doesn't work doesn't have them in that folder. I have unzipped those unrelated jars just to make sure and they don't have that javax.servlet package in there and they are jars so they don't have anything else that might affect the gradle build.
Basically my theory right now is that those jars are just large enough to push the javax.servlet stuff out of the first dex file, but that isn't entirely right because the properties files don't even go in the dex file. If I just unzip the apk, I can see the javax package on the root folder and the resource files inside the right place but not LocalStrings.properties whereas if I do that for an apk that works, I can see LocalStrings.properties in there.
Right now I've been testing multiDexKeepProguard and I got all javax.servlet to go in the main dex file but I still can't get LocalStrings.properties to show up in the apk, even with:
-keepclassmembers class **$Properties
I've also tried a few other crazy things like putting the LocalStrings.properties files inside my main app package using the javax.servlet package and it didn't help either.
So what else can I try? Is this a bug or am I doing something wrong?
Thanks.
Edit: I would like to report that I've once again gotten past this issue by removing a dependency (an ad network) that I'm no longer using. I realized I still had that dependency when I used dex2smali to look at the first dex file and saw it was there. So it definitely appears to be an issue with the size of the jars it puts on the first dex file.
Edit: I have this on my proguard settings:
-keep class javax.** {*;}
-keep interface javax.** {*;}
-keepclassmembers class javax.** {*;}
-keepclassmembers class **$Properties
Not sure if this was the right way to do it, but apparently proguard was mistakenly deleting the *.properties files for me.
What fixed it was doing a build with
minifyEnabled false
and then going back to
minifyEnabled true
After that, the *.properties files were all available in the final build again.
It looks like your package property file is stripped by ProGuard. I am not sure whether you have tried below configurations
-keep class javax.servlet.**
or
-keep class javax.servlet.** { *; }
or
-optimizations !javax.servlet
-dontoptimize
Also, more additional references:
shrink-code: https://developer.android.com/studio/build/shrink-code
Processing resource files: https://docs.huihoo.com/proguard/manual/examples.html
-keeppackagenames https://stackoverflow.com/a/5866755/8034839
How to tell ProGuard to keep everything in a particular package?

Proguard: Keep classes in a certain package

I have some classes which are used via reflection. It appears ProGuard is removing these when I build for release.
Is there any way to specify that all the classes in a particular package (as opposed to the specific class names) to not be removed when ProGuard runs. As a note, obfuscation is wanted. As another note, all these classes extend a single class which is present.
Thanks
Using -keep class com.yourpackage.name.** { *; } works well in my case. It essentially uses the wildcard to keep all classes belonging to that package. Also note the *; is required.
Also, to help debug your issue, you should check the generated seeds.txt, if the class is included then its kept.
Moreover it could be another issue like using public static inner classes or so. The best way to debug would be to decompile the minified apk using dex2jar and JD-GUI and manually skim through the code to see what's being stripped or kept. Don't forget to -dontdeobfuscate before.
Have you tried to add this line in your proguard-rules.pro file?
-keep class com.company.application.PackagePrefix*
{
*;
}
If you don't want to use package prefix you can use ** instead.
Hope it helps.

Android ProGuard obfuscation of library: keep class not working

Intro: I have in AS 1 project with 2 models:
Android library project with some "Public API class"
Android APP dependent on above library (the library module is on the dependency list)
Task: I want to obfuscate my library project because I want to expose it as public SDK but keep my code protected...
What I did: So I made custom ProGuard rules:
-dontshrink
-dontoptimize
-dontpreverify
-keep class com.org.my_public_api_class_name
I skip all other stages in order to eliminate where the bug is to only obfuscation stage.
Result: Build of the APP module fails with errors like
Error: cannot find symbol class my_public_api_class_name
It seems for me that the problem is that the obfuscation NOT skipped the class I wanted to, so now he has some meaningless name and therefore in the APP, where I'm using him, The original name not exist.
Thanks,
To exclude your class from obfuscation, try this:
-keep class com.org.my_public_api_class_name**
-keepclassmembers class com.org.my_public_api_class_name** {*;}

Unknown classes ILicensingService notes when obfuscating Android project

I'm trying to build an Android release with Ant and ProGuard. I uncommented the following line in project.properties, despite the comment in said file noting that you shouldn't modify it ;):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
When obfuscating, I get the following notes:
[proguard] Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
[proguard] Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
I do understand why this is happening. These lines can be found in the default ProGuard config file (${sdk.dir}/tools/proguard/proguard-android.txt):
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
I'm not using the Google Licensing Service, so the classes are indeed unknown. I found a solution to get rid of these notes by updating the proguard-project.txt:
-dontnote **ILicensingService
My question: Is this the correct way of handling this? It seems to me that these classes shouldn't be kept by default anyway, since that lib isn't mandatory for an android project. The only way I can think of to achieve this is by copying the default config file to my project, removing the -keep lines and ignoring the default config file in the SDK completely. Which doesn't seem as the proper way to go either. Or am I missing something?
The setting "-dontnote com.google.vending.licensing.ILicensingService" is fine. In fact, it could have been part of the default configuration file.
The -keep option may be necessary for projects that use the library.
The -dontnote option may be nice to suppress the note about the -keep option, for projects that don't use the library. The note is just a gentle reminder that the configuration file could contain a typo, because the specified class doesn't seem to exist. It doesn't affect the processing.

Problems compiling an Android App with Ant and Proguard

I have an Android App which consists on different modules. The Main module is using some libs like Google's GSON or the v4.support.package. A custom build script with the right proguard.cfg will build it, too.
Now I must integrate another "Android-Library" which uses partly the same libs (GSON support.v4). Beside from getting a lot of Notes like
Note: duplicate definition of program class [com.google.gson.Gson]
I get also some Notes like
[proguard] Note: com.google.gson.UnsafeAllocator: can't find dynamically referenced class sun.misc.Unsafe
[proguard] Note: the configuration refers to the unknown class 'sun.misc.Unsafe'
that I find strange cause i have some 'keeps' in my Proguard.cfg especially for that:
-keepattributes Signature, Annotation
-keep class com.google.gson.** {*;}
-keep class sun.misc.Unsafe { *; }
which works well on my project without referencing the module-library inside it.
I'm on the Latest SDK and Tools, and added a custom proguard.cfg to the module-library, which works well on the module-lib itself (if build in standalone-mode).
It seems to me, that the build is not depending on custom proguard.cfg inside library-projects. Any idea on what to try highly appreciated
I finally found a solution for it myself:
with the last Android Tools (16), every Android-Library gets compiled on its own first.
So when the lib has not a "standart" build and defines some custom build script including proguard --keeps, and this --keeps are defined on the same Project (excluding Android SDK classes, as thei're not compiled) it leads to an proguard error.
The Solution was do remove proguard out of the lib and copy the --keeps inside the main App

Categories

Resources