Proguard - how to keep all files a JAR uses? - android

I have manually added a JAR to my project. It uses lots of classes with different prefixes and I can't search and add all of them to my proguard file.
Is there a way to keep all the file the jar uses? The jar is called nano-0.7.0.jar

Do you have a package name in your jar file? If you have, try -keep class yourpackageName.**{*;} to tell proguard system keep all the classes in your package, and then add -dontwarn yourpackageName.** to tell proguard system do not warning if the system can't find the class referenced by the class in your package.

Related

Android library proguard package obfuscation produces a.a.a.a.a collision

I'm writing 2 Android libraries. When I obfuscate both, the obfuscated code in both of them contains a class named a.a.a.a.a which causes the following error when trying to use both libraries in the same application:
Duplicate class a.a.a.a.a found in modules classes.jar (lib1) and classes.jar (lib2)
How can I prevent Proguard from obfuscating the first 3 packages to end up with:
my.domain.lib1.a.a and my.domain.lib2.a.a?
Edit: The obfuscation is happening as part of building the libraries, not while building the application.
This can be resolved by putting -repackageclasses my.domain.lib#.ofs in the proguard-rules file of each library while replaceing # with 1 and 2 respectivly. This will move all the obfuscated classes into the my.domain.lib#.ofs package while all the non-obfuscated classes will remain in their original packages and you're guaranteed to have no collisions.
As the Proguard documentation states:
-repackageclasses [package_name]
Specifies to repackage all class files that are renamed, by moving them into the single given package.
Another solution is to use -keeppackagenames. Unfortunately, I couldn't find a way to make it keep only the first 3 packages.
See the Proguard documentation:
-keeppackagenames [package_filter]
Specifies not to obfuscate the given package names.
Add the code below to the proguard-rules.pro file.
-keeppackagenames

For Android,will the unused class files in jar be (or not be) included in .apk?

For Android,will the unused class files in jar be (or not be) included in .apk?
For example, there is a library in the form of the jar file. I may only use some of the files. I am just wondering what the .apk will include.
Thanks!
Basically, the unused files will be still included in apk.
But if you enabled ProGuard, they will be removed.
The APK will include unused class files.
Expanding on #Ivan's answer: You can configure ProGuard to keep certain classes it would otherwise remove (for example, because they are only referenced by reflection) by adding lines like this to proguard-project.txt:
# single class
-keep class com.example.reflected.MyClass
# whole package
-keep class com.example.reflected.*

is it possible to obfuscate part of classes/packages in apk?

I have developed one android application which is having set of activities,background service and some other utility packages(which are written on pure java), i don't want to obfuscate activities and background service but need to obfuscate utility packages(pure java code), is it possible to do so?if yes then how to enable it in my proguard.cfg file?
OR in more precise way:
Application is using some external jars which are added to project's lib folder, I want only these jars should get obfuscated and rest of application code(activities , service etc...) should not get obfuscated. How to achieve the same using eclipse and proguard.cfg?
Regards,
Piks
If proguard is activated then Android will automatically include the used library files when creating a signed APK in Eclipse, so you don't have to specify this.
If the package name of your application is different from the package names of your libraries then you can simply use a wildcard and add this to your proguard.cfg file
-keepnames class com.mypackagename.** {
*;
}
If the packagename is the same, then you'll have to specify which classes should be obfuscated by hand, e.g.
-keepnames class com.mypackagename.MyApp {
*;
}
-keepnames class com.mypackagename.subpackage.MyClass {
*;
}

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

How can I exclude external .jar from obfuscation by Proguard (Android project)?

When I export android project with proguard.cfg, all referenced .jar files are obfuscated as well. How can I exclude some of that .jars from obfuscation?
If you don't want to edit the Ant script, you can add -keep options to proguard.cfg for the classes in those external jars. For instance:
-keep class othercode.** { *; }
Or with a regular expression containing a negator:
-keep class !mycode.** { *; }
The standard Ant script will still merge all referenced jars in the single output jar though.
In your config file, set up your jars as library jars instead of input jars. This leaves them untouched.
-libjars <path/to/jars>
Using proguard maven plugin I do it like that
<inclusion>
<groupId>foo.bar</groupId>
<artifactId>foo-bar</artifactId>
<library>true</library>
<filter>!META-INF/**</filter>
</inclusion>
The
<library>true</library>
lead to the external jar merged into the final jar after the obfuscation. But this might lead to the Manifest being overwritten. I haven't figured out yet how to avoid that the best way.

Categories

Resources