Android Proguard - dontobfuscate doesn't disable obfuscation - android

a question on android proguard obfuscation.
For some reason, when generating apk using ant, I want proguard to shrink size (remove unused classes) but no name obfuscation. In proguard.cfg, I added:
-dontobfuscate
all others in proguard.cfg are either -dontwarn or -keep class. But after decompile the generated apk (dex2jar), I found many names are still obfuscated. Why this happens?
Thanks

Since nobody was actually answering this, here is the link to the post that solves the issue: Using Proguard with Android without obfuscation
Basically, what you need to add below your -dontobfuscate instruction is:
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable
Important thing here is: !code/allocation/variable

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?

Enabling ProGuard in Android

I plan on using ProGuard to obfuscate the code of my Android app. I have been researching about it. Most of the articles and videos on YouTube seem to be out dated. From all what I have gathered, here is what need to be done:
In the project.properties class I will have to uncomment the line:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
I am using Crashlytics and it says that to have informative stack traces, I must add a line to ProGuard configuration:
-keepattributes SourceFile,LineNumberTable
I am guessing that the above line is appended to proguard-project.txt?
And then that's it. Please correct me where I am wrong.
Correct. In your project.properties you need to define the
path/filename for your Proguard configuration file, e.g.
proguard.config=proguard-project.txt
(in this case proguard-project.txt sits in the project's root folder)
Also correct. As per the Proguard documentation:
-keepattributes [attribute_filter]
Specifies any optional attributes to be preserved.
...
You should also keep the SourceFile and LineNumberTable
attributes for producing useful obfuscated stack traces. Finally, you
may want to keep annotations if your code depends on them. Only
applicable when obfuscating.

Android ProGuard Optimization

To perform optimization on my Android app I enabled the following statement in project.properties.
target=android-15
android.library=true
proguard.config=proguard-android-optimize.txt
and in the proguard-android-optimize.txt, I have written the -dontwarn and -keep statements.
My build is getting successful but it is not getting optimized: my problem is that The size of the APK is remaining the same.
Can anyone suggest me how can I optimize my code?
The line in project.properties that enables ProGuard with optimization typically looks like this:
proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
You can of course replace these two files by a single custom configuration file, but you then should be careful to specify the proper settings. If you specify -dontshrink, -dontoptimize, -dontobfuscate, or -keep with patterns that are too broad, you may not see the optimizations that you'd like.
With Ant and Eclipse, ProGuard is only applied for release builds.
With Gradle, you have to enable ProGuard in build.gradle.

Obfuscating Android library project, when aspectj used

I have Android library project. There are some .aj files (aspectJ) with pointcuts.
I need to obfuscate compiled artifact (jar). When i obfuscate it and add as library to another project, aspects stops working.
Can anyone help with obfuscation using ProGuard? Some configuration examples or any useful information.
May be it is not possible at all? Are there some alternatives?
Thanks.
I also have an android library with aspects. My pointcuts are not for my library code, but for the app's code (pointcuts on Activity.onCreate, for example), so people who use my library are already expected to set up their android projects as AspectJ projects.
Everything works perfectly without Proguard, but despite making every exception I can think of I couldn't get the advice to apply if my library jar was obfuscated. I verified that the aspect class and all methods and fields were kept in the mapping.
Here's the final version of my proguard config that didn't get the aspects working, although everything compiles fine and there are no errors:
...some config, the injar included my iajc compiled library project with aspects
-optimizations !code/allocation/variable,!code/simplification/arithmetic,!field/*,!class/merging/*
-optimizationpasses 3
-dontpreverify
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-verbose
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
# This is the package of my aspects
-keep class my.package.aspectj.** { *; }
# This is all the public methods that my aspect code calls in to
-keep class my.package.allthethingsmyaspectscall.** { *; }
...keep some other classes not related to this
With all this my aspects still didn't apply.
My solution was to copy my aspect files into the client app's project at install time so that it is in their app classpath and gets woven when their app gets built.

"Unknown Source" in release apk

Everything in my debug APK works just fine. However, when I export my APK and install it, everything works fine until I make a call to a referenced library.
E/AndroidRuntime(32571): at com.znood.znoodapp.ShowResultsActivity.a (Unknown Source)
I am using ProGuard.
My libraries are in the libs directory and are added to build path.
Any pointers are highly appreciated =)
The problem was with the Google Gson library. Proguard converts class names into obfuscated ones rendering json conversion buggy.
In order to solve this problem, make sure to have the following in your proguard-project.txt
# the classes that you use for Gson conversion
-keep class com.yourapp.objects.** { *; }
# without this line, I was having ClassCastException
-keepattributes Signature, *Annotation*
I hope this helps someone =)
If you haven't defined your libraries in proguard-project.txt then you can add like this
-libraryjars /libs/smack.jar
-libraryjars /libs/libphonenumber-5.0v1.5.jar
Android obfuscate app using proguard keeps obfuscating library jars - or is it?

Categories

Resources