diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-01-08 09:33:44 +0100 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-01-08 10:48:48 +0100 |
| commit | 1c1ca559b69810904538c2e46c2326d2ebcaa014 (patch) | |
| tree | e42cb4535910bdeeb25018786035a21130e5ff31 /android/app | |
| parent | ac9ac4b824b62d60f64be46f69a4678152d64fb5 (diff) | |
| download | mullvadvpn-1c1ca559b69810904538c2e46c2326d2ebcaa014.tar.xz mullvadvpn-1c1ca559b69810904538c2e46c2326d2ebcaa014.zip | |
Add gradle rust plugin
Diffstat (limited to 'android/app')
| -rw-r--r-- | android/app/build.gradle.kts | 110 |
1 files changed, 86 insertions, 24 deletions
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 5fed7575d4..dd1444302c 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -1,7 +1,9 @@ import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties import com.android.build.gradle.internal.tasks.factory.dependsOn import com.github.triplet.gradle.androidpublisher.ReleaseStatus +import java.io.ByteArrayOutputStream import java.io.FileInputStream +import java.io.FileOutputStream import java.util.Properties import org.gradle.internal.extensions.stdlib.capitalized @@ -13,6 +15,7 @@ plugins { alias(libs.plugins.kotlin.ksp) alias(libs.plugins.compose) alias(libs.plugins.protobuf.core) + alias(libs.plugins.rust.android.gradle) id(Dependencies.junit5AndroidPluginId) version Versions.junit5Plugin } @@ -21,7 +24,6 @@ val repoRootPath = rootProject.projectDir.absoluteFile.parentFile.absolutePath val extraAssetsDirectory = layout.buildDirectory.dir("extraAssets").get() val relayListPath = extraAssetsDirectory.file("relays.json").asFile val defaultChangelogAssetsDirectory = "$repoRootPath/android/src/main/play/release-notes/" -val extraJniDirectory = layout.buildDirectory.dir("extraJni").get() val credentialsPath = "${rootProject.projectDir}/credentials" val keystorePropertiesFile = file("$credentialsPath/keystore.properties") @@ -35,6 +37,7 @@ android { namespace = "net.mullvad.mullvadvpn" compileSdk = Versions.compileSdkVersion buildToolsVersion = Versions.buildToolsVersion + ndkVersion = Versions.ndkVersion defaultConfig { val localProperties = gradleLocalProperties(rootProject.projectDir, providers) @@ -126,7 +129,6 @@ android { .getOrDefault("OVERRIDE_CHANGELOG_DIR", defaultChangelogAssetsDirectory) assets.srcDirs(extraAssetsDirectory, changelogDir) - jniLibs.srcDirs(extraJniDirectory) } } @@ -239,12 +241,14 @@ android { createDistBundle.dependsOn("bundle$capitalizedVariantName") - // Ensure all relevant assemble tasks depend on our ensure tasks. - tasks["assemble$capitalizedVariantName"].apply { - dependsOn(tasks["ensureRelayListExist"]) - dependsOn(tasks["ensureJniDirectoryExist"]) - dependsOn(tasks["ensureValidVersionCode"]) - } + // Ensure we have relay list ready before merging assets. + tasks["merge${capitalizedVariantName}Assets"].dependsOn(tasks["generateRelayList"]) + + // Ensure that we have all the JNI libs before merging them. + tasks["merge${capitalizedVariantName}JniLibFolders"].dependsOn("cargoBuild") + + // Ensure all relevant assemble tasks depend on our ensure task. + tasks["assemble$capitalizedVariantName"].dependsOn(tasks["ensureValidVersionCode"]) } } @@ -255,6 +259,80 @@ junitPlatform { } } +cargo { + val isReleaseBuild = isReleaseBuild() + val enableApiOverride = !isReleaseBuild || isAlphaOrDevBuild() + module = repoRootPath + libname = "mullvad-jni" + // All available targets: + // https://github.com/mozilla/rust-android-gradle/tree/master?tab=readme-ov-file#targets + targets = + gradleLocalProperties(rootProject.projectDir, providers) + .getProperty("CARGO_TARGETS") + ?.split(",") ?: listOf("arm", "arm64", "x86", "x86_64") + profile = + if (isReleaseBuild) { + "release" + } else { + "debug" + } + prebuiltToolchains = true + targetDirectory = "$repoRootPath/target" + features { + if (enableApiOverride) { + defaultAnd(arrayOf("api-override")) + } + } + targetIncludes = arrayOf("libmullvad_jni.so") + extraCargoBuildArguments = buildList { + add("--package=mullvad-jni") + if (isReleaseBuild) { + add("--locked") + } + } +} + +tasks.register<Exec>("generateRelayList") { + workingDir = File(repoRootPath) + standardOutput = ByteArrayOutputStream() + + onlyIf { isReleaseBuild() || !relayListPath.exists() } + + commandLine("cargo", "run", "--bin", "relay_list") + + doLast { + val output = standardOutput as ByteArrayOutputStream + // Create file if needed + relayListPath.parentFile.mkdirs() + relayListPath.createNewFile() + FileOutputStream(relayListPath).use { it.write(output.toByteArray()) } + } +} + +tasks.register<Exec>("cargoClean") { + workingDir = File(repoRootPath) + commandLine("cargo", "clean") +} + +if ( + gradleLocalProperties(rootProject.projectDir, providers) + .getProperty("CLEAN_CARGO_BUILD") + ?.toBoolean() != false +) { + tasks["clean"].dependsOn("cargoClean") +} + +// This is a hack and will not work correctly under all scenarios. +// See DROID-1696 for how we can improve this. +fun isReleaseBuild() = + gradle.startParameter.getTaskNames().any { it.contains("release", ignoreCase = true) } + +fun isAlphaOrDevBuild(): Boolean { + val localProperties = gradleLocalProperties(rootProject.projectDir, providers) + val versionName = generateVersionName(localProperties) + return versionName.contains("dev") || versionName.contains("alpha") +} + androidComponents { beforeVariants { variantBuilder -> variantBuilder.enable = @@ -276,22 +354,6 @@ configure<org.owasp.dependencycheck.gradle.extension.DependencyCheckExtension> { skipConfigurations = listOf("lintClassPath") } -tasks.register("ensureRelayListExist") { - doLast { - if (!relayListPath.exists()) { - throw GradleException("Missing relay list: $relayListPath") - } - } -} - -tasks.register("ensureJniDirectoryExist") { - doLast { - if (!extraJniDirectory.asFile.exists()) { - throw GradleException("Missing JNI directory: $extraJniDirectory") - } - } -} - // This is a safety net to avoid generating too big version codes, since that could potentially be // hard and inconvenient to recover from. tasks.register("ensureValidVersionCode") { |
