@@ -13,26 +13,52 @@ plugins {
1313
1414val javaTarget = JvmTarget .fromTarget(libs.versions.jvmTarget.get())
1515
16- fun getGitCommitHash (): String {
17- return try {
18- val headFile = file(" ${project.rootDir} /.git/HEAD" )
19-
20- // Read the commit hash from .git/HEAD
21- if (headFile.exists()) {
22- val headContent = headFile.readText().trim()
23- if (headContent.startsWith(" ref:" )) {
24- val refPath = headContent.substring(5 ) // e.g., refs/heads/main
25- val commitFile = file(" ${project.rootDir} /.git/$refPath " )
26- if (commitFile.exists()) commitFile.readText().trim() else " "
27- } else headContent // If it's a detached HEAD (commit hash directly)
28- } else {
29- " " // If .git/HEAD doesn't exist
30- }.take(7 ) // Return the short commit hash
31- } catch (_: Throwable ) {
32- " " // Just return an empty string if any exception occurs
16+ abstract class GenerateGitHashTask : DefaultTask () {
17+
18+ @get:InputFile
19+ @get:PathSensitive(PathSensitivity .RELATIVE )
20+ abstract val headFile: RegularFileProperty
21+
22+ @get:InputDirectory
23+ @get:PathSensitive(PathSensitivity .RELATIVE )
24+ abstract val headsDir: DirectoryProperty
25+
26+ @get:OutputDirectory
27+ abstract val outputDir: DirectoryProperty
28+
29+ @TaskAction
30+ fun generate () {
31+ val head = headFile.get().asFile
32+
33+ val hash = try {
34+ if (head.exists()) {
35+ // Read the commit hash from .git/HEAD
36+ val headContent = head.readText().trim()
37+ if (headContent.startsWith(" ref:" )) {
38+ val refPath = headContent.substring(5 ) // e.g., refs/heads/main
39+ val commitFile = File (head.parentFile, refPath)
40+ if (commitFile.exists()) commitFile.readText().trim() else " "
41+ } else headContent // If it's a detached HEAD (commit hash directly)
42+ } else " " // If .git/HEAD doesn't exist
43+ } catch (_: Throwable ) {
44+ " " // Just set to an empty string if any exception occurs
45+ }.take(7 ) // Get the short commit hash
46+
47+ val outFile = outputDir.file(" git-hash.txt" ).get().asFile
48+ outFile.parentFile.mkdirs()
49+ outFile.writeText(hash)
3350 }
3451}
3552
53+ val generateGitHash = tasks.register<GenerateGitHashTask >(" generateGitHash" ) {
54+ val gitDir = layout.projectDirectory.dir(" ../.git" )
55+
56+ headFile.set(gitDir.file(" HEAD" ))
57+ headsDir.set(gitDir.dir(" refs/heads" ))
58+
59+ outputDir.set(layout.buildDirectory.dir(" generated/git" ))
60+ }
61+
3662android {
3763 @Suppress(" UnstableApiUsage" )
3864 testOptions {
@@ -47,6 +73,15 @@ android {
4773 includeInBundle = false
4874 }
4975
76+ androidComponents {
77+ onVariants { variant ->
78+ variant.sources.assets?.addGeneratedSourceDirectory(
79+ generateGitHash,
80+ GenerateGitHashTask ::outputDir
81+ )
82+ }
83+ }
84+
5085 signingConfigs {
5186 // We just use SIGNING_KEY_ALIAS here since it won't change
5287 // so won't kill the configuration cache.
@@ -72,8 +107,6 @@ android {
72107 versionCode = 68
73108 versionName = " 4.7.0"
74109
75- resValue(" string" , " commit_hash" , getGitCommitHash())
76-
77110 manifestPlaceholders[" target_sdk_version" ] = libs.versions.targetSdk.get()
78111
79112 // Reads local.properties
@@ -142,11 +175,11 @@ android {
142175 }
143176
144177 java {
145- // Use Java 17 toolchain even if a higher JDK runs the build.
178+ // Use Java 17 toolchain even if a higher JDK runs the build.
146179 // We still use Java 8 for now which higher JDKs have deprecated.
147- toolchain {
148- languageVersion.set(JavaLanguageVersion .of(libs.versions.jdkToolchain.get()))
149- }
180+ toolchain {
181+ languageVersion.set(JavaLanguageVersion .of(libs.versions.jdkToolchain.get()))
182+ }
150183 }
151184
152185 lint {
@@ -156,7 +189,6 @@ android {
156189
157190 buildFeatures {
158191 buildConfig = true
159- resValues = true
160192 viewBinding = true
161193 }
162194
0 commit comments