Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Scala and Proguard

After running headfirst into as many problems as there are permutations of the set of Android command-line tools, I finally managed to compile a mix of Scala and Java source code into a usable apk.

As many suggest, I used proguard to squeeze the Scala library through the dex tool. The problem is this:

BUILD SUCCESSFUL
Total time: 1 minute 29 seconds

One minute and a half. We're talking about an application with Hello-World complexity here. I don't think I can develop like that. I'm gonna need to take meditation classes.

This is the proguard configuration:

-injars ${out.absolute.dir}/classes:${scala-library.jar}(!META-INF/MANIFEST.MF,!library.properties)
-outjars ${out.absolute.dir}/classes.min.jar
-libraryjars ${android.jar}
-dontwarn
-dontoptimize
-dontobfuscate
-keep public class * extends android.app.Activity

Is there a way to speed up the proguard step?

Edit: I'm running this in a pretty decent dual-core, 3GB ram machine, on top of 64-bit Linux. A run of ant compile (scalac/javac) takes 3 seconds. A full ant install takes 1:30, as described above. It's the proguard step that "freezes", according to the output, most likely because of the scala/android runtime lib sizes.

like image 944
slezica Avatar asked Oct 09 '11 14:10

slezica


People also ask

Should I use R8 or ProGuard?

R8 is having a faster processing time than Proguard which reduces build time. R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4.

What is ProGuard used for in Android?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

Should I use ProGuard?

It is quite easy to reverse engineer Android applications, so if you want to prevent this from happening, yes, you should use ProGuard for its main function: obfuscation. ProGuard has also two other important functions: shrinking which eliminates unused code and is obviously highly useful and also optimization.


3 Answers

Working via android Ant builds is probably not the right way to go about this.

The current "best advice" is to use SBT with the

  • proguard (https://github.com/siasia/xsbt-proguard-plugin) and
  • android (https://github.com/jberkel/android-plugin)

plugins.

like image 162
Kevin Wright Avatar answered Sep 24 '22 12:09

Kevin Wright


ProGuard takes a lot longer to shrink the Scala 2.9.1 library than the Scala 2.8.1 library (54 seconds vs. 13 seconds, for 8.5 MB vs. 6.2 MB). Either the structure of the library classes has changed fundamentally, or some new classes are causing excessive computations. I'll have to figure out if ProGuard or its configuration can be improved for this case. For now, you might be able to work with Scala 2.8.1.

I'm assuming your ProGuard configuration also contains the required options for Android and for Scala, as discussed in the ProGuard manual. If you are using the regular Android build process, the input (classes, libs) and libraries (android.jar) are already specified for you in the Ant build file, and you don't need to specify them again in the ProGuard configuration file. Reading them twice will just take time and generate many warnings (which you have switched off completely -- it's safer to switch them off selectively).

like image 35
Eric Lafortune Avatar answered Sep 28 '22 12:09

Eric Lafortune


If you have a rooted phone and only want to test on this you can install the scala libs directly on your phone so you don't need the proguard step anymore:

https://github.com/jrudolph/scala-android-libs

like image 38
Fabian Avatar answered Sep 27 '22 12:09

Fabian