Skip to content

Commit

Permalink
Add support for Magisk's built-in module updater for release builds
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed May 23, 2022
1 parent e016f45 commit 3236de1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
54 changes: 45 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.ObjectId
import org.jetbrains.kotlin.backend.common.pop
import org.json.JSONObject

plugins {
id("com.android.application")
Expand All @@ -10,6 +11,7 @@ plugins {
buildscript {
dependencies {
classpath("org.eclipse.jgit:org.eclipse.jgit:6.1.0.202203080745-r")
classpath("org.json:json:20220320")
}
}

Expand Down Expand Up @@ -88,6 +90,9 @@ val gitVersionTriple = describeVersion(git)
val gitVersionCode = getVersionCode(gitVersionTriple)
val gitVersionName = getVersionName(git, gitVersionTriple)

val projectUrl = "https://github.com/chenxiaolong/BCR"
val releaseMetadataBranch = "master"

android {
namespace = "com.chiller3.bcr"

Expand All @@ -102,7 +107,8 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

buildConfigField("String", "GIT_COMMIT", "\"${gitVersionTriple.third.name}\"")
buildConfigField("String", "PROJECT_URL_AT_COMMIT",
"\"${projectUrl}/tree/${gitVersionTriple.third.name}\"")
}
signingConfigs {
create("release") {
Expand Down Expand Up @@ -154,14 +160,19 @@ android.applicationVariants.all {
outputs.file(outputFile)

doLast {
outputFile.writeText("""
id=${variant.applicationId}
name=Basic Call Recorder
version=v${variant.versionName}
versionCode=${variant.versionCode}
author=chenxiaolong
description=Basic Call Recorder
""".trimIndent())
val props = LinkedHashMap<String, String>()
props["id"] = variant.applicationId
props["name"] = "BCR"
props["version"] = "v${variant.versionName}"
props["versionCode"] = variant.versionCode.toString()
props["author"] = "chenxiaolong"
props["description"] = "Basic Call Recorder"

if (variant.name == "release") {
props["updateJson"] = "${projectUrl}/raw/${releaseMetadataBranch}/app/magisk/updates/${variant.name}/info.json"
}

outputFile.writeText(props.map { "${it.key}=${it.value}" }.joinToString("\n"))
}
}

Expand Down Expand Up @@ -208,4 +219,29 @@ android.applicationVariants.all {
from(File(rootDir, "LICENSE"))
from(File(rootDir, "README.md"))
}

tasks.register("updateJson${capitalized}") {
val magiskDir = File(projectDir, "magisk")
val updatesDir = File(magiskDir, "updates")
val variantUpdateDir = File(updatesDir, variant.name)
val jsonFile = File(variantUpdateDir, "info.json")
val changelogFile = File(variantUpdateDir, "changelog.txt")

outputs.file(jsonFile)
outputs.file(changelogFile)

doLast {
val root = JSONObject()
root.put("version", variant.versionName)
root.put("versionCode", variant.versionCode)
root.put("zipUrl", "${projectUrl}/releases/download/${gitVersionTriple.first}/BCR-${variant.versionName}-release.zip")
root.put("changelog", "${projectUrl}/raw/${releaseMetadataBranch}/app/magisk/updates/${variant.name}/changelog.txt")

jsonFile.writer().use {
root.write(it, 4, 0)
}

changelogFile.writeText("Please see ${projectUrl}/releases/tag/${gitVersionTriple.first} for the changelog.\n")
}
}
}
8 changes: 2 additions & 6 deletions app/src/main/java/com/chiller3/bcr/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class SettingsActivity : AppCompatActivity() {
return true
}
prefVersion -> {
val url = PROJECT_BASE_URL + BuildConfig.GIT_COMMIT
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
val uri = Uri.parse(BuildConfig.PROJECT_URL_AT_COMMIT)
startActivity(Intent(Intent.ACTION_VIEW, uri))
return true
}
}
Expand Down Expand Up @@ -169,9 +169,5 @@ class SettingsActivity : AppCompatActivity() {
}
}
}

companion object {
private const val PROJECT_BASE_URL = "https://github.com/chenxiaolong/BCR/tree/"
}
}
}

0 comments on commit 3236de1

Please sign in to comment.