Android ProGuard obfuscation of library: keep class not working - android

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** {*;}

Related

ProGuard - Obfuscate and Shrink "app" module only?

I am trying to use ProGuard first time to obfuscate only "app" module of my project. But i either face problems generating apk or the generated apk simply crash the app.
I have already tried following ProGuard rule:
-keep class !com.example.mypackage.**{ *; }
But it generates following error:
Warning:library class android.content.res.XmlResourceParser extends or
implements program class org.xmlpull.v1.XmlPullParser
So i want to know if i can just obfuscate and shrink only single module without using any other functionality.
The rule itself will not create this warning. It looks like you included something as compile dependency that contains the XmlPullParser class which is already present in the Android SDK jar.
This warnings is usually harmless and can be ignored by adding -ignorewarnings to your ProGuard configuration, but I would check all your dependencies and investigate where this class is included from.

Including external libraries in Android Proguard

My app is able to run without any issues during testing etc. But when I export out apk compiled with ProGuard, there are issues like random crashing and some features not working as expected.
I not sure is it due to the external jar libraries I have included in the project which is not properly configured in Proguard.
I have included the following in the proguard-android.txt file. I have two libraries so I added these:
-keep class org.apache.commons.net.** { *; }
-keep class org.jsoup.** { *; }
Is it the correct way? Is there any other way?
To add libraries just add -libraryjars ../libs/<libname>
After that you may need to keep classes and interfaces based on the errors you receive

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