-
Notifications
You must be signed in to change notification settings - Fork 199
/
build.gradle
327 lines (303 loc) · 12.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
plugins {
id "com.android.application" version "3.4.1"
id "org.sonarqube" version "2.6"
id "jacoco"
}
repositories {
google()
jcenter()
}
ext {
android_support_test_version = "1.0.2"
espresso_version = "3.0.2"
hamcrest_version = "1.3"
junit_version = "4.12"
lightweight_stream_api_version = "1.2.1"
mockito_version = "2.24.0"
openpgp_api_version = "12.0"
room_version = "2.1.0-alpha03"
uiautomator_version = "2.2.0"
lifecycle_version = "2.0.0"
}
dependencies {
implementation group: "org.sufficientlysecure", name: "openpgp-api",
version: "$openpgp_api_version"
implementation group: "androidx.room", name: "room-runtime",
version: "$room_version"
annotationProcessor group: "androidx.room", name: "room-compiler",
version: "$room_version"
implementation group: "com.annimon", name: "stream",
version: "$lightweight_stream_api_version"
testImplementation group: "org.hamcrest", name: "hamcrest-core",
version: hamcrest_version
testImplementation group: "org.mockito", name: "mockito-core",
version: mockito_version
testImplementation group: "junit", name: "junit", version: junit_version
testImplementation group: "androidx.room", name: "room-testing",
version: "$room_version"
androidTestImplementation group: "com.android.support.test",
name: "runner", version: android_support_test_version
androidTestImplementation group: "com.android.support.test.espresso",
name: "espresso-core", version: espresso_version
androidTestImplementation group: "com.android.support.test",
name: "rules", version: android_support_test_version
androidTestImplementation(group: "org.mockito", name: "mockito-android",
version: mockito_version)
androidTestImplementation group: "androidx.test.uiautomator",
name: "uiautomator", version: uiautomator_version
implementation group: "androidx.lifecycle", name: "lifecycle-livedata",
version: lifecycle_version
}
int versionMajor = 0
int versionMinor = 3
int versionPath = 5
// translate from product flavour names to abi names
Map<String, String> abiFlavorMap = [
"arm": "armeabi-v7a",
"arm64": "arm64-v8a",
"x86":"x86",
"x86_64": "x86_64"
]
Map<String, String> flavorTargetMap = [
"arm": "armv7-linux-androideabi",
"arm64": "aarch64-linux-android",
"x86": "i686-linux-android",
"x86_64": "x86_64-linux-android"
]
Map<String, Integer> abiVersionCodes = [
"armeabi-v7a": 1,
"arm64-v8a": 2,
"x86": 3,
"x86_64": 4
]
sonarqube {
properties {
def jacocoReports = []
fileTree("${buildDir}/outputs/code_coverage").forEach({f ->
if(f.path.endsWith(".ec") || f.path.endsWith(".exec")){
jacocoReports << f
}
})
property "sonar.jacoco.reportPaths", jacocoReports
}
}
android {
compileSdkVersion 28
defaultConfig {
// https://medium.com/@manas/manage-your-android-app-s-versioncode-versionname-with-gradle-7f9c5dcf09bf
versionCode = versionMajor * 10000 +
versionMinor * 100 +
versionPath
versionName = "${versionMajor}.${versionMinor}.${versionPath}"
minSdkVersion 19
targetSdkVersion 28
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas"
.toString()]
}
}
}
buildTypes {
debug {
testCoverageEnabled = true
}
}
flavorDimensions "abi"
productFlavors {
arm {}
arm64 {}
x86 {}
x86_64 {}
universal {} // flavor containing all abi versions
// https://medium.com/@ookami.kb/adding-flavor-specific-tasks-to-gradle-b52b40a3c1b7
all { flavor ->
String name = flavor.name.capitalize()
/* workaround for the issue that we have to execute the copy
* tasks both when packaging debug and release apks but a gradle
* task can only be run once during a build. so this defines two
* separate sets of tasks with the same content.
*/
["Release", "Debug"].each { buildType ->
task("copy${name}${buildType}Assets", type: Copy) {
into "src/main/assets"
if (flavor.name != "universal") {
into("${abiFlavorMap.get(flavor.name)}") {
from "assets/${abiFlavorMap.get(flavor.name)}"
}
} else {
from "assets"
}
eachFile { logger.info("copying file: ${it.file}") }
}
task("clean${name}${buildType}Assets", type: Delete) {
doFirst {
delete "src/main/assets"
targetFiles.getFiles().forEach { file ->
logger.info("deleting file: ${file}")
}
}
}
}
// adding files to an apk:
// https://discuss.gradle.org/t/add-generated-file-to-meta-inf/11831
/* this task generates a dummy lib directory in the apk which
* should trick fdroid into believing that the apk ships native
* code without needing to build an actual jni library with the ndk.
*
* this should work because fdroid uses aapt (not aapt2) to determine if an
* app ships with native code (fdroidserver/update.py:1172 as of 568256f75cb393611769bfc0ff94bf16b4124d3d)
* and aapt scans a lib directory for subdirectories to populate its "native-code" field
* (android/platform/frameworks/base/tools/aapt/Command.cpp:2296 as of 009b2dd158183062f4c9ad681636797350699f57).
*
* this is probably not a good strategy for fdroid in the long run -
* and since this code is only meant to handle the fdroid case it
* may need to change in unforeseen ways in the future.
*/
task("generate${flavor.name.capitalize()}LibDir") {
/*
* at first i tried adding a temporary directory to the resources classpath.
* that works well but only for filenames without extensions.
* for some reason i couldn't make it include any files ending
* with .so for example, seemingly no matter what i put into the include method.
* and even though aapt gets tricked by the dummy file not ending in .so,
* com_android_internal_content_NativeLibraryHelper.cpp, which copies out
* library binaries included in the apk, skips files not ending in .so.
* so installing the apk would fail with "Failed to extract native libraries, res=-113]".
* therefore i now put the files into src/main/jniLibs.
* the disadvantage to this method is that i have to manage
* the state of the directory because it isn't temporary.
*/
def resDir = file("src/main/jniLibs")
doLast {
resDir.deleteDir()
resDir.mkdirs()
List<String> arches = []
if(flavor.name != "universal") {
arches.push(abiFlavorMap.get(flavor.name))
} else {
abiFlavorMap.values().each {
arches.push(it)
}
}
arches.each { arch ->
def destDir = new File(resDir, arch)
destDir.mkdirs()
def file = new File(destDir, "dummy.so")
// the file seem to need to have some dummy content
// to be included
file.text = "${arch}"
}
}
}
}
}
android.productFlavors.each { flavor ->
task("build${flavor.name.capitalize()}Binary") {
if(flavor.name != "universal") {
doLast {
exec {
executable "./build.sh"
// TODO: determine buildtype to specify if the rust binary
// should be built debug or release
args = ["release", "--target", "${flavorTargetMap.get(flavor.name)}",
"--abi", "${abiFlavorMap.get(flavor.name)}"]
}
}
} else {
doLast {
// check if any binaries need to be built
final Map<String, String> missingBinariesMap = [:]
abiFlavorMap.entrySet().each { entry ->
final abi = entry.value
if (!(new File("assets", abi).exists())) {
missingBinariesMap.put(entry.key, abi)
}
}
missingBinariesMap.each { entry ->
println("missing abi for universal flavour: " + entry.value)
exec {
executable "./build.sh"
args = ["release", "--target",
"${flavorTargetMap.get(entry.key)}",
"--abi", "${entry.value}"]
}
}
}
}
}
}
android.productFlavors.each { flavor ->
if(flavor.name != "universal") {
// ensure that packaging the universal apk happens after all
// the binaries have been built
tasks."buildUniversalBinary".mustRunAfter "build${flavor.name.capitalize()}Binary"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {
abortOnError false
}
afterEvaluate {
android.productFlavors.all { flavor ->
String name = flavor.name.capitalize()
tasks."generate${name}DebugResources".dependsOn "build${name}Binary",
"copy${name}DebugAssets", "clean${name}DebugAssets"
tasks."generate${name}ReleaseResources".dependsOn "build${name}Binary",
"copy${name}ReleaseAssets", "clean${name}ReleaseAssets"
tasks."merge${name}DebugResources".dependsOn "generate${name}LibDir"
tasks."merge${name}ReleaseResources".dependsOn "generate${name}LibDir"
}
}
}
android.applicationVariants.all { variant ->
// at the moment product flavours are only used for distinguishing abi
// versions so this should be safe
variant.outputs.each { output ->
String flavor = variant.productFlavors[0].name
String abi = abiFlavorMap.get(flavor)
Integer abiVersionCode = abiVersionCodes.get(abi)
// apks requiring higher api levels should have higher version codes:
// https://developer.android.com/google/play/publishing/multiple-apks#VersionCodes
if(abiVersionCode != null) {
output.versionCodeOverride =
Integer.parseInt("${abiVersionCode}${variant.versionCode}")
}
output.versionNameOverride = variant.versionName + "-" + flavor
}
}
clean {
doLast {
abiFlavorMap.values().each {
delete "assets/${it}"
delete "src/main/assets/${it}"
}
}
}
gradle.taskGraph.whenReady { graph ->
def lastTask = graph.allTasks.last()
if(lastTask.name ==~ /(assemble|build).*/) {
lastTask.doLast {
def pythonExists = false
try {
Runtime r = Runtime.getRuntime()
Process p = r.exec("python3 --version")
p.waitFor()
pythonExists = p.exitValue() == 0 ? true : false
} catch(IOException e) {
logger.warn("no python - not running apk integrity checks")
}
if(pythonExists) {
exec {
environment "GRADLE_BUILD_DIR", "$buildDir"
executable = "python3"
args = ["-m", "unittest", "discover", "-s", "tests"]
}
}
}
}
}