mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 07:58:02 +00:00
android: sign release APKs with Gradle
This commit is contained in:
1 parent
0366b13275
commit
d72649b4e4
5 files changed
+47
-183
No files matched your search
@@ -80,7 +80,7 @@ The app calls into a Haskell core library via JNI/FFI:
|
||||
### `local.properties` (create from `local.properties.example`)
|
||||
|
||||
```properties
|
||||
compression.level=0 # APK compression (0-9)
|
||||
compression.level=0 # not 0: native libraries compressed in the APK and extracted on install
|
||||
enable_debuggable=true # Debug mode
|
||||
application_id.suffix=.debug # Multiple app instances on same device
|
||||
app.name=SimpleX Debug # App name for debug builds
|
||||
|
||||
@@ -8,9 +8,27 @@ plugins {
|
||||
id("org.jetbrains.kotlin.plugin.compose")
|
||||
}
|
||||
|
||||
// The release key, as simplex-chat-build.py passes it; android.injected.signing.* properties take precedence
|
||||
val releaseSigningEnv = listOf("ANDROID_KEYSTORE", "ANDROID_KEYSTORE_PASSWORD", "ANDROID_KEY_ALIAS", "ANDROID_KEY_PASSWORD")
|
||||
.associateWith { System.getenv(it) }
|
||||
if (releaseSigningEnv.values.any { it != null } && releaseSigningEnv.values.any { it == null }) {
|
||||
throw GradleException("Set all of ${releaseSigningEnv.keys.joinToString()} to sign, or none")
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk = 36
|
||||
|
||||
if (releaseSigningEnv.values.all { it != null }) {
|
||||
signingConfigs {
|
||||
create("release") {
|
||||
storeFile = file(releaseSigningEnv.getValue("ANDROID_KEYSTORE")!!)
|
||||
storePassword = releaseSigningEnv.getValue("ANDROID_KEYSTORE_PASSWORD")
|
||||
keyAlias = releaseSigningEnv.getValue("ANDROID_KEY_ALIAS")
|
||||
keyPassword = releaseSigningEnv.getValue("ANDROID_KEY_PASSWORD")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "chat.simplex.app"
|
||||
namespace = "chat.simplex.app"
|
||||
@@ -61,6 +79,7 @@ android {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
|
||||
signingConfig = signingConfigs.findByName("release")
|
||||
}
|
||||
}
|
||||
kotlinOptions {
|
||||
@@ -156,6 +175,11 @@ gradle.taskGraph.whenReady {
|
||||
if (hasTask("$projectPath:packageFossReleaseBundle")) {
|
||||
throw GradleException("An app bundle must include Play Billing, use bundleGoogleRelease or assembleFossRelease")
|
||||
}
|
||||
val releaseTasks = listOf("packageFossRelease", "packageGoogleReleaseBundle").map { "$projectPath:$it" }
|
||||
val signed = android.signingConfigs.findByName("release") != null || project.hasProperty("android.injected.signing.key.alias")
|
||||
if (releaseTasks.any { hasTask(it) } && !signed) {
|
||||
logger.warn("warning: the release is unsigned, set ${releaseSigningEnv.keys.joinToString()} to sign it")
|
||||
}
|
||||
// `isBundle` above is derived from the whole invocation, so a bundle in it disables abi splits
|
||||
if (apkTaskPaths.any { hasTask(it) } && bundleTaskPaths.any { hasTask(it) }) {
|
||||
throw GradleException("Build the apks and the bundle in separate invocations, the bundle disables abi splits")
|
||||
@@ -192,76 +216,16 @@ dependencies {
|
||||
debugImplementation("androidx.compose.ui:ui-tooling:1.6.4")
|
||||
}
|
||||
|
||||
tasks {
|
||||
val compressApk by creating {
|
||||
doLast {
|
||||
val javaHome = System.getProperties()["java.home"] ?: org.gradle.internal.jvm.Jvm.current().javaHome
|
||||
val sdkDir = android.sdkDirectory.absolutePath
|
||||
// A single invocation can package more than one variant, for example assembleDebug
|
||||
gradle.taskGraph.allTasks.filter { it.path in apkTaskPaths }.forEach { packageTask ->
|
||||
val variant = packageTask.name.removePrefix("package")
|
||||
val buildType: String = if (variant.endsWith("Release")) "release" else "debug"
|
||||
val keyAlias: String
|
||||
val keyPassword: String
|
||||
val storeFile: String
|
||||
val storePassword: String
|
||||
if (project.properties["android.injected.signing.key.alias"] != null) {
|
||||
keyAlias = project.properties["android.injected.signing.key.alias"] as String
|
||||
keyPassword = project.properties["android.injected.signing.key.password"] as String
|
||||
storeFile = project.properties["android.injected.signing.store.file"] as String
|
||||
storePassword = project.properties["android.injected.signing.store.password"] as String
|
||||
} else {
|
||||
try {
|
||||
val gradleConfig = android.signingConfigs.getByName(buildType)
|
||||
keyAlias = gradleConfig.keyAlias!!
|
||||
keyPassword = gradleConfig.keyPassword!!
|
||||
storeFile = gradleConfig.storeFile!!.absolutePath
|
||||
storePassword = gradleConfig.storePassword!!
|
||||
} catch (e: UnknownDomainObjectException) {
|
||||
// There is no signing config for current build type, can"t sign the apk
|
||||
println("No signing configs for this build type: $buildType")
|
||||
return@forEach
|
||||
}
|
||||
}
|
||||
val outputDir = packageTask.outputs.files.files.last()
|
||||
exec {
|
||||
workingDir("../../scripts/android")
|
||||
environment = mapOf(
|
||||
"JAVA_HOME" to "$javaHome",
|
||||
"PATH" to "${System.getenv("PATH")}:$javaHome/bin"
|
||||
)
|
||||
commandLine = listOf(
|
||||
"./compress-and-sign-apk.sh",
|
||||
"${rootProject.extra["compression.level"]}",
|
||||
"$outputDir",
|
||||
sdkDir,
|
||||
storeFile,
|
||||
storePassword,
|
||||
keyAlias,
|
||||
keyPassword
|
||||
)
|
||||
}
|
||||
|
||||
if (project.properties["android.injected.signing.key.alias"] != null && buildType == "release") {
|
||||
val flavor = variant.removeSuffix("Release").lowercase()
|
||||
mapOf("arm64-v8a" to "simplex.apk", "armeabi-v7a" to "simplex-armv7a.apk").forEach { (abi, name) ->
|
||||
if (!File(outputDir, "android-$flavor-$abi-release.apk").renameTo(File(outputDir, name))) {
|
||||
logger.warn("No $abi apk to rename to $name")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// View all gradle properties set
|
||||
// project.properties.each { k, v -> println "$k -> $v" }
|
||||
// Release apks keep the names of the released files
|
||||
tasks.matching { it.path == "$projectPath:packageFossRelease" }.configureEach {
|
||||
doLast {
|
||||
val outputDir = outputs.files.files.last()
|
||||
val renamed = mapOf("arm64-v8a" to "simplex.apk", "armeabi-v7a" to "simplex-armv7a.apk").filter { (abi, name) ->
|
||||
listOf("release", "release-unsigned").map { File(outputDir, "android-foss-$abi-$it.apk") }
|
||||
.find { it.exists() }?.renameTo(File(outputDir, name)) == true
|
||||
}
|
||||
}
|
||||
|
||||
// Don"t do anything if no compression is needed
|
||||
if (rootProject.extra["compression.level"] as Int != 0) {
|
||||
whenTaskAdded {
|
||||
if (name in apkTasks) {
|
||||
finalizedBy(compressApk)
|
||||
}
|
||||
if (renamed.isEmpty()) {
|
||||
throw GradleException("No release apk in $outputDir to rename")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ buildscript {
|
||||
// Provide, for example, `application_id.suffix=.debug` in local.properties
|
||||
// to allow debug & release versions to coexist
|
||||
extra.set("application_id.suffix", prop["application_id.suffix"] ?: "")
|
||||
// Compression level for debug AND release apk. 0 = disable compression. Max is 9
|
||||
// Not 0: the apk compresses the native libraries, and Android extracts them on install
|
||||
extra.set("compression.level", (prop["compression.level"] as String?)?.toIntOrNull() ?: 0)
|
||||
if (prop["simplex.assets.dir"] != null) {
|
||||
extra.set("simplex.assets.dir", prop["simplex.assets.dir"])
|
||||
|
||||
@@ -15,13 +15,13 @@ nix_config="sandbox = true
|
||||
max-jobs = auto
|
||||
experimental-features = nix-command flakes"
|
||||
|
||||
commands="nix git curl gradle zip unzip zipalign"
|
||||
commands="nix git curl gradle unzip"
|
||||
arches="${ARCHES:-aarch64 armv7a}"
|
||||
|
||||
arch_map() {
|
||||
case $1 in
|
||||
aarch64) android_arch="arm64-v8a" ;;
|
||||
armv7a) android_arch="armeabi-v7a" ;;
|
||||
aarch64) android_arch="arm64-v8a"; release_apk="simplex.apk" ;;
|
||||
armv7a) android_arch="armeabi-v7a"; release_apk="simplex-armv7a.apk" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
@@ -101,51 +101,38 @@ build() {
|
||||
sed -i.bak 's/${extract_native_libs}/true/' "$folder/apps/multiplatform/android/src/main/AndroidManifest.xml"
|
||||
sed -i.bak 's/jniLibs.useLegacyPackaging =.*/jniLibs.useLegacyPackaging = true/' "$folder/apps/multiplatform/android/build.gradle.kts"
|
||||
sed -i.bak '/android {/a lint {abortOnError = false}' "$folder/apps/multiplatform/android/build.gradle.kts"
|
||||
sed -i.bak '/^tasks {/Q' "$folder/apps/multiplatform/android/build.gradle.kts"
|
||||
sed -i.bak "s/android.version_code=.*/android.version_code=${vercode}/" "$folder/apps/multiplatform/gradle.properties"
|
||||
|
||||
for arch in $arches; do
|
||||
android_simplex_lib="${folder}#hydraJobs.x86_64-linux.${arch}-android:lib:simplex-chat"
|
||||
android_support_lib="${folder}#hydraJobs.x86_64-linux.${arch}-android:lib:support"
|
||||
|
||||
android_simplex_lib_output="${PWD}/result/pkg-${arch}-android-libsimplex.zip"
|
||||
android_support_lib_output="${PWD}/result/pkg-${arch}-android-libsupport.zip"
|
||||
android_simplex_lib_result="${PWD}/result-${arch}-libsimplex"
|
||||
android_support_lib_result="${PWD}/result-${arch}-libsupport"
|
||||
android_simplex_lib_output="${android_simplex_lib_result}/pkg-${arch}-android-libsimplex.zip"
|
||||
android_support_lib_output="${android_support_lib_result}/pkg-${arch}-android-libsupport.zip"
|
||||
|
||||
arch_map "$arch"
|
||||
|
||||
android_tmp_folder="${tmp}/android-${arch}"
|
||||
android_apk_output="${folder}/apps/multiplatform/android/build/outputs/apk/foss/release/android-foss-${android_arch}-release-unsigned.apk"
|
||||
android_apk_output="${folder}/apps/multiplatform/android/build/outputs/apk/foss/release/${release_apk}"
|
||||
android_apk_output_final="simplex-chat-${android_arch}.apk"
|
||||
libs_folder="${folder}/apps/multiplatform/common/src/commonMain/cpp/android/libs"
|
||||
|
||||
# Create missing folders
|
||||
mkdir -p "$libs_folder/$android_arch"
|
||||
|
||||
nix build "$android_simplex_lib"
|
||||
nix build --out-link "$android_simplex_lib_result" "$android_simplex_lib"
|
||||
unzip -o "$android_simplex_lib_output" -d "$libs_folder/$android_arch"
|
||||
|
||||
nix build "$android_support_lib"
|
||||
nix build --out-link "$android_support_lib_result" "$android_support_lib"
|
||||
unzip -o "$android_support_lib_output" -d "$libs_folder/$android_arch"
|
||||
|
||||
# Build only one arch
|
||||
sed -i.bak "s/include(.*/include(\"${android_arch}\")/" "$folder/apps/multiplatform/android/build.gradle.kts"
|
||||
gradle -p "$folder/apps/multiplatform/" -Psimplex.assets.dir=../../assets clean :android:assembleFossRelease
|
||||
|
||||
mkdir -p "$android_tmp_folder"
|
||||
unzip -oqd "$android_tmp_folder" "$android_apk_output"
|
||||
|
||||
# Determenistic build
|
||||
find "$android_tmp_folder" -type f -exec chmod 644 {} +
|
||||
find "$android_tmp_folder" -type d -exec chmod 755 {} +
|
||||
find "$android_tmp_folder" -exec touch -h -d '@1764547200' {} +
|
||||
|
||||
(
|
||||
cd "$android_tmp_folder" && \
|
||||
find . -type f -print0 | sort -z | xargs -0 zip -X -rq5 "$tmp/$android_apk_output_final" && \
|
||||
find res resources.arsc -type f -print0 | sort -z | xargs -0 zip -X -rq0 "$tmp/$android_apk_output_final"
|
||||
)
|
||||
|
||||
zipalign -p -f 4 "$tmp/$android_apk_output_final" "$PWD/$android_apk_output_final"
|
||||
# AGP's apk is deterministic: the release is this apk with AGP's signature
|
||||
cp "$android_apk_output" "$PWD/$android_apk_output_final"
|
||||
|
||||
rm -rf "$libs_folder/$android_arch"
|
||||
done
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Fail fast in case any command fails
|
||||
set -e
|
||||
|
||||
level=$1
|
||||
apk_parent_dir=$2
|
||||
sdk_dir=$3
|
||||
|
||||
store_file=$4
|
||||
store_password=$5
|
||||
key_alias=$6
|
||||
key_password=$7
|
||||
|
||||
if [ -z "${7}" ]; then echo "You didn't enter all required params:
|
||||
compress-and-sign-apk.sh level apk_parent_dir sdk_dir store_file store_password key_alias key_password"
|
||||
fi
|
||||
|
||||
cd "$apk_parent_dir"
|
||||
|
||||
touch remove_this_file remove_this_FILE
|
||||
(( $(ls | grep "remove_this" | wc -l)==1 )) && case_insensitive=1 || case_insensitive=0
|
||||
#echo Case-insensitive file system: $case_insensitive
|
||||
rm remove_this_file remove_this_FILE 2> /dev/null || true
|
||||
|
||||
ORIG_NAMES=( $(echo android*.apk) )
|
||||
for ORIG_NAME in "${ORIG_NAMES[@]}"; do
|
||||
unzip -o -q -d apk $ORIG_NAME
|
||||
ORIG_NAME_COPY=$ORIG_NAME-copy
|
||||
mv "$ORIG_NAME" "$ORIG_NAME_COPY"
|
||||
|
||||
# Determenistic build
|
||||
find apk -type f -exec chmod 644 {} +
|
||||
find apk -type d -exec chmod 755 {} +
|
||||
find apk -exec touch -h -d '2025-12-01T00:00:00' {} +
|
||||
|
||||
(
|
||||
cd apk
|
||||
find . -not -path './res/*' -not -name 'resources.arsc' -type f -print0 | sort -z | xargs -0 zip -X -r -q -"$level" ../"$ORIG_NAME"
|
||||
)
|
||||
|
||||
if [ $case_insensitive -eq 1 ]; then
|
||||
# For case-insensitive file systems
|
||||
list_of_files=$(unzip -l "$ORIG_NAME_COPY" | grep res/ | sed -e "s|.*res/|res/|" | sort)
|
||||
for file in $list_of_files; do
|
||||
unzip -o -q -d apk "$ORIG_NAME_COPY" "$file"
|
||||
(
|
||||
cd apk
|
||||
chmod 644 "$file"
|
||||
touch -h -d '2025-12-01T00:00:00' "$file"
|
||||
zip -X -r -q -0 ../"$ORIG_NAME" "$file"
|
||||
)
|
||||
done
|
||||
else
|
||||
# This method is not working correctly on case-insensitive file systems since Android AAPT produce the same names of files
|
||||
# but with different case like xX.png, Xx.png, xx.png, etc
|
||||
(
|
||||
cd apk
|
||||
find res -type f -print0 | sort -z | xargs -0 zip -X -r -q -0 ../"$ORIG_NAME"
|
||||
)
|
||||
fi
|
||||
|
||||
# Shouldn't be compressed because of Android requirement
|
||||
(
|
||||
cd apk
|
||||
find resources.arsc -type f -print0 | sort -z | xargs -0 zip -X -r -q -0 ../"$ORIG_NAME"
|
||||
)
|
||||
|
||||
#(cd apk && 7z a -r -mx=$level -tzip -x!resources.arsc ../$ORIG_NAME .)
|
||||
#(cd apk && 7z a -r -mx=0 -tzip ../$ORIG_NAME resources.arsc)
|
||||
|
||||
ALL_TOOLS=("$sdk_dir"/build-tools/*/)
|
||||
BIN_DIR="${ALL_TOOLS[${#ALL_TOOLS[@]}-1]}"
|
||||
|
||||
"$BIN_DIR"/zipalign -p -f 4 "$ORIG_NAME" "$ORIG_NAME"-2
|
||||
|
||||
mv "$ORIG_NAME"{-2,}
|
||||
|
||||
"$BIN_DIR"/apksigner sign \
|
||||
--ks "$store_file" --ks-key-alias "$key_alias" --ks-pass "pass:$store_password" \
|
||||
--key-pass "pass:$key_password" "$ORIG_NAME"
|
||||
|
||||
# cleanup
|
||||
rm "$ORIG_NAME_COPY" 2> /dev/null || true
|
||||
rm -rf apk || true
|
||||
rm "${ORIG_NAME}".idsig 2> /dev/null || true
|
||||
done
|
||||
Reference in new issue
Block a user