This repository was archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathAndroidInstall.scala
More file actions
185 lines (161 loc) · 8.02 KB
/
AndroidInstall.scala
File metadata and controls
185 lines (161 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import proguard.{Configuration=>ProGuardConfiguration, ProGuard, ConfigurationParser}
import sbt._
import Keys._
import AndroidKeys._
import AndroidHelpers._
import java.io.{File => JFile}
object AndroidInstall {
private def installTask(emulator: Boolean) = (dbPath, packageApkPath, streams) map { (dp, p, s) =>
adbTask(dp.absolutePath, emulator, s, "install", "-r ", p.absolutePath)
}
private def uninstallTask(emulator: Boolean) = (dbPath, manifestPackage, streams) map { (dp, m, s) =>
adbTask(dp.absolutePath, emulator, s, "uninstall", m)
}
private def aaptPackageTask: Project.Initialize[Task[File]] =
(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) ++
libraryResPathArgs
s.log.debug("packaging: "+aapt.mkString(" "))
if (aapt.run(false).exitValue != 0) sys.error("error packaging resources")
resApkPath
}
private def dxTask: Project.Initialize[Task[File]] =
(dxPath, dxInputs, dxOpts, proguardOptimizations, classDirectory, classesDexPath, scalaInstance, streams) map {
(dxPath, dxInputs, dxOpts, proguardOptimizations, classDirectory, classesDexPath, scalaInstance, streams) =>
def dexing(inputs: Seq[JFile], output: JFile) {
val uptodate = output.exists && inputs.forall(input =>
input.isDirectory match {
case true =>
(input ** "*").get.forall(_.lastModified <= output.lastModified)
case false =>
input.lastModified <= output.lastModified
}
)
if (!uptodate) {
val noLocals = if (proguardOptimizations.isEmpty) "" else "--no-locals"
val dxCmd = (Seq(dxPath.absolutePath,
dxMemoryParameter(dxOpts._1),
"--dex", noLocals,
"--num-threads="+java.lang.Runtime.getRuntime.availableProcessors,
"--output="+output.getAbsolutePath) ++
inputs.map(_.absolutePath)).filter(_.length > 0)
streams.log.debug(dxCmd.mkString(" "))
streams.log.info("Dexing "+output.getAbsolutePath)
streams.log.debug(dxCmd !!)
} else streams.log.debug("dex file " + output.getAbsolutePath + " uptodate, skipping")
}
// Option[Seq[String]]
// - None standard dexing for prodaction stage
// - Some(Seq(predex_library_regexp)) predex only changed libraries for development stage
dxOpts._2 match {
case None =>
dexing(dxInputs.get, classesDexPath)
case Some(predex) =>
val (dexFiles, predexFiles) = predex match {
case exceptSeq: Seq[_] if exceptSeq.nonEmpty =>
val (filtered, orig) = dxInputs.get.partition(file =>
exceptSeq.exists(filter => {
streams.log.debug("apply filter \"" + filter + "\" to \"" + file.getAbsolutePath + "\"")
file.getAbsolutePath.matches(filter)
}))
// dex only classes directory ++ filtered, predex all other
((classDirectory --- scalaInstance.libraryJar).get ++ filtered, orig)
case _ =>
// dex only classes directory, predex all other
((classDirectory --- scalaInstance.libraryJar).get, (dxInputs --- classDirectory).get)
}
dexFiles.foreach(s => streams.log.debug("pack in dex \"" + s.getName + "\""))
predexFiles.foreach(s => streams.log.debug("pack in predex \"" + s.getName + "\""))
// dex
dexing(dexFiles, classesDexPath)
// predex
predexFiles.get.foreach(f => {
val predexPath = new JFile(classesDexPath.getParent, "predex")
if (!predexPath.exists)
predexPath.mkdir
val output = new File(predexPath, f.getName)
val outputPermissionDescriptor = new File(predexPath, f.getName.replaceFirst(".jar$", ".xml"))
dexing(Seq(f), output)
val permission = <permissions><library name={ f.getName.replaceFirst(".jar$", "") } file={ "/data/" + f.getName } /></permissions>
val p = new java.io.PrintWriter(outputPermissionDescriptor)
try { p.println(permission) } finally { p.close() }
})
}
classesDexPath
}
private def proguardTask: Project.Initialize[Task[Option[File]]] =
(useProguard, proguardOptimizations, classDirectory, proguardInJars, streams,
classesMinJarPath, libraryJarPath, proguardOption) map {
(useProguard, proguardOptimizations, classDirectory, proguardInJars, streams,
classesMinJarPath, libraryJarPath, proguardOption) =>
if (useProguard) {
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 inJars = ("\"" + classDirectory.absolutePath + "\"") +:
proguardInJars.map("\""+_+"\""+manifestr.mkString("(", ",!**/", ")"))
val args = (
"-injars" :: inJars.mkString(sep) ::
"-outjars" :: "\""+classesMinJarPath.absolutePath+"\"" ::
"-libraryjars" :: libraryJarPath.map("\""+_+"\"").mkString(sep) ::
Nil) ++ optimizationOptions ++ proguardOption
streams.log.debug("executing proguard: " + (for (i <- 0 until args.size) yield {"arg" + (i + 1) + ": " + args(i)}).mkString("\n"))
val config = new ProGuardConfiguration
new ConfigurationParser(args.toArray[String]).parse(config)
streams.log.debug("executing proguard: "+args.mkString("\n"))
new ProGuard(config).execute
Some(classesMinJarPath)
} else {
streams.log.info("Skipping Proguard")
None
}
}
private def packageTask(debug: Boolean):Project.Initialize[Task[File]] = (packageConfig, streams) map { (c, s) =>
val builder = new ApkBuilder(c, debug)
builder.build.fold(sys.error(_), s.log.info(_))
s.log.debug(builder.outputStream.toString)
c.packageApkPath
}
lazy val installerTasks = Seq (
installEmulator <<= installTask(emulator = true) dependsOn packageDebug,
installDevice <<= installTask(emulator = false) dependsOn packageDebug
)
lazy val settings: Seq[Setting[_]] = inConfig(Android) (installerTasks ++ Seq (
uninstallEmulator <<= uninstallTask(emulator = true),
uninstallDevice <<= uninstallTask(emulator = false),
makeAssetPath <<= directory(mainAssetsPath),
aaptPackage <<= aaptPackageTask,
aaptPackage <<= aaptPackage dependsOn (makeAssetPath, dx),
dx <<= dxTask,
dxInputs <<= (proguard, proguardInJars, scalaInstance, classDirectory) map {
(proguard, proguardInJars, scalaInstance, classDirectory) =>
proguard match {
case Some(file) => Seq(file)
case None => (classDirectory +++ proguardInJars --- scalaInstance.libraryJar) get
}
},
cleanApk <<= (packageApkPath) map (IO.delete(_)),
proguard <<= proguardTask,
proguard <<= proguard dependsOn (compile in Compile),
packageConfig <<=
(toolsPath, packageApkPath, resourcesApkPath, classesDexPath,
nativeLibrariesPath, managedNativePath, dxInputs, resourceDirectory) map
(ApkConfig(_, _, _, _, _, _, _, _)),
packageDebug <<= packageTask(true),
packageRelease <<= packageTask(false)
) ++ Seq(packageDebug, packageRelease).map {
t => t <<= t dependsOn (cleanApk, aaptPackage, copyNativeLibraries)
})
}