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 113
/
Copy pathAndroidInstall.scala
297 lines (261 loc) · 11.9 KB
/
AndroidInstall.scala
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.util.Properties
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, manifestPackage, proguardOption) map {
(useProguard, proguardOptimizations, classDirectory, proguardInJars, streams,
classesMinJarPath, libraryJarPath, manifestPackage, 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 ++ (
"-dontwarn" :: "-dontobfuscate" ::
"-dontnote scala.Enumeration" ::
"-dontnote org.xml.sax.EntityResolver" ::
"-keep public class * extends android.app.Activity" ::
"-keep public class * extends android.app.Service" ::
"-keep public class * extends android.app.backup.BackupAgent" ::
"-keep public class * extends android.appwidget.AppWidgetProvider" ::
"-keep public class * extends android.content.BroadcastReceiver" ::
"-keep public class * extends android.content.ContentProvider" ::
"-keep public class * extends android.view.View" ::
"-keep public class * extends android.app.Application" ::
"-keep public class "+manifestPackage+".** { public protected *; }" ::
"-keep public class * implements junit.framework.Test { public void test*(); }" ::
"""
-keepclassmembers class * implements java.io.Serializable {
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
""" ::
proguardOption :: Nil )
val config = new ProGuardConfiguration
new ConfigurationParser(args.toArray[String], new Properties).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, fullClasspath, preserveServiceRegistry, serviceRegistryInclude, serviceRegistryExclude, streams) map { (c, cp, p, in, ex, s) =>
val builder = new ApkBuilder(c, debug)
builder.build.fold(sys.error(_), s.log.info(_))
s.log.debug(builder.outputStream.toString)
if (p) {
val services = extractServiceRegistry(cp.files, in, ex)
insertServiceRegistry(c.packageApkPath, services)
}
c.packageApkPath
}
private def extractServiceRegistry(
cp: Seq[JFile], in: Seq[String], ex: Seq[String]
): Map[String, List[String]] = {
import java.util.jar.JarFile
import scala.io.Source
import scala.collection.JavaConversions._
var ret = Map[String, List[String]]()
cp.filter(_.toString.matches(".*\\.jar")).foreach {f => {
val jarfile = new JarFile(f)
try {
for (entry <- jarfile.entries()) {
val name = entry.getName()
if (name.matches("META-INF/services/..*")) {
val is = jarfile.getInputStream(entry)
try {
val lns = Source.fromInputStream(is).getLines().
filter(cls => in.exists(r => cls.matches(r))).
filter(cls => ex.forall(r => !cls.matches(r)))
ret += name -> (ret.getOrElse(name, List()) ++ lns)
}
finally {
is.close()
}
}
}
}
finally {
jarfile.close()
}
}}
ret
}
private def insertServiceRegistry(apk: File,
services: Map[String, List[String]]) = {
import java.util.zip.ZipOutputStream
import java.util.zip.ZipInputStream
import java.util.zip.ZipEntry
import java.io.FileOutputStream
import java.io.FileInputStream
import java.io.File
val tmp = new File(apk.getAbsolutePath + ".tmp")
tmp.delete()
if (!apk.renameTo(tmp)) {
throw new RuntimeException("could not rename file " + apk)
}
val buf = Array.ofDim[Byte](1024)
val in = new ZipInputStream(new FileInputStream(tmp))
val out = new ZipOutputStream(new FileOutputStream(apk))
try {
var entry = in.getNextEntry()
while (entry != null) {
out.putNextEntry(new ZipEntry(entry.getName))
var len = in.read(buf)
while (len > 0) {
out.write(buf, 0, len)
len = in.read(buf)
}
entry = in.getNextEntry()
}
for ((name, value) <- services) {
if (value.size > 0) {
out.putNextEntry(new ZipEntry(name))
out.write(value.mkString("\n").getBytes)
}
}
}
finally {
tmp.delete()
in.close()
out.close()
}
}
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)
})
}