blob: 92701ecd309ceddd2e94bfa6068f9a5cc994f645 [file] [log] [blame]
LuK133726b79022022-09-05 23:32:37 +02001import groovy.util.Node
2import groovy.util.NodeList
3import groovy.xml.XmlParser
4
LuK13379c4638c2022-09-05 13:37:03 +02005plugins {
6 id("com.android.application")
7 id("kotlin-android")
8}
9
10android {
11 compileSdk = 33
12
13 defaultConfig {
LuK133747933d12022-09-06 15:27:27 +020014 applicationId = "org.lineageos.aperture.dev"
LuK13379c4638c2022-09-05 13:37:03 +020015 minSdk = 31
16 targetSdk = 33
17 versionCode = 1
18 versionName = "1.0"
19
20 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21 }
22
23 buildTypes {
24 named("release") {
25 // Enables code shrinking, obfuscation, and optimization.
26 isMinifyEnabled = true
27
28 // Enables resource shrinking.
29 isShrinkResources = true
30
31 // Includes the default ProGuard rules files.
32 setProguardFiles(
33 listOf(
34 getDefaultProguardFile("proguard-android-optimize.txt"),
35 "proguard-rules.pro"
36 )
37 )
38 }
39 }
40
41 compileOptions {
42 sourceCompatibility = JavaVersion.VERSION_1_8
43 targetCompatibility = JavaVersion.VERSION_1_8
44 }
45
46 kotlinOptions {
47 jvmTarget = "1.8"
48 }
49}
50
51dependencies {
52 implementation("androidx.core:core-ktx:1.8.0")
53 implementation("androidx.appcompat:appcompat:1.5.0")
54 implementation("androidx.constraintlayout:constraintlayout:2.1.4")
55 implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
LuK1337afc94a32022-09-05 23:30:33 +020056 implementation("androidx.preference:preference:1.2.0")
LuK13379c4638c2022-09-05 13:37:03 +020057 implementation("com.google.android.material:material:1.8.0-alpha01")
58
59 // CameraX core library using the camera2 implementation
60 val cameraxVersion = "1.2.0-beta02"
61 // The following line is optional, as the core library is included indirectly by camera-camera2
62 implementation("androidx.camera:camera-core:${cameraxVersion}")
63 implementation("androidx.camera:camera-camera2:${cameraxVersion}")
64 // If you want to additionally use the CameraX Lifecycle library
65 implementation("androidx.camera:camera-lifecycle:${cameraxVersion}")
66 // If you want to additionally use the CameraX VideoCapture library
67 implementation("androidx.camera:camera-video:${cameraxVersion}")
68 // If you want to additionally use the CameraX View class
69 implementation("androidx.camera:camera-view:${cameraxVersion}")
70 // If you want to additionally use the CameraX Extensions library
71 implementation("androidx.camera:camera-extensions:${cameraxVersion}")
72
73 // ZXing
74 implementation("com.google.zxing:core:3.5.0")
75
76 // Coil
77 implementation("io.coil-kt:coil:2.2.0")
78 implementation("io.coil-kt:coil-video:2.2.0")
79}
LuK133726b79022022-09-05 23:32:37 +020080
81tasks.register("generateBp") {
82 val project = project(":app")
83 val configuration = project.configurations["debugRuntimeClasspath"]
84
85 val libsBase = File("${project.projectDir.absolutePath}/libs")
86 libsBase.deleteRecursively()
87
88 val moduleString = { it: ModuleVersionIdentifier -> "${it.group}:${it.name}:${it.version}" }
89 val modulePath =
90 { it: ModuleVersionIdentifier -> "${it.group.replace(".", "/")}/${it.name}/${it.version}" }
91
92 val spaces = { it: Int ->
93 var ret = ""
94 for (i in it downTo 1) {
95 ret += ' '
96 }
97 ret
98 }
99
100 val moduleName = { it: Any ->
101 when (it) {
102 is ModuleVersionIdentifier -> {
103 "${rootProject.name}_${it.group}_${it.name}"
104 }
105 is String -> {
106 if (it.contains(":")) {
107 val (group, artifactId) = it.split(":")
108 "${rootProject.name}_${group}_${artifactId}"
109 } else {
110 "${rootProject.name}_${it}"
111 }
112 }
113 else -> {
114 throw Exception("Invalid `it` type")
115 }
116 }
117 }
118
119 val moduleNameAosp = { it: String ->
120 when (it) {
121 "androidx.constraintlayout:constraintlayout" -> "androidx-constraintlayout_constraintlayout"
122 "com.google.auto.value:auto-value-annotations" -> "auto_value_annotations"
123 "com.google.guava:listenablefuture" -> "guava"
124 "org.jetbrains.kotlin:kotlin-stdlib" -> "kotlin-stdlib"
125 "org.jetbrains.kotlin:kotlin-stdlib-jdk8" -> "kotlin-stdlib-jdk8"
126 "org.jetbrains.kotlinx:kotlinx-coroutines-android" -> "kotlinx-coroutines-android"
127 else -> it.replace(":", "_")
128 }
129 }
130
131 val isAvailableInAosp = { group: String, artifactId: String ->
132 when {
133 group.startsWith("androidx") -> {
134 // We provide our own androidx.camera & lifecycle-common
135 !group.startsWith("androidx.camera") && artifactId != "lifecycle-common"
136 }
137 group.startsWith("org.jetbrains") -> {
138 // kotlin-android-extensions-runtime & kotlin-parcelize-runtime aren't in AOSP
139 !artifactId.startsWith("kotlin-android-extensions-runtime") &&
140 !artifactId.startsWith("kotlin-parcelize-runtime")
141 }
142 group == "com.google.auto.value" -> true
143 group == "com.google.guava" -> true
144 group == "junit" -> true
145 else -> false
146 }
147 }
148
149 // Update app/Android.bp
150 File("${project.projectDir.absolutePath}/Android.bp").let { file ->
151 // Read dependencies
152 val dependencies = "${spaces(8)}// DO NOT EDIT THIS SECTION MANUALLY\n".plus(
153 configuration.allDependencies.joinToString("\n") {
154 if (isAvailableInAosp(it.group!!, it.name)) {
155 "${spaces(8)}\"${moduleNameAosp("${it.group}:${it.name}")}\","
156 } else {
157 "${spaces(8)}\"${moduleName("${it.group}:${it.name}")}\","
158 }
159 }
160 )
161
162 // Replace existing dependencies with newly generated ones
163 file.writeText(
164 file.readText().replace(
165 "static_libs: \\[.*?\\]".toRegex(RegexOption.DOT_MATCHES_ALL),
166 "static_libs: [%s]".format("\n$dependencies\n${spaces(4)}")
167 )
168 )
169 }
170
171 // Update app/libs
172 configuration.resolvedConfiguration.resolvedArtifacts.sortedBy {
173 moduleString(it.moduleVersion.id)
174 }.distinctBy {
175 moduleString(it.moduleVersion.id)
176 }.forEach {
177 val id = it.moduleVersion.id
178
179 // Skip modules that are available in AOSP
180 if (isAvailableInAosp(id.group, it.name)) {
181 return@forEach
182 }
183
184 // Get file path
185 val dirPath = "${libsBase}/${modulePath(id)}"
186 val filePath = "${dirPath}/${it.file.name}"
187
188 // Copy artifact to app/libs
189 it.file.copyTo(File(filePath))
190
191 // Parse dependencies
192 val dependencies =
193 it.file.parentFile.parentFile.walk().first { file -> file.extension == "pom" }
194 .let { file ->
195 val ret = mutableListOf<String>()
196
197 val pom = XmlParser().parse(file)
198 val dependencies = (pom["dependencies"] as NodeList).firstOrNull() as Node?
199
200 dependencies?.children()?.forEach { node ->
201 val dependency = node as Node
202 ret.add(
203 "${
204 (dependency.get("groupId") as NodeList).text()
205 }:${
206 (dependency.get("artifactId") as NodeList).text()
207 }"
208 )
209 }
210
211 ret
212 }
213
LuK1337ba4d20f2022-09-06 20:24:53 +0200214 var targetSdkVersion = android.defaultConfig.minSdk
LuK133726b79022022-09-05 23:32:37 +0200215 var minSdkVersion = 14
216
217 // Extract AndroidManifest.xml for AARs
218 if (it.file.extension == "aar") {
219 copy {
220 from(zipTree(filePath).matching { include("/AndroidManifest.xml") }.singleFile)
221 into(dirPath)
222 }
223
224 val androidManifest = XmlParser().parse(File("${dirPath}/AndroidManifest.xml"))
225
226 val usesSdk = (androidManifest["uses-sdk"] as NodeList).first() as Node
227 targetSdkVersion = (usesSdk.get("@targetSdkVersion") as Int?) ?: targetSdkVersion
228 minSdkVersion = (usesSdk.get("@minSdkVersion") as Int?) ?: minSdkVersion
229 }
230
231 // Write Android.bp
232 File("$libsBase/Android.bp").let { file ->
233 // Add autogenerated header if file is empty
234 if (file.length() == 0L) {
235 file.writeText("// DO NOT EDIT THIS FILE MANUALLY")
236 }
237
238 val formatDeps = { addNoDeps: Boolean ->
239 val deps = dependencies.filter { dep ->
240 when {
241 configuration.resolvedConfiguration.resolvedArtifacts.firstOrNull { artifact ->
242 dep == "${artifact.moduleVersion.id.group}:${artifact.moduleVersion.id.name}"
243 } == null -> {
244 val moduleName = if (addNoDeps) {
245 moduleName(id)
246 } else {
247 "${moduleName(id)}-nodeps"
248 }
249 println("$moduleName: Skipping $dep because it's not in resolvedArtifacts")
250 false
251 }
252 dep == "org.jetbrains.kotlin:kotlin-stdlib-common" -> false
253 else -> true
254 }
LuK133726b79022022-09-05 23:32:37 +0200255 }.toMutableList()
256
257 if (addNoDeps) {
258 // Add -nodeps dependency for android_library/java_library_static
259 deps.add(0, "${id.group}_${id.name}-nodeps")
260 }
261
262 var ret = ""
263
264 if (deps.isNotEmpty()) {
265 deps.forEach { dep ->
266 ret += if (dep.contains(":")) {
267 val (group, artifactId) = dep.split(":")
268 if (isAvailableInAosp(group, artifactId)) {
269 "\n${spaces(8)}\"${moduleNameAosp(dep)}\","
270 } else {
271 "\n${spaces(8)}\"${moduleName(dep)}\","
272 }
273 } else {
274 "\n${spaces(8)}\"${moduleName(dep)}\","
275 }
276 }
277 ret += "\n${spaces(4)}"
278 }
279
280 ret
281 }
282
283 when (it.extension) {
284 "aar" -> {
285 file.appendText(
286 """
287
288 android_library_import {
289 name: "${moduleName(id)}-nodeps",
290 aars: ["${modulePath(id)}/${it.file.name}"],
291 sdk_version: "$targetSdkVersion",
292 min_sdk_version: "$minSdkVersion",
293 apex_available: [
294 "//apex_available:platform",
295 "//apex_available:anyapex",
296 ],
297 static_libs: [%s],
298 }
299
300 android_library {
301 name: "${moduleName(id)}",
302 sdk_version: "$targetSdkVersion",
303 min_sdk_version: "$minSdkVersion",
304 apex_available: [
305 "//apex_available:platform",
306 "//apex_available:anyapex",
307 ],
308 manifest: "${modulePath(id)}/AndroidManifest.xml",
309 static_libs: [%s],
310 java_version: "1.7",
311 }
312
313 """.trimIndent().format(formatDeps(false), formatDeps(true))
314 )
315 }
316 "jar" -> {
317 file.appendText(
318 """
319
320 java_import {
321 name: "${moduleName(id)}-nodeps",
322 jars: ["${modulePath(id)}/${it.file.name}"],
323 sdk_version: "$targetSdkVersion",
324 min_sdk_version: "$minSdkVersion",
325 apex_available: [
326 "//apex_available:platform",
327 "//apex_available:anyapex",
328 ],
329 }
330
331 java_library_static {
332 name: "${moduleName(id)}",
333 sdk_version: "$targetSdkVersion",
334 min_sdk_version: "$minSdkVersion",
335 apex_available: [
336 "//apex_available:platform",
337 "//apex_available:anyapex",
338 ],
339 static_libs: [%s],
340 java_version: "1.7",
341 }
342
343 """.trimIndent().format(formatDeps(true))
344 )
345 }
346 else -> throw Exception("Unknown file extension: ${it.extension}")
347 }
348 }
349 }
350}