diff --git a/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt b/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt index 2c1966f..1d8a3f7 100644 --- a/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt +++ b/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt @@ -15,6 +15,9 @@ open class CargoBuildTask : DefaultTask() { @Input var toolchain: Toolchain? = null + @Input + var ndk: Ndk? = null + @Suppress("unused") @TaskAction fun build() = with(project) { @@ -25,10 +28,12 @@ open class CargoBuildTask : DefaultTask() { throw GradleException("toolchain cannot be null") } + val ndk = ndk ?: throw GradleException("ndk cannot be null") + project.plugins.all { when (it) { - is AppPlugin -> buildProjectForTarget(project, toolchain, this) - is LibraryPlugin -> buildProjectForTarget(project, toolchain, this) + is AppPlugin -> buildProjectForTarget(project, toolchain, ndk, this) + is LibraryPlugin -> buildProjectForTarget(project, toolchain, ndk, this) } } // CARGO_TARGET_DIR can be used to force the use of a global, shared target directory @@ -77,8 +82,7 @@ open class CargoBuildTask : DefaultTask() { } } - inline fun buildProjectForTarget(project: Project, toolchain: Toolchain, cargoExtension: CargoExtension) { - val app = project.extensions[T::class] + inline fun buildProjectForTarget(project: Project, toolchain: Toolchain, ndk: Ndk, cargoExtension: CargoExtension) { val apiLevel = cargoExtension.apiLevels[toolchain.platform]!! val defaultTargetTriple = getDefaultTargetTriple(project, cargoExtension.rustcCommand) @@ -165,13 +169,8 @@ open class CargoBuildTask : DefaultTask() { // Cross-compiling to Android requires toolchain massaging. if (toolchain.type != ToolchainType.DESKTOP) { - val ndkPath = app.ndkDirectory - val ndkVersion = ndkPath.name - val ndkVersionMajor = try { - ndkVersion.split(".").first().toInt() - } catch (ex: NumberFormatException) { - 0 // Falls back to generic behaviour. - } + val ndkPath = ndk.path + val ndkVersionMajor = ndk.versionMajor val toolchainDirectory = if (toolchain.type == ToolchainType.ANDROID_PREBUILT) { environment("CARGO_NDK_MAJOR_VERSION", ndkVersionMajor) diff --git a/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt b/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt index 892db54..5166274 100644 --- a/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt +++ b/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt @@ -107,6 +107,11 @@ val toolchains = listOf( "android/x86_64") ) +data class Ndk(val path: File, val version: String) { + val versionMajor: Int + get() = version.split(".").first().toInt() +} + data class Toolchain(val platform: String, val type: ToolchainType, val target: String, @@ -227,21 +232,23 @@ open class RustAndroidPlugin : Plugin { } // Determine the NDK version, if present - val ndkSourceProperties = Properties() - val ndkSourcePropertiesFile = File(extensions[T::class].ndkDirectory, "source.properties") - if (ndkSourcePropertiesFile.exists()) { - ndkSourceProperties.load(ndkSourcePropertiesFile.inputStream()) + val ndk = extensions[T::class].ndkDirectory.let { + val ndkSourceProperties = Properties() + val ndkSourcePropertiesFile = File(it, "source.properties") + if (ndkSourcePropertiesFile.exists()) { + ndkSourceProperties.load(ndkSourcePropertiesFile.inputStream()) + } + val ndkVersion = ndkSourceProperties.getProperty("Pkg.Revision", "0.0") + Ndk(path = it, version = ndkVersion) } - val ndkVersion = ndkSourceProperties.getProperty("Pkg.Revision", "0.0") - val ndkVersionMajor = ndkVersion.split(".").first().toInt() // Determine whether to use prebuilt or generated toolchains val usePrebuilt = cargoExtension.localProperties.getProperty("rust.prebuiltToolchains")?.equals("true") ?: cargoExtension.prebuiltToolchains ?: - (ndkVersionMajor >= 19); + (ndk.versionMajor >= 19); - if (usePrebuilt && ndkVersionMajor < 19) { + if (usePrebuilt && ndk.versionMajor < 19) { throw GradleException("usePrebuilt = true requires NDK version 19+") } @@ -299,6 +306,7 @@ open class RustAndroidPlugin : Plugin { group = RUST_TASK_GROUP description = "Build library ($target)" toolchain = theToolchain + this.ndk = ndk } if (!usePrebuilt) {