Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
ezh edited this page May 25, 2012 · 3 revisions

PLASE REVIEW THIS ARTICLE

IMHO the best way will be to make this text like draft or standard

after that we may edit android-plugin source code and make it cover this text in proper way

Predex – ability to pregenerate dex as android shared libraries

What is it? For example, our application depends on Scala. And, for example, we don't want to waste our time on every build iteration. Dex stage on every build that prepare huge apk with big part of stable, immutable code is very ineffective. The simplest decision is to pre generate dex files for all stable dependencies and upload them to our real device or emulator. http://lampwww.epfl.ch/~michelou/android/emulator-android-sdk.html explains how to prepare and upload libraries to Android. But our real application may depends on different libraries, not only Scala. It would be nice to preload all of them and thus reduce build time of our project. Predex is solution.

At least two ways to preload libraries on Android

Preload via BOOTCLASS

For example

and so on...

This method is complex and have some drawbacks. You must modify runtime environment (emulator img file) every time when library list modified. Also all libraries loaded at the boot time so some broken stuff may blow up boot sequence of our device. This way mostly suitable for real people that acquire pleasure from solving different puzzles, especially with boot sequence.

Preload as shared library.

There is not any example because this method very simple. Google provide it for loading shared libraries and using it widely. You may read more about Android share libraries in “beautiful and detailed” Android documentation, or source code... Just modify emulator image once and make /system/etc/permissions writable or use real device. After that upload XML library descriptors to /system/etc/permissions, pregenerated dex libraries to /data and reboot device. That's all. Next time we apply our changes without any environment modification - only overwrite exist library or upload new. At the end of modification process (adb push) we reboot our device. The whole process can be completed in a few seconds. Reboot will take longer ;-) Permission descriptors generated for every predexed library by plugin automatically. We must add reference for every shared library to development version of our AndroidManifest.xml like this:

Setup project

dxOpt the only predex setting, keep it simple.

dxOpts in Android := ("-JXmx512m", None)
dxOpts in Android := ("-JXmx512m", Some(Seq()))
dxOpts in Android := ("-JXmx512m", Some(Seq(".*")))
dxOpts in Android := ("-JXmx512m", Some(Seq(".*/digilib.*")))

Proguard turned off

dxOpt set to None

Predex stage is disabled. APK file generated without Proguard

dxOpt set to Some(Seq())

Predex stage is enables. APK file generated without Proguard. APK file based on “classes.dex” that contain only application classes from sbt 'android:class-directory'. All dependencies processes as external dex libraries and located at sbt 'android:classes-dex-path'/predex directory.

dxOpt set to Some(Seq(“regexp”, ...))

Predex stage is enables. APK file generated without Proguard. APK file based on “classes.dex” that contain application classes from sbt 'android:class-directory' directory AND dependencies that match out regular expression. All OTHER dependencies processes as external dex libraries and located at sbt 'android:classes-dex-path'/predex directory.

Proguard turned on

Predex stage is disabled while proguard is turned on. APK file generated with Proguard.

Build project

1st time build with sbt

  1. Upload pregenerated dex files located at sbt 'android:classes-dex-path'/predex directory to /data, if needed
  2. Upload created xml security handler template to /system/etc/permissions, if needed
  3. Reboot device if needed (predex libraries modified)
  4. Next we run APK to our real device or emulator

Make sure, that that XML file point to proper library location (/data by default). By default all libraries located in /data directory. There are several reasons for such location: /data always accessible, /data located on EFS partition with POSIX file system object permission model, /data available early at boot process, and the last one is in normal environment /data directory contains no files, only directories. Now, Android environment ready for our stripped APK package.

2nd time build with sbt

Predex libraries are already cached.

  1. All steps skipped except stripped APK generation.

Our build time reduced. Problem solved