diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-01-24 11:15:52 +0100 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-01-28 10:35:15 +0100 |
| commit | 1fd061cc62fa0b3deb56302d8d74d513345c3037 (patch) | |
| tree | be88797d5613567c2e1806fbbf82d438bd720dc3 /android | |
| parent | 95e51f000f8259dfb3e5966831bd102937e6e935 (diff) | |
| download | mullvadvpn-1fd061cc62fa0b3deb56302d8d74d513345c3037.tar.xz mullvadvpn-1fd061cc62fa0b3deb56302d8d74d513345c3037.zip | |
Bundle a pre-generated relay list instead of generating one
The android app will try to bundle the relays.json
found under dist-assets/relays/ if it exists.
Otherwise it will rely on the relay list
downloaded by the api.
For alpha, beta and stable release builds it is required
to have a bundled relays.json file.
Diffstat (limited to 'android')
6 files changed, 52 insertions, 48 deletions
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 409b042d8c..36625c50e4 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -1,9 +1,7 @@ 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 @@ -21,8 +19,7 @@ plugins { } val repoRootPath = rootProject.projectDir.absoluteFile.parentFile.absolutePath -val extraAssetsDirectory = layout.buildDirectory.dir("extraAssets").get() -val relayListPath = extraAssetsDirectory.file("relays.json").asFile +val relayListDirectory = file("$repoRootPath/dist-assets/relays/").absolutePath val defaultChangelogAssetsDirectory = "$repoRootPath/android/src/main/play/release-notes/" val rustJniLibsDir = layout.buildDirectory.dir("rustJniLibs/android").get() @@ -133,7 +130,7 @@ android { gradleLocalProperties(rootProject.projectDir, providers) .getOrDefault("OVERRIDE_CHANGELOG_DIR", defaultChangelogAssetsDirectory) - assets.srcDirs(extraAssetsDirectory, changelogDir) + assets.srcDirs(relayListDirectory, changelogDir) } } @@ -240,9 +237,6 @@ android { createDistBundle.dependsOn("bundle$capitalizedVariantName") - // 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"].apply { // This is required for the merge task to run every time the .so files are updated. @@ -265,8 +259,10 @@ junitPlatform { } cargo { + val localProperties = gradleLocalProperties(rootProject.projectDir, providers) val isReleaseBuild = isReleaseBuild() - val enableApiOverride = !isReleaseBuild || isAlphaOrDevBuild() + val enableApiOverride = + !isReleaseBuild || isDevBuild(localProperties) || isAlphaBuild(localProperties) module = repoRootPath libname = "mullvad-jni" // All available targets: @@ -300,23 +296,6 @@ cargo { } } -tasks.register<Exec>("generateRelayList") { - workingDir = File(repoRootPath) - standardOutput = ByteArrayOutputStream() - - onlyIf { isReleaseBuild() || !relayListPath.exists() } - - commandLine("cargo", "run", "-p", "mullvad-api", "--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") @@ -330,19 +309,6 @@ if ( 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) || it.contains("fdroid", 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 = diff --git a/android/buildSrc/src/main/kotlin/Utils.kt b/android/buildSrc/src/main/kotlin/Utils.kt index af00586bea..514d511c03 100644 --- a/android/buildSrc/src/main/kotlin/Utils.kt +++ b/android/buildSrc/src/main/kotlin/Utils.kt @@ -1,6 +1,23 @@ import java.util.* import org.gradle.api.Project +// This is a hack and will not work correctly under all scenarios. +// See DROID-1696 for how we can improve this. +fun Project.isReleaseBuild() = + gradle.startParameter.getTaskNames().any { + it.contains("release", ignoreCase = true) || it.contains("fdroid", ignoreCase = true) + } + +fun Project.isAlphaBuild(localProperties: Properties): Boolean { + val versionName = generateVersionName(localProperties) + return versionName.contains("alpha") +} + +fun Project.isDevBuild(localProperties: Properties): Boolean { + val versionName = generateVersionName(localProperties) + return versionName.contains("-dev-") +} + fun Project.generateVersionCode(localProperties: Properties): Int { return localProperties.getProperty("OVERRIDE_VERSION_CODE")?.toIntOrNull() ?: execVersionCodeCargoCommand() diff --git a/android/scripts/update-lockfile.sh b/android/scripts/update-lockfile.sh index 0c20ae31c3..9c08ff80a0 100755 --- a/android/scripts/update-lockfile.sh +++ b/android/scripts/update-lockfile.sh @@ -20,7 +20,6 @@ GRADLE_TASKS=( "lint" ) EXCLUDED_GRADLE_TASKS=( - "-xgenerateRelayList" "-xcargoBuild" ) diff --git a/android/service/build.gradle.kts b/android/service/build.gradle.kts index 717e2c6ef7..8c9f0034cb 100644 --- a/android/service/build.gradle.kts +++ b/android/service/build.gradle.kts @@ -1,3 +1,5 @@ +import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties + plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) @@ -9,7 +11,16 @@ android { compileSdk = Versions.compileSdkVersion buildToolsVersion = Versions.buildToolsVersion - defaultConfig { minSdk = Versions.minSdkVersion } + defaultConfig { + minSdk = Versions.minSdkVersion + val localProperties = gradleLocalProperties(rootProject.projectDir, providers) + val shouldRequireBundleRelayFile = isReleaseBuild() && !isDevBuild(localProperties) + buildConfigField( + "Boolean", + "REQUIRE_BUNDLED_RELAY_FILE", + shouldRequireBundleRelayFile.toString(), + ) + } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index e4c8eba3c6..3c26ad89de 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -220,7 +220,10 @@ class MullvadVpnService : TalpidVpnService() { } private fun Context.prepareFiles() { - extractAndOverwriteIfAssetMoreRecent(RELAY_LIST_ASSET_NAME) + extractAndOverwriteIfAssetMoreRecent( + RELAY_LIST_ASSET_NAME, + BuildConfig.REQUIRE_BUNDLED_RELAY_FILE, + ) } companion object { diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt index 51240fa16d..b4d50caa25 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt @@ -1,23 +1,31 @@ package net.mullvad.mullvadvpn.service.util import android.content.Context +import co.touchlab.kermit.Logger import java.io.File +import java.io.FileNotFoundException import java.io.FileOutputStream -fun Context.extractAndOverwriteIfAssetMoreRecent(assetName: String) { +fun Context.extractAndOverwriteIfAssetMoreRecent(assetName: String, requireAssetFile: Boolean) { val forceOverwriteIfMoreRecent = lastUpdatedTime() > File(filesDir, assetName).lastModified() val destination = File(filesDir, assetName) if (!destination.exists() || forceOverwriteIfMoreRecent) { - extractFile(assetName, destination) + extractFile(assetName, destination, requireAssetFile) } } private fun Context.lastUpdatedTime(): Long = packageManager.getPackageInfo(packageName, 0).lastUpdateTime -private fun Context.extractFile(asset: String, destination: File) { - val destinationStream = FileOutputStream(destination) - assets.open(asset).copyTo(destinationStream) - destinationStream.close() +private fun Context.extractFile(asset: String, destination: File, requireAssetFile: Boolean) { + if (assets.list("")?.contains(asset) == true) { + val destinationStream = FileOutputStream(destination) + assets.open(asset).copyTo(destinationStream) + destinationStream.close() + } else if (requireAssetFile) { + throw FileNotFoundException("Asset $asset not found") + } else { + Logger.i("Asset $asset not found") + } } |
