Debian Codegraph @

Android/testbed/app/build.gradle.kts

raw
1 import com.android.build.api.variant.*
2 import kotlin.math.max
3
4 plugins {
5 id("com.android.application")
6 id("org.jetbrains.kotlin.android")
7 }
8
9 val ANDROID_DIR = file("../..")
10 val PYTHON_DIR = ANDROID_DIR.parentFile!!
11 val PYTHON_CROSS_DIR = file(System.getenv("CROSS_BUILD_DIR") ?: "$PYTHON_DIR/cross-build")
12 val inSourceTree = (
13 ANDROID_DIR.name == "Android" && file("$PYTHON_DIR/pyconfig.h.in").exists()
14 )
15
16 val KNOWN_ABIS = mapOf(
17 "aarch64-linux-android" to "arm64-v8a",
18 "arm-linux-androideabi" to "armeabi-v7a",
19 "i686-linux-android" to "x86",
20 "x86_64-linux-android" to "x86_64",
21 )
22
23 val osArch = System.getProperty("os.arch")
24 val NATIVE_ABI = mapOf(
25 "aarch64" to "arm64-v8a",
26 "amd64" to "x86_64",
27 "arm64" to "arm64-v8a",
28 "x86_64" to "x86_64",
29 )[osArch] ?: throw GradleException("Unknown os.arch '$osArch'")
30
31 // Discover prefixes.
32 val prefixes = ArrayList<File>()
33 if (inSourceTree) {
34 for ((triplet, _) in KNOWN_ABIS.entries) {
35 val prefix = file("$PYTHON_CROSS_DIR/$triplet/prefix")
36 if (prefix.exists()) {
37 prefixes.add(prefix)
38 }
39 }
40 } else {
41 // Testbed is inside a release package.
42 val prefix = file("$ANDROID_DIR/prefix")
43 if (prefix.exists()) {
44 prefixes.add(prefix)
45 }
46 }
47 if (prefixes.isEmpty()) {
48 throw GradleException(
49 "No Android prefixes found: see README.md for testing instructions"
50 )
51 }
52
53 // Detect Python versions and ABIs.
54 lateinit var pythonVersion: String
55 var abis = HashMap<File, String>()
56 for ((i, prefix) in prefixes.withIndex()) {
57 val libDir = file("$prefix/lib")
58 val version = run {
59 for (filename in libDir.list()!!) {
60 """python(\d+\.\d+[a-z]*)""".toRegex().matchEntire(filename)?.let {
61 return@run it.groupValues[1]
62 }
63 }
64 throw GradleException("Failed to find Python version in $libDir")
65 }
66 if (i == 0) {
67 pythonVersion = version
68 } else if (pythonVersion != version) {
69 throw GradleException(
70 "${prefixes[0]} is Python $pythonVersion, but $prefix is Python $version"
71 )
72 }
73
74 val libPythonDir = file("$libDir/python$pythonVersion")
75 val triplet = run {
76 for (filename in libPythonDir.list()!!) {
77 """_sysconfigdata_[a-z]*_android_(.+).py""".toRegex()
78 .matchEntire(filename)?.let {
79 return@run it.groupValues[1]
80 }
81 }
82 throw GradleException("Failed to find Python triplet in $libPythonDir")
83 }
84 abis[prefix] = KNOWN_ABIS[triplet]!!
85 }
86
87
88 android {
89 val androidEnvFile = file("../../android-env.sh").absoluteFile
90
91 namespace = "org.python.testbed"
92 compileSdk = 35
93
94 defaultConfig {
95 applicationId = "org.python.testbed"
96
97 minSdk = androidEnvFile.useLines {
98 for (line in it) {
99 """ANDROID_API_LEVEL:=(\d+)""".toRegex().find(line)?.let {
100 return@useLines it.groupValues[1].toInt()
101 }
102 }
103 throw GradleException("Failed to find API level in $androidEnvFile")
104 }
105
106 // This controls the API level of the maxVersion managed emulator, which is used
107 // by CI and cibuildwheel.
108 // * 33 has excessive buffering in the logcat client
109 // (https://cs.android.com/android/_/android/platform/system/logging/+/d340721894f223327339010df59b0ac514308826).
110 // * 34 consumes too much disk space on GitHub Actions (#142289).
111 // * 35 has issues connecting to the internet (#142387).
112 // * 36 and later are not available as aosp_atd images yet.
113 targetSdk = 32
114
115 versionCode = 1
116 versionName = "1.0"
117
118 ndk.abiFilters.addAll(abis.values)
119 externalNativeBuild.cmake.arguments(
120 "-DPYTHON_PREFIX_DIR=" + if (inSourceTree) {
121 // AGP uses the ${} syntax for its own purposes, so use a Jinja style
122 // placeholder.
123 "$PYTHON_CROSS_DIR/{{triplet}}/prefix"
124 } else {
125 prefixes[0]
126 },
127 "-DPYTHON_VERSION=$pythonVersion",
128 "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
129 )
130
131 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
132 }
133
134 ndkVersion = androidEnvFile.useLines {
135 for (line in it) {
136 """ndk_version=(\S+)""".toRegex().find(line)?.let {
137 return@useLines it.groupValues[1]
138 }
139 }
140 throw GradleException("Failed to find NDK version in $androidEnvFile")
141 }
142 externalNativeBuild.cmake {
143 path("src/main/c/CMakeLists.txt")
144 }
145
146 // Set this property to something nonexistent but non-empty. Otherwise it'll use the
147 // default list, which ignores asset directories beginning with an underscore, and
148 // maybe also other files required by tests.
149 aaptOptions.ignoreAssetsPattern = "android-testbed-dont-ignore-anything"
150
151 compileOptions {
152 sourceCompatibility = JavaVersion.VERSION_1_8
153 targetCompatibility = JavaVersion.VERSION_1_8
154 }
155 kotlinOptions {
156 jvmTarget = "1.8"
157 }
158
159 testOptions {
160 managedDevices {
161 localDevices {
162 // systemImageSource should use what its documentation calls an
163 // "explicit source", i.e. the sdkmanager package name format, because
164 // that will be required in CreateEmulatorTask below.
165 create("minVersion") {
166 device = "Small Phone"
167
168 // Managed devices have a minimum API level of 27.
169 apiLevel = max(27, defaultConfig.minSdk!!)
170
171 // ATD devices are smaller and faster, but have a minimum
172 // API level of 30.
173 systemImageSource = if (apiLevel >= 30) "aosp_atd" else "default"
174 }
175
176 create("maxVersion") {
177 device = "Small Phone"
178 apiLevel = defaultConfig.targetSdk!!
179 systemImageSource = "aosp_atd"
180 }
181 }
182
183 // If the previous test run succeeded and nothing has changed,
184 // Gradle thinks there's no need to run it again. Override that.
185 afterEvaluate {
186 (localDevices.names + listOf("connected")).forEach {
187 tasks.named("${it}DebugAndroidTest") {
188 outputs.upToDateWhen { false }
189 }
190 }
191 }
192 }
193 }
194 }
195
196 dependencies {
197 implementation("androidx.appcompat:appcompat:1.6.1")
198 implementation("com.google.android.material:material:1.11.0")
199 implementation("androidx.constraintlayout:constraintlayout:2.1.4")
200 androidTestImplementation("androidx.test.ext:junit:1.1.5")
201 androidTestImplementation("androidx.test:rules:1.5.0")
202 }
203
204
205 afterEvaluate {
206 // Every new emulator has a maximum of 2 GB RAM, regardless of its hardware profile
207 // (https://cs.android.com/android-studio/platform/tools/base/+/refs/tags/studio-2025.3.2:sdklib/src/main/java/com/android/sdklib/internal/avd/EmulatedProperties.java;l=68).
208 // This is barely enough to test Python, and not enough to test Pandas
209 // (https://github.com/python/cpython/pull/137186#issuecomment-3136301023,
210 // https://github.com/pandas-dev/pandas/pull/63405#issuecomment-3667846159).
211 // So we'll increase it by editing the emulator configuration files.
212 //
213 // If the emulator doesn't exist yet, we want to edit it after it's created, but
214 // before it starts for the first time. Otherwise it'll need to be cold-booted
215 // again, which would slow down the first run, which is likely the only run in CI
216 // environments. But the Setup task both creates and starts the emulator if it
217 // doesn't already exist. So we create it ourselves before the Setup task runs.
218 for (device in android.testOptions.managedDevices.localDevices) {
219 val createTask = tasks.register<CreateEmulatorTask>("${device.name}Create") {
220 this.device = device.device
221 apiLevel = device.apiLevel
222 systemImageSource = device.systemImageSource
223 abi = NATIVE_ABI
224 }
225 tasks.named("${device.name}Setup") {
226 dependsOn(createTask)
227 }
228 }
229 }
230
231 abstract class CreateEmulatorTask : DefaultTask() {
232 @get:Input abstract val device: Property<String>
233 @get:Input abstract val apiLevel: Property<Int>
234 @get:Input abstract val systemImageSource: Property<String>
235 @get:Input abstract val abi: Property<String>
236 @get:Inject abstract val execOps: ExecOperations
237
238 private val avdName by lazy {
239 listOf(
240 "dev${apiLevel.get()}",
241 systemImageSource.get(),
242 abi.get(),
243 device.get().replace(' ', '_'),
244 ).joinToString("_")
245 }
246
247 private val avdDir by lazy {
248 // XDG_CONFIG_HOME is respected by both avdmanager and Gradle.
249 val userHome = System.getenv("ANDROID_USER_HOME") ?: (
250 (System.getenv("XDG_CONFIG_HOME") ?: System.getProperty("user.home")!!)
251 + "/.android"
252 )
253 File("$userHome/avd/gradle-managed", "$avdName.avd")
254 }
255
256 @TaskAction
257 fun run() {
258 if (!avdDir.exists()) {
259 createAvd()
260 }
261 updateAvd()
262 }
263
264 fun createAvd() {
265 val systemImage = listOf(
266 "system-images",
267 "android-${apiLevel.get()}",
268 systemImageSource.get(),
269 abi.get(),
270 ).joinToString(";")
271
272 runCmdlineTool("sdkmanager", systemImage)
273 runCmdlineTool(
274 "avdmanager", "create", "avd",
275 "--name", avdName,
276 "--path", avdDir,
277 "--device", device.get().lowercase().replace(" ", "_"),
278 "--package", systemImage,
279 )
280
281 val iniName = "$avdName.ini"
282 if (!File(avdDir.parentFile.parentFile, iniName).renameTo(
283 File(avdDir.parentFile, iniName)
284 )) {
285 throw GradleException("Failed to rename $iniName")
286 }
287 }
288
289 fun updateAvd() {
290 for (filename in listOf(
291 "config.ini", // Created by avdmanager; always exists
292 "hardware-qemu.ini", // Created on first run; might not exist
293 )) {
294 val iniFile = File(avdDir, filename)
295 if (!iniFile.exists()) {
296 if (filename == "config.ini") {
297 throw GradleException("$iniFile does not exist")
298 }
299 continue
300 }
301
302 val iniText = iniFile.readText()
303 val pattern = Regex(
304 """^\s*hw.ramSize\s*=\s*(.+?)\s*$""", RegexOption.MULTILINE
305 )
306 val matches = pattern.findAll(iniText).toList()
307 if (matches.size != 1) {
308 throw GradleException(
309 "Found ${matches.size} instances of $pattern in $iniFile; expected 1"
310 )
311 }
312
313 val expectedRam = "4096"
314 if (matches[0].groupValues[1] != expectedRam) {
315 iniFile.writeText(
316 iniText.replace(pattern, "hw.ramSize = $expectedRam")
317 )
318 }
319 }
320 }
321
322 fun runCmdlineTool(tool: String, vararg args: Any) {
323 val androidHome = System.getenv("ANDROID_HOME")!!
324 val exeSuffix =
325 if (System.getProperty("os.name").lowercase().startsWith("win")) ".exe"
326 else ""
327 val command =
328 listOf("$androidHome/cmdline-tools/latest/bin/$tool$exeSuffix", *args)
329 println(command.joinToString(" "))
330 execOps.exec {
331 commandLine(command)
332 }
333 }
334 }
335
336
337 // Create some custom tasks to copy Python and its standard library from
338 // elsewhere in the repository.
339 androidComponents.onVariants { variant ->
340 val pyPlusVer = "python$pythonVersion"
341 generateTask(variant, variant.sources.assets!!) {
342 into("python") {
343 // Include files such as pyconfig.h are used by some of the tests.
344 into("include/$pyPlusVer") {
345 for (prefix in prefixes) {
346 from("$prefix/include/$pyPlusVer")
347 }
348 duplicatesStrategy = DuplicatesStrategy.EXCLUDE
349 }
350
351 into("lib/$pyPlusVer") {
352 // To aid debugging, the source directory takes priority when
353 // running inside a CPython source tree.
354 if (inSourceTree) {
355 from("$PYTHON_DIR/Lib")
356 }
357 for (prefix in prefixes) {
358 from("$prefix/lib/$pyPlusVer")
359 }
360
361 into("site-packages") {
362 from("$projectDir/src/main/python")
363
364 val sitePackages = findProperty("python.sitePackages") as String?
365 if (!sitePackages.isNullOrEmpty()) {
366 if (!file(sitePackages).exists()) {
367 throw GradleException("$sitePackages does not exist")
368 }
369 from(sitePackages)
370 }
371 }
372
373 duplicatesStrategy = DuplicatesStrategy.EXCLUDE
374 exclude("**/__pycache__")
375 }
376
377 into("cwd") {
378 val cwd = findProperty("python.cwd") as String?
379 if (!cwd.isNullOrEmpty()) {
380 if (!file(cwd).exists()) {
381 throw GradleException("$cwd does not exist")
382 }
383 from(cwd)
384 }
385 }
386
387 // A filename ending with .gz will be automatically decompressed
388 // while building the APK. Avoid this by adding a dash to the end,
389 // and add an extra dash to any filenames that already end with one.
390 // This will be undone in MainActivity.kt.
391 rename(""".*(\.gz|-)""", "$0-")
392 }
393 }
394
395 generateTask(variant, variant.sources.jniLibs!!) {
396 for ((prefix, abi) in abis.entries) {
397 into(abi) {
398 from("$prefix/lib")
399 include("libpython*.*.so")
400 include("lib*_python.so")
401 }
402 }
403 }
404 }
405
406
407 fun generateTask(
408 variant: ApplicationVariant, directories: SourceDirectories,
409 configure: GenerateTask.() -> Unit
410 ) {
411 val taskName = "generate" +
412 listOf(variant.name, "Python", directories.name)
413 .map { it.replaceFirstChar(Char::uppercase) }
414 .joinToString("")
415
416 directories.addGeneratedSourceDirectory(
417 tasks.register<GenerateTask>(taskName) {
418 into(outputDir)
419 configure()
420 },
421 GenerateTask::outputDir)
422 }
423
424
425 // addGeneratedSourceDirectory requires the task to have a DirectoryProperty.
426 abstract class GenerateTask: Sync() {
427 @get:OutputDirectory
428 abstract val outputDir: DirectoryProperty
429 }