| 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("$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 |
"x86_64-linux-android" to "x86_64", |
| 19 |
) |
| 20 |
|
| 21 |
// Discover prefixes. |
| 22 |
val prefixes = ArrayList<File>() |
| 23 |
if (inSourceTree) { |
| 24 |
for ((triplet, _) in KNOWN_ABIS.entries) { |
| 25 |
val prefix = file("$PYTHON_CROSS_DIR/$triplet/prefix") |
| 26 |
if (prefix.exists()) { |
| 27 |
prefixes.add(prefix) |
| 28 |
} |
| 29 |
} |
| 30 |
} else { |
| 31 |
// Testbed is inside a release package. |
| 32 |
val prefix = file("$ANDROID_DIR/prefix") |
| 33 |
if (prefix.exists()) { |
| 34 |
prefixes.add(prefix) |
| 35 |
} |
| 36 |
} |
| 37 |
if (prefixes.isEmpty()) { |
| 38 |
throw GradleException( |
| 39 |
"No Android prefixes found: see README.md for testing instructions" |
| 40 |
) |
| 41 |
} |
| 42 |
|
| 43 |
// Detect Python versions and ABIs. |
| 44 |
lateinit var pythonVersion: String |
| 45 |
var abis = HashMap<File, String>() |
| 46 |
for ((i, prefix) in prefixes.withIndex()) { |
| 47 |
val libDir = file("$prefix/lib") |
| 48 |
val version = run { |
| 49 |
for (filename in libDir.list()!!) { |
| 50 |
"""python(\d+\.\d+[a-z]*)""".toRegex().matchEntire(filename)?.let { |
| 51 |
return@run it.groupValues[1] |
| 52 |
} |
| 53 |
} |
| 54 |
throw GradleException("Failed to find Python version in $libDir") |
| 55 |
} |
| 56 |
if (i == 0) { |
| 57 |
pythonVersion = version |
| 58 |
} else if (pythonVersion != version) { |
| 59 |
throw GradleException( |
| 60 |
"${prefixes[0]} is Python $pythonVersion, but $prefix is Python $version" |
| 61 |
) |
| 62 |
} |
| 63 |
|
| 64 |
val libPythonDir = file("$libDir/python$pythonVersion") |
| 65 |
val triplet = run { |
| 66 |
for (filename in libPythonDir.list()!!) { |
| 67 |
"""_sysconfigdata_[a-z]*_android_(.+).py""".toRegex() |
| 68 |
.matchEntire(filename)?.let { |
| 69 |
return@run it.groupValues[1] |
| 70 |
} |
| 71 |
} |
| 72 |
throw GradleException("Failed to find Python triplet in $libPythonDir") |
| 73 |
} |
| 74 |
abis[prefix] = KNOWN_ABIS[triplet]!! |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
android { |
| 79 |
val androidEnvFile = file("../../android-env.sh").absoluteFile |
| 80 |
|
| 81 |
namespace = "org.python.testbed" |
| 82 |
compileSdk = 35 |
| 83 |
|
| 84 |
defaultConfig { |
| 85 |
applicationId = "org.python.testbed" |
| 86 |
|
| 87 |
minSdk = androidEnvFile.useLines { |
| 88 |
for (line in it) { |
| 89 |
"""ANDROID_API_LEVEL:=(\d+)""".toRegex().find(line)?.let { |
| 90 |
return@useLines it.groupValues[1].toInt() |
| 91 |
} |
| 92 |
} |
| 93 |
throw GradleException("Failed to find API level in $androidEnvFile") |
| 94 |
} |
| 95 |
|
| 96 |
// This controls the API level of the maxVersion managed emulator, which is used |
| 97 |
// by CI and cibuildwheel. |
| 98 |
// * 33 has excessive buffering in the logcat client |
| 99 |
// (https://cs.android.com/android/_/android/platform/system/logging/+/d340721894f223327339010df59b0ac514308826). |
| 100 |
// * 34 consumes too much disk space on GitHub Actions (#142289). |
| 101 |
// * 35 has issues connecting to the internet (#142387). |
| 102 |
// * 36 and later are not available as aosp_atd images yet. |
| 103 |
targetSdk = 32 |
| 104 |
|
| 105 |
versionCode = 1 |
| 106 |
versionName = "1.0" |
| 107 |
|
| 108 |
ndk.abiFilters.addAll(abis.values) |
| 109 |
externalNativeBuild.cmake.arguments( |
| 110 |
"-DPYTHON_PREFIX_DIR=" + if (inSourceTree) { |
| 111 |
// AGP uses the ${} syntax for its own purposes, so use a Jinja style |
| 112 |
// placeholder. |
| 113 |
"$PYTHON_CROSS_DIR/{{triplet}}/prefix" |
| 114 |
} else { |
| 115 |
prefixes[0] |
| 116 |
}, |
| 117 |
"-DPYTHON_VERSION=$pythonVersion", |
| 118 |
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON", |
| 119 |
) |
| 120 |
|
| 121 |
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
| 122 |
} |
| 123 |
|
| 124 |
ndkVersion = androidEnvFile.useLines { |
| 125 |
for (line in it) { |
| 126 |
"""ndk_version=(\S+)""".toRegex().find(line)?.let { |
| 127 |
return@useLines it.groupValues[1] |
| 128 |
} |
| 129 |
} |
| 130 |
throw GradleException("Failed to find NDK version in $androidEnvFile") |
| 131 |
} |
| 132 |
externalNativeBuild.cmake { |
| 133 |
path("src/main/c/CMakeLists.txt") |
| 134 |
} |
| 135 |
|
| 136 |
// Set this property to something nonexistent but non-empty. Otherwise it'll use the |
| 137 |
// default list, which ignores asset directories beginning with an underscore, and |
| 138 |
// maybe also other files required by tests. |
| 139 |
aaptOptions.ignoreAssetsPattern = "android-testbed-dont-ignore-anything" |
| 140 |
|
| 141 |
compileOptions { |
| 142 |
sourceCompatibility = JavaVersion.VERSION_1_8 |
| 143 |
targetCompatibility = JavaVersion.VERSION_1_8 |
| 144 |
} |
| 145 |
kotlinOptions { |
| 146 |
jvmTarget = "1.8" |
| 147 |
} |
| 148 |
|
| 149 |
testOptions { |
| 150 |
managedDevices { |
| 151 |
localDevices { |
| 152 |
create("minVersion") { |
| 153 |
device = "Small Phone" |
| 154 |
|
| 155 |
// Managed devices have a minimum API level of 27. |
| 156 |
apiLevel = max(27, defaultConfig.minSdk!!) |
| 157 |
|
| 158 |
// ATD devices are smaller and faster, but have a minimum |
| 159 |
// API level of 30. |
| 160 |
systemImageSource = if (apiLevel >= 30) "aosp-atd" else "aosp" |
| 161 |
} |
| 162 |
|
| 163 |
create("maxVersion") { |
| 164 |
device = "Small Phone" |
| 165 |
apiLevel = defaultConfig.targetSdk!! |
| 166 |
systemImageSource = "aosp-atd" |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// If the previous test run succeeded and nothing has changed, |
| 171 |
// Gradle thinks there's no need to run it again. Override that. |
| 172 |
afterEvaluate { |
| 173 |
(localDevices.names + listOf("connected")).forEach { |
| 174 |
tasks.named("${it}DebugAndroidTest") { |
| 175 |
outputs.upToDateWhen { false } |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
dependencies { |
| 184 |
implementation("androidx.appcompat:appcompat:1.6.1") |
| 185 |
implementation("com.google.android.material:material:1.11.0") |
| 186 |
implementation("androidx.constraintlayout:constraintlayout:2.1.4") |
| 187 |
androidTestImplementation("androidx.test.ext:junit:1.1.5") |
| 188 |
androidTestImplementation("androidx.test:rules:1.5.0") |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
// Create some custom tasks to copy Python and its standard library from |
| 193 |
// elsewhere in the repository. |
| 194 |
androidComponents.onVariants { variant -> |
| 195 |
val pyPlusVer = "python$pythonVersion" |
| 196 |
generateTask(variant, variant.sources.assets!!) { |
| 197 |
into("python") { |
| 198 |
// Include files such as pyconfig.h are used by some of the tests. |
| 199 |
into("include/$pyPlusVer") { |
| 200 |
for (prefix in prefixes) { |
| 201 |
from("$prefix/include/$pyPlusVer") |
| 202 |
} |
| 203 |
duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 204 |
} |
| 205 |
|
| 206 |
into("lib/$pyPlusVer") { |
| 207 |
// To aid debugging, the source directory takes priority when |
| 208 |
// running inside a CPython source tree. |
| 209 |
if (inSourceTree) { |
| 210 |
from("$PYTHON_DIR/Lib") |
| 211 |
} |
| 212 |
for (prefix in prefixes) { |
| 213 |
from("$prefix/lib/$pyPlusVer") |
| 214 |
} |
| 215 |
|
| 216 |
into("site-packages") { |
| 217 |
from("$projectDir/src/main/python") |
| 218 |
|
| 219 |
val sitePackages = findProperty("python.sitePackages") as String? |
| 220 |
if (!sitePackages.isNullOrEmpty()) { |
| 221 |
if (!file(sitePackages).exists()) { |
| 222 |
throw GradleException("$sitePackages does not exist") |
| 223 |
} |
| 224 |
from(sitePackages) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 229 |
exclude("**/__pycache__") |
| 230 |
} |
| 231 |
|
| 232 |
into("cwd") { |
| 233 |
val cwd = findProperty("python.cwd") as String? |
| 234 |
if (!cwd.isNullOrEmpty()) { |
| 235 |
if (!file(cwd).exists()) { |
| 236 |
throw GradleException("$cwd does not exist") |
| 237 |
} |
| 238 |
from(cwd) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// A filename ending with .gz will be automatically decompressed |
| 243 |
// while building the APK. Avoid this by adding a dash to the end, |
| 244 |
// and add an extra dash to any filenames that already end with one. |
| 245 |
// This will be undone in MainActivity.kt. |
| 246 |
rename(""".*(\.gz|-)""", "$0-") |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
generateTask(variant, variant.sources.jniLibs!!) { |
| 251 |
for ((prefix, abi) in abis.entries) { |
| 252 |
into(abi) { |
| 253 |
from("$prefix/lib") |
| 254 |
include("libpython*.*.so") |
| 255 |
include("lib*_python.so") |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
fun generateTask( |
| 263 |
variant: ApplicationVariant, directories: SourceDirectories, |
| 264 |
configure: GenerateTask.() -> Unit |
| 265 |
) { |
| 266 |
val taskName = "generate" + |
| 267 |
listOf(variant.name, "Python", directories.name) |
| 268 |
.map { it.replaceFirstChar(Char::uppercase) } |
| 269 |
.joinToString("") |
| 270 |
|
| 271 |
directories.addGeneratedSourceDirectory( |
| 272 |
tasks.register<GenerateTask>(taskName) { |
| 273 |
into(outputDir) |
| 274 |
configure() |
| 275 |
}, |
| 276 |
GenerateTask::outputDir) |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
// addGeneratedSourceDirectory requires the task to have a DirectoryProperty. |
| 281 |
abstract class GenerateTask: Sync() { |
| 282 |
@get:OutputDirectory |
| 283 |
abstract val outputDir: DirectoryProperty |
| 284 |
} |