forked from martoreto/aauto-sdk
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.gradle
331 lines (292 loc) · 11.5 KB
/
build.gradle
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
plugins {
id "de.undercouch.download" version "3.2.0"
}
def downloadUrl = 'https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=289495'
// NOTE WHEN UPGRADING:
// - update Play Services version in maven dependencies below
def apkFile = new File(buildDir, 'in.apk')
def apkDir = new File(buildDir, 'apk')
def outDir = new File(buildDir, 'out')
def smaliDir = new File(outDir, 'smali')
// Detect Android SDK location using Android Studio-generated
// local.properties in the root project.
def rootSdkDir
def rootLocalProperties = new File(rootProject.projectDir, 'local.properties')
if (rootLocalProperties.exists()) {
Properties properties = new Properties()
rootLocalProperties.withInputStream { instr ->
properties.load(instr)
}
rootSdkDir = properties.getProperty('sdk.dir')
}
def sdkDir = new File(rootSdkDir ?: System.getProperty('sdk.dir') ?: System.getenv("ANDROID_SDK")
?: System.getenv("SDK_ROOT") ?: System.getenv("ANDROID_HOME"))
import de.undercouch.gradle.tasks.download.Download
import java.util.regex.Pattern
apply plugin: "maven-publish"
task downloadApk(type: Download) {
src downloadUrl
dest apkFile
onlyIfNewer true
}
// This is a Gradle incremental task which means that it won't
// be re-executed if the inputFile did not change.
class JavaExecExtract extends JavaExec {
@Input
File inputFile
@OutputDirectory
File outputDir
}
task extractApk(dependsOn: downloadApk, type: JavaExecExtract) {
classpath = files('libs/apktool_2.3.0.jar')
main = 'brut.apktool.Main'
args 'decode', '-o', apkDir, '-f', apkFile
inputFile = apkFile
outputDir = apkDir
}
task collectResources(dependsOn: extractApk) {
doLast {
def KEEP_EXTRA_RES = [
'@style/Base.CardView',
]
// Extract symbols
def symbols = [:]
fileTree(dir: apkDir)
.include('smali*/com/google/android/apps/auto/sdk/ui/R$*.smali')
.each { File file ->
def filesyms = [:];
file.eachLine { String line ->
if (!line.startsWith('.field public static final '))
return
def tokens = line.split()
def namesig = tokens[4].split(':')
if (namesig[1] != 'I')
return
filesyms[namesig[0]] = Integer.decode(tokens[6])
}
def basename = file.name.take(file.name.lastIndexOf('.')).split('\\$')[1]
symbols[basename] = filesyms
}
println symbols
// Process value resources
fileTree(dir: apkDir)
.include('res/values*/*')
.exclude('res/values/public.xml')
.each { File file ->
def relpath = file.absolutePath - apkDir.absolutePath
println relpath
def xml = new XmlParser().parse(file)
assert xml.name() == 'resources'
println xml.children().size()
def toremove = []
xml.each { child ->
def category = child.name()
def name = child.@name
if (category == 'item' || category == 'public')
category = child.@type
if (!(symbols.get(category) ?: [:]).containsKey(name)
&& !KEEP_EXTRA_RES.contains('@' + category + '/' + name)) {
toremove.add(child)
}
}
toremove.each { child ->
xml.remove(child)
}
println xml.children().size()
if (xml.children().size() > 0) {
def outfile = new File(outDir, relpath)
outfile.getParentFile().mkdirs()
new XmlNodePrinter(new PrintWriter(new FileWriter(outfile))).print(xml)
}
}
// Process remaining resources
fileTree(dir: apkDir)
.include('res/*/*')
.exclude('res/values*/*')
.each { File file ->
def category = file.getParentFile().getName().split('-')[0]
def name = file.name.take(file.name.lastIndexOf('.'))
if (!(symbols.get(category) ?: [:]).containsKey(name))
return
def relpath = file.absolutePath - apkDir.absolutePath
println relpath
def outfile = new File(outDir, relpath)
outfile.getParentFile().mkdirs()
outfile.setBytes(file.getBytes())
}
// Fixup attrs
def attrsFile = new File(outDir, 'res/values/attrs.xml')
def xml = new XmlParser().parse(attrsFile)
def attrsDone = [] as Set
symbols.styleable.each { k, v ->
def s = k.split('_')[0]
if (attrsDone.contains(s))
return
attrsDone.add(s)
def items = symbols.styleable.findAll { it.key.startsWith(s + '_') }.collect { it.key }
items.sort { symbols.styleable[it] }
def decl = xml.appendNode('declare-styleable', ['name': s])
items.each { item ->
def name = item.split('_', 2)[1].replace('_', ':')
def node = xml.attr.find { attr -> attr.@name == name }
if (node) {
xml.remove(node)
decl.append(node)
} else {
decl.appendNode('attr', ['name': name])
}
}
}
new XmlNodePrinter(new PrintWriter(new FileWriter(attrsFile))).print(xml)
// Fixup layouts
fileTree(dir: outDir)
.include('res/layout*/*')
.each { File file ->
def relpath = file.absolutePath - outDir.absolutePath
println relpath
def content = file.text.replaceAll('@id/', '@+id/')
file.text = content
}
// Write manifest
new File(outDir, 'AndroidManifest.xml').text =
"""<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.apps.auto.sdk.ui">
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="25" />
<application />
</manifest>"""
}
}
task aaptResources(dependsOn: collectResources, type: Exec) {
def aaptDir = new File(outDir, 'aapt')
doFirst {
aaptDir.mkdirs()
}
executable new File(sdkDir, 'build-tools/25.0.1/aapt')
args 'p',
'-M', new File(outDir, 'AndroidManifest.xml'),
'-S', new File(outDir, 'res'),
'-S', new File(projectDir, 'res-extra'),
'-I', new File(sdkDir, 'platforms/android-25/android.jar'),
'-J', aaptDir,
'--output-text-symbols', outDir,
'--auto-add-overlay'
}
task makePublicTxt(dependsOn: aaptResources, type: Copy) {
from new File(outDir, 'R.txt')
into outDir
rename { 'public.txt' }
}
task collectSmali(dependsOn: extractApk) {
doLast {
def PACKAGES_TO_EXTRACT = [
'com.google.android.apps.auto.sdk',
'com.google.android.gms.car',
'com.google.android.a',
'com.google.android.b',
'com.google.a',
'android.support.car',
]
smaliDir.mkdirs()
fileTree(dir: apkDir)
.include('smali*/**/*.smali')
.each { File file ->
def relpath = file.absolutePath - apkDir.absolutePath
relpath = relpath.split(Pattern.quote(File.separator), 3)[2]
def classname = relpath.split('\\.')[0].replaceAll(Pattern.quote(File.separator), '.')
if (classname.split('\\$')[0] == 'com.google.android.apps.auto.sdk.ui.R')
return
def found = false
PACKAGES_TO_EXTRACT.each { pkg ->
if (classname.startsWith(pkg + '.'))
found = true
}
if (!found)
return
println classname
def text = file.text
// HACK: This is not strictly needed, but it confuses Android
// Studio, which thinks that CarActivity is not a ContextManager.
text = text.replaceAll('L(com/google/android/./.);', 'L$1gizmo;')
def outfile = new File(smaliDir, relpath)
outfile.getParentFile().mkdirs()
outfile.text = text
}
fileTree(dir: smaliDir)
.include('com/google/android/?/?.smali')
.each { File file ->
file.renameTo new File(file.getParentFile(),
file.getName().split('\\.')[0] + 'gizmo.smali')
}
}
}
task buildDex(dependsOn: collectSmali, type: JavaExec) {
classpath = files('libs/smali-2.2.1.jar')
main = 'org.jf.smali.Main'
args 'a', '-o', new File(outDir, 'classes.dex'), smaliDir
}
task buildJar(dependsOn: buildDex, type: JavaExec) {
classpath = fileTree(dir: 'libs/dex2jar-2.0')
main = 'com.googlecode.dex2jar.tools.Dex2jarCmd'
args '-d', '-o', new File(outDir, 'classes.jar'), '--force',
new File(outDir, 'classes.dex')
}
task makeProguardTxt(type: Copy) {
from new File(projectDir, 'proguard.txt')
into outDir
}
task buildAar(dependsOn: [collectResources, makePublicTxt, buildJar,
makeProguardTxt], type: Zip) {
from outDir
include 'AndroidManifest.xml', 'R.txt', 'public.txt', 'proguard.txt',
'classes.jar', 'res/**'
archiveName 'aauto.aar'
destinationDir buildDir
}
task clean {
doLast {
delete buildDir
}
}
task build(dependsOn: buildAar)
publishing {
publications {
maven(MavenPublication) {
groupId System.getenv('GROUP') ?: 'com.github.martoreto'
artifactId System.getenv('ARTIFACT') ?: 'aauto-sdk'
version System.getenv('VERSION') ?: 'master-SNAPSHOT'
artifact new File(buildDir, 'aauto.aar')
pom.withXml {
def deps = asNode().appendNode('dependencies')
def dep
// The final product support libraries version should be the
// same as its compileSdk, and we don't know what compileSdk
// the final app will have. Threfore I leave the oldest
// possible versions of support libraries here, and if the app
// uses the newer ones, Gradle will resolve it so that these
// newer versions will be used.
dep = deps.appendNode('dependency')
dep.appendNode('groupId', 'com.android.support')
dep.appendNode('artifactId', 'support-v4')
dep.appendNode('version', '21.0.3')
dep = deps.appendNode('dependency')
dep.appendNode('groupId', 'com.android.support')
dep.appendNode('artifactId', 'appcompat-v7')
dep.appendNode('version', '21.0.3')
dep = deps.appendNode('dependency')
dep.appendNode('groupId', 'com.android.support')
dep.appendNode('artifactId', 'recyclerview-v7')
dep.appendNode('version', '21.0.3')
// The Play Services version here should be the same as used in
// the source APK. I'm not sure what happens when the app uses
// the newer version, though.
dep = deps.appendNode('dependency')
dep.appendNode('groupId', 'com.google.android.gms')
dep.appendNode('artifactId', 'play-services-base')
dep.appendNode('version', '11.4.0')
}
}
}
}