Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove unused classes with proguard for Android

History/Context I have a project[1] where size really matters - recently I moved stuff to a shared lib[2] and thought proguard will take care and remove the unused classes because I had a config that was drastically reducing the size but by using the lib i came over the magic 100kb mark so I investigated: classes which I do not use for sure are in the resulting dex file - and even with full name ( not shortened to single-char ) - e.g. I see the SquareView in the dex which I in no way use in the App.

Question Surprisingly I found in the proguard documentation the following:

The library jars themselves always remain unchanged. 

Can I somehow tell/trick proguard (in)to process them? I find this really strange especially because I expect more stuff to be removeable in the lib than in the App itself..

[1] https://github.com/ligi/FAST [2] https://github.com/ligi/AndroidHelper

like image 355
ligi Avatar asked Aug 26 '13 21:08

ligi


2 Answers

The Eclipse/Ant/Gradle build processes in the Android SDK automatically specify your code (from bin/classes) and its libraries (from libs) with the option -injars. This means that the complete application is compacted, optimized, and obfuscated (in release builds, assuming ProGuard is enabled).

The build processes only specify the Android runtime android.jar with the option -libraryjars. It is necessary to process the code, but it should not end up in the processed apk, since it is already present on the device.

So it should all work out automatically. You may still see entire libraries with their original names in processed apks, if your configuration proguard-project.txt contains lines like -keep class org.mylibrary.** { *; }. Such configuration is typically a conservative solution to account for reflection. With some research and experimentation, you can often refine the configuration and get better results. You can figure out why classes are being kept with the option -whyareyoukeeping.

like image 85
Eric Lafortune Avatar answered Sep 30 '22 20:09

Eric Lafortune


I believe you have to use -injars:

-injars class_path Specifies the input jars (or wars, ears, zips, or directories) of the application to be processed. The class files in these jars will be processed and written to the output jars. By default, any non-class files will be copied without changes. Please be aware of any temporary files (e.g. created by IDEs), especially if you are reading your input files straight from directories. The entries in the class path can be filtered, as explained in the filters section. For better readability, class path entries can be specified using multiple -injars options.

Source: http://proguard.sourceforge.net/index.html#manual/usage.html

like image 25
rfgamaral Avatar answered Sep 30 '22 22:09

rfgamaral