Last active
March 23, 2026 19:14
-
-
Save ArthurKun21/c751abfd100cb739f1b20a50ea777291 to your computer and use it in GitHub Desktop.
git commit gradle configuration cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| androidComponents { | |
| onVariants(selector().all()) { variant -> | |
| val variantName = variant.name.replaceFirstChar { it.uppercase() } | |
| val generateConstantsTaskName = "generateBuildConstants$variantName" | |
| val generateConstantsTaskDescription = "Generates BuildConstants.kt with commit count and SHA for $variantName" | |
| val buildConstantTasks = tasks.register<GenerateBuildConstantsTask>(generateConstantsTaskName) { | |
| group = "build" | |
| description = generateConstantsTaskDescription | |
| outputDir.set(layout.buildDirectory.dir("generated/source/buildConstants/kotlin")) | |
| outputs.upToDateWhen { false } | |
| outputs.cacheIf { false } | |
| } | |
| variant.sources.kotlin?.addGeneratedSourceDirectory(buildConstantTasks) { tasks -> | |
| tasks.outputDir | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.gradle.api.DefaultTask | |
| import org.gradle.api.file.DirectoryProperty | |
| import org.gradle.api.file.RegularFileProperty | |
| import org.gradle.api.provider.Property | |
| import org.gradle.api.tasks.Input | |
| import org.gradle.api.tasks.Internal | |
| import org.gradle.api.tasks.OutputDirectory | |
| import org.gradle.api.tasks.TaskAction | |
| import org.gradle.process.ExecOperations | |
| import java.io.ByteArrayOutputStream | |
| import javax.inject.Inject | |
| abstract class GenerateBuildConstantsTask @Inject constructor( | |
| private val execOperations: ExecOperations, | |
| ) : DefaultTask() { | |
| @get:OutputDirectory | |
| abstract val outputDir: DirectoryProperty | |
| @TaskAction | |
| fun generate() { | |
| val commitCountValue = getCommitCount(execOperations) | |
| val commitShaValue = getGitSha(execOperations) | |
| val packageName = "io.github.app.generated" | |
| val className = "BuildConstants" | |
| val fileContent = """ | |
| // Generated by Gradle. Do not edit. | |
| package $packageName | |
| object $className { | |
| const val COMMIT_COUNT: String = "$commitCountValue" | |
| const val COMMIT_SHA: String = "$commitShaValue" | |
| } | |
| """.trimIndent() | |
| val packagePath = packageName.replace('.', '/') | |
| val outputFile = outputDir.file("$packagePath/$className.kt").get().asFile | |
| outputFile.parentFile.mkdirs() | |
| outputFile.writeText(fileContent) | |
| } | |
| fun getCommitCount(execOperations: ExecOperations): String = try { | |
| runCommand(execOperations, "git rev-list --count HEAD") | |
| } catch (e: Exception) { | |
| println("Error getting commit count: ${e.message}") | |
| "0" | |
| } | |
| fun getGitSha(execOperations: ExecOperations): String = try { | |
| runCommand(execOperations, "git rev-parse --short HEAD") | |
| } catch (e: Exception) { | |
| println("Error getting git sha: ${e.message}") | |
| "0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment