Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

support apklib dependencies #112

Merged
merged 4 commits into from
Jan 31, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 84 additions & 5 deletions src/main/scala/AndroidBase.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sbt._

import scala.xml._

import Keys._
import AndroidKeys._
import AndroidHelpers._
Expand All @@ -8,16 +10,90 @@ import sbinary.DefaultProtocol.StringFormat

object AndroidBase {

private def apklibSourcesTask =
(extractApkLibDependencies, streams) map {
(projectLibs, s) => {
s.log.info("generating source files from apklibs")
val xs = for (
l <- projectLibs;
f <- l.sources
) yield f
s.log.info("generated " + xs.size + " source files from " + projectLibs.size + " apklibs")
xs
}
}

private def apklibDependenciesTask =
(update in Compile, sourceManaged, managedJavaPath, resourceManaged, streams) map {
(updateReport, srcManaged, javaManaged, resManaged, s) => {

val apklibs = updateReport.matching(artifactFilter(`type` = "apklib"))

apklibs map { apklib =>
s.log.info("extracting apklib " + apklib.name)
val dest = srcManaged / ".." / apklib.base

val unzipped = IO.unzip(apklib, dest)
def moveContents(fromDir: File, toDir: File) = {
toDir.mkdirs()
val pairs = for (
file <- unzipped;
rel <- IO.relativize(fromDir, file)
) yield (file, toDir / rel)
IO.move(pairs)
pairs map { case (_,t) => t }
}
val sources = moveContents(dest / "src", javaManaged)

val manifest = dest / "AndroidManifest.xml"
val pkgName = XML.loadFile(manifest).attribute("package").get.head.text
LibraryProject(
pkgName,
manifest,
sources,
Some(dest / "res") filter { _.exists },
Some(dest / "assets") filter { _.exists }
)
}
}
}

private def aaptGenerateTask =
(manifestPackage, aaptPath, manifestPath, mainResPath, jarPath, managedJavaPath) map {
(mPackage, aPath, mPath, resPath, jPath, javaPath) =>
(manifestPackage, aaptPath, manifestPath, mainResPath, jarPath, managedJavaPath, extractApkLibDependencies, streams) map {
(mPackage, aPath, mPath, resPath, jPath, javaPath, apklibs, s) =>

val libraryResPathArgs = for (
lib <- apklibs;
d <- lib.resDir.toSeq;
arg <- Seq("-S", d.absolutePath)
) yield arg

val libraryAssetPathArgs = for (
lib <- apklibs;
d <- lib.assetsDir.toSeq;
arg <- Seq("-A", d.absolutePath)
) yield arg

Seq(aPath.absolutePath, "package", "--auto-add-overlay", "-m",
"--custom-package", mPackage,
"--custom-package", mPackage,
"-M", mPath.head.absolutePath,
"-S", resPath.absolutePath,
"-I", jPath.absolutePath,
"-J", javaPath.absolutePath) ++
libraryResPathArgs ++
libraryAssetPathArgs !

apklibs.foreach { (lib) =>
Seq(aPath.absolutePath, "package", "--auto-add-overlay", "-m",
"--custom-package", lib.pkgName,
"-M", mPath.head.absolutePath,
"-S", resPath.absolutePath,
"-I", jPath.absolutePath,
"-J", javaPath.absolutePath) !
"-J", javaPath.absolutePath,
"--non-constant-id") ++
libraryResPathArgs ++
libraryAssetPathArgs !
}

javaPath ** "R.java" get
}
Expand Down Expand Up @@ -71,6 +147,8 @@ object AndroidBase {
managedJavaPath <<= (sourceManaged in Compile) (_ / "java"),
managedScalaPath <<= (sourceManaged in Compile) ( _ / "scala"),

extractApkLibDependencies <<= apklibDependenciesTask,

managedSourceDirectories in Compile <<= (managedJavaPath, managedScalaPath) (Seq(_, _)),

classesMinJarPath <<= (target, classesMinJarName) (_ / _),
Expand Down Expand Up @@ -99,13 +177,14 @@ object AndroidBase {

makeManagedJavaPath <<= directory(managedJavaPath),

apklibSources <<= apklibSourcesTask,
aaptGenerate <<= aaptGenerateTask,
aaptGenerate <<= aaptGenerate dependsOn makeManagedJavaPath,
aidlGenerate <<= aidlGenerateTask,

unmanagedJars in Compile <++= (libraryJarPath) map (_.map(Attributed.blank(_))),

sourceGenerators in Compile <+= (aaptGenerate, aidlGenerate) map (_ ++ _),
sourceGenerators in Compile <+= (apklibSources, aaptGenerate, aidlGenerate) map (_ ++ _ ++ _),

resourceDirectories <+= (mainAssetsPath),

Expand Down
14 changes: 11 additions & 3 deletions src/main/scala/AndroidInstall.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ object AndroidInstall {
}

private def aaptPackageTask: Project.Initialize[Task[File]] =
(aaptPath, manifestPath, mainResPath, mainAssetsPath, jarPath, resourcesApkPath, streams) map {
(apPath, manPath, rPath, assetPath, jPath, resApkPath, s) =>
(aaptPath, manifestPath, mainResPath, mainAssetsPath, jarPath, resourcesApkPath, extractApkLibDependencies, streams) map {
(apPath, manPath, rPath, assetPath, jPath, resApkPath, apklibs, s) =>

val libraryResPathArgs = for (
lib <- apklibs;
d <- lib.resDir.toSeq;
arg <- Seq("-S", d.absolutePath)
) yield arg

val aapt = Seq(apPath.absolutePath, "package", "--auto-add-overlay", "-f",
"-M", manPath.head.absolutePath,
"-S", rPath.absolutePath,
"-A", assetPath.absolutePath,
"-I", jPath.absolutePath,
"-F", resApkPath.absolutePath)
"-F", resApkPath.absolutePath) ++
libraryResPathArgs
s.log.debug("packaging: "+aapt.mkString(" "))
if (aapt.run(false).exitValue != 0) sys.error("error packaging resources")
resApkPath
Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/AndroidKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ object AndroidKeys {
val resourcesApkPath = SettingKey[File]("resources-apk-path")
val packageApkPath = SettingKey[File]("package-apk-path")
val useProguard = SettingKey[Boolean]("use-proguard")
val projectLibraryDependencies = SettingKey[Seq[ModuleID]]("project-library-dependencies", "Declares managed apklib dependencies.")

/** Install Settings */
val packageConfig = TaskKey[ApkConfig]("package-config",
Expand All @@ -92,6 +93,11 @@ object AndroidKeys {
val manifestTemplatePath = SettingKey[File]("manifest-template-path")

/** Base Tasks */
case class LibraryProject(pkgName: String, manifest: File, sources: Set[File], resDir: Option[File], assetsDir: Option[File])

val extractApkLibDependencies = TaskKey[Seq[LibraryProject]]("apklib-dependencies", "Unpack apklib dependencies")

val apklibSources = TaskKey[Seq[File]]("apklib-sources", "Enumerate Java sources from apklibs")
val aaptGenerate = TaskKey[Seq[File]]("aapt-generate", "Generate R.java")
val aidlGenerate = TaskKey[Seq[File]]("aidl-generate",
"Generate Java classes from .aidl files.")
Expand Down