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