Skip to content

Commit

Permalink
Allow for optimizations via ProGuard.
Browse files Browse the repository at this point in the history
- Optionally optimize application code.

- Upgrade to ProGuard 4.6.
  • Loading branch information
Martin Kneissl committed Oct 11, 2011
1 parent 5abe8be commit 999a15c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,34 @@ is issued by the plugin when the same ID is used for different types
of a resources; the type of resources retrieved by that ID will be
unpredictable.

## ProGuard optimizations

The plugin allows to run ProGuard optimizations on your classes. This
might improve the performance of your application dramatically, but also
break it easily. By default, the application is only shrinked by ProGuard,
but not optimized.

If you want to apply optimizations, you specify ProGuard
optimization options in your project like this:

```scala
lazy val someOptimizedProject = Project(
id = ...,
...
settings = ... ++ inConfig(Android)(
proguardOptimizations := Seq(
"-optimizationpasses 8",
"-dontpreverify",
"-allowaccessmodification",
"-optimizations !code/simplification/arithmetic"
)
)
)
```

Please note that you will receive even more warnings from ProGuard
and dex when you apply optimizations.

## Getting screenshots

In the sbt console run:
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")

libraryDependencies ++= Seq(
"com.google.android.tools" % "ddmlib" % "r10",
"net.sf.proguard" % "proguard" % "4.4"
"net.sf.proguard" % "proguard-base" % "4.6"
)

sbtPlugin := true
1 change: 1 addition & 0 deletions src/main/scala/AndroidBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ object AndroidBase {
resourcesApkPath <<= (target, resourcesApkName) (_ / _),
packageApkPath <<= (target, packageApkName) (_ / _),
useProguard := true,
proguardOptimizations := Seq.empty,

addonsJarPath <<= (manifestTemplatePath, manifestSchema, mapsJarPath) {
(mPath, man, mapsPath) =>
Expand Down
20 changes: 12 additions & 8 deletions src/main/scala/AndroidInstall.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ object AndroidInstall {

private def dxTask: Project.Initialize[Task[File]] =
(scalaInstance, dxJavaOpts, dxPath, classDirectory,
proguardInJars, proguard, classesDexPath, streams) map {
proguardInJars, proguard, proguardOptimizations, classesDexPath, streams) map {
(scalaInstance, dxJavaOpts, dxPath, classDirectory,
proguardInJars, proguard, classesDexPath, streams) =>
proguardInJars, proguard, proguardOptimizations, classesDexPath, streams) =>

val inputs = proguard match {
case Some(file) => file get
Expand All @@ -44,7 +44,8 @@ object AndroidInstall {
!inputs.exists (_.lastModified > classesDexPath.lastModified)

if (!uptodate) {
val dxCmd = String.format("%s %s --dex --output=%s %s",
val noLocals = if (proguardOptimizations.isEmpty) "" else "--no-locals"
val dxCmd = String.format("%s %s --dex " + noLocals + " --output=%s %s",
dxPath, dxMemoryParameter(dxJavaOpts), classesDexPath, inputs.mkString(" "))
streams.log.debug(dxCmd)
streams.log.info("Dexing "+classesDexPath)
Expand All @@ -55,22 +56,25 @@ object AndroidInstall {
}

private def proguardTask: Project.Initialize[Task[Option[File]]] =
(useProguard, classDirectory, proguardInJars, streams,
(useProguard, proguardOptimizations, classDirectory, proguardInJars, streams,
classesMinJarPath, libraryJarPath, manifestPackage, proguardOption) map {
(useProguard, classDirectory, proguardInJars, streams,
(useProguard, proguardOptimizations, classDirectory, proguardInJars, streams,
classesMinJarPath, libraryJarPath, manifestPackage, proguardOption) =>
useProguard match {
case true =>
val optimizationOptions = if (proguardOptimizations.isEmpty) Seq("-dontoptimize") else proguardOptimizations
val manifestr = List("!META-INF/MANIFEST.MF", "R.class", "R$*.class",
"TR.class", "TR$.class", "library.properties")
val sep = JFile.pathSeparator
val args =
val args = (
"-injars" :: classDirectory.absolutePath + sep +
(if (!proguardInJars.isEmpty)
proguardInJars.map(_+manifestr.mkString("(", ",!**/", ")")).mkString(sep) else "") ::
"-outjars" :: classesMinJarPath.absolutePath ::
"-libraryjars" :: libraryJarPath.mkString(sep) ::
"-dontwarn" :: "-dontoptimize" :: "-dontobfuscate" ::
Nil) ++
optimizationOptions ++ (
"-dontwarn" :: "-dontobfuscate" ::
"-dontnote scala.Enumeration" ::
"-dontnote org.xml.sax.EntityResolver" ::
"-keep public class * extends android.app.Activity" ::
Expand All @@ -82,7 +86,7 @@ object AndroidInstall {
"-keep public class * extends android.app.Application" ::
"-keep public class "+manifestPackage+".** { public protected *; }" ::
"-keep public class * implements junit.framework.Test { public void test*(); }" ::
proguardOption :: Nil
proguardOption :: Nil )
val config = new ProGuardConfiguration
new ConfigurationParser(args.toArray[String]).parse(config)
streams.log.debug("executing proguard: "+args.mkString("\n"))
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/AndroidKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object AndroidKeys {

/** Proguard Settings */
val proguardOption = SettingKey[String]("proguard-option")
val proguardOptimizations = SettingKey[Seq[String]]("proguard-optimizations")
val libraryJarPath = SettingKey[Seq[File]]("library-path")

/** Default Settings */
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/PlainJavaProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import AndroidKeys._
object PlainJavaProject {
lazy val settings: Seq[Setting[_]] = inConfig(Android) (Seq(
useProguard := false,
proguardOptimizations := Seq.empty,
manifestPath <<= (baseDirectory, manifestName) (_ / _),
mainResPath <<= (baseDirectory, resDirectoryName) (_ / _),
javaSource in Compile <<= (baseDirectory) (_ / "src")
Expand Down

0 comments on commit 999a15c

Please sign in to comment.