Skip to content

Commit

Permalink
Fixes based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jan 24, 2025
1 parent 6ebdca4 commit 67c9476
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
37 changes: 4 additions & 33 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -22,7 +20,6 @@ plugins {

val repoRootPath = rootProject.projectDir.absoluteFile.parentFile.absolutePath
val relayListDirectory = file("$repoRootPath/dist-assets/relays/").absolutePath
val relayListPath = file("$relayListDirectory/relays.json")
val defaultChangelogAssetsDirectory = "$repoRootPath/android/src/main/play/release-notes/"
val rustJniLibsDir = layout.buildDirectory.dir("rustJniLibs/android").get()

Expand Down Expand Up @@ -246,10 +243,7 @@ android {
}

// Ensure all relevant assemble tasks depend on our ensure task.
tasks["assemble$capitalizedVariantName"].apply {
dependsOn(tasks["ensureRelayListExist"])
dependsOn(tasks["ensureValidVersionCode"])
}
tasks["assemble$capitalizedVariantName"].dependsOn(tasks["ensureValidVersionCode"])
}
}

Expand All @@ -261,8 +255,10 @@ junitPlatform {
}

cargo {
val localProperties = gradleLocalProperties(rootProject.projectDir, providers)
val isReleaseBuild = isReleaseBuild()
val enableApiOverride = !isReleaseBuild || isDevBuild() || isAlphaBuild()
val enableApiOverride =
!isReleaseBuild || isDevBuild(localProperties) || isAlphaBuild(localProperties)
module = repoRootPath
libname = "mullvad-jni"
// All available targets:
Expand Down Expand Up @@ -296,14 +292,6 @@ cargo {
}
}

tasks.register("ensureRelayListExist") {
doLast {
if (isReleaseBuild() && !isDevBuild() && !relayListPath.exists()) {
throw GradleException("Missing relay list: $relayListPath")
}
}
}

tasks.register<Exec>("cargoClean") {
workingDir = File(repoRootPath)
commandLine("cargo", "clean")
Expand All @@ -317,23 +305,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) }

fun isAlphaBuild(): Boolean {
val localProperties = gradleLocalProperties(rootProject.projectDir, providers)
val versionName = generateVersionName(localProperties)
return versionName.contains("alpha")
}

fun isDevBuild(): Boolean {
val localProperties = gradleLocalProperties(rootProject.projectDir, providers)
val versionName = generateVersionName(localProperties)
return versionName.contains("dev")
}

androidComponents {
beforeVariants { variantBuilder ->
variantBuilder.enable =
Expand Down
15 changes: 15 additions & 0 deletions android/buildSrc/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
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) }

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()
Expand Down
13 changes: 12 additions & 1 deletion android/service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties

plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ 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) {
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")
}
Expand Down
5 changes: 2 additions & 3 deletions prepare-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ if [[ "$ANDROID" == "true" ]]; then
echo "Generate relays.json"
mkdir dist-assets/relays
cargo run -q -p mullvad-api --bin relay_list > dist-assets/relays/relays.json

git add dist-assets/relays/relays.json
git commit -S -m "Bundle relay list" \

git commit -S -m "Add relay list to bundle with $PRODUCT_VERSION" \
dist-assets/relays/relays.json
fi

Expand Down

0 comments on commit 67c9476

Please sign in to comment.