@@ -2,6 +2,7 @@ import java.io.FileInputStream
22import java.util.*
33import com.android.build.gradle.BaseExtension
44import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
5+ import org.kohsuke.github.GitHub
56
67val isAndroid = plugins.hasPlugin(" com.android.application" )
78val isAndroidLib = plugins.hasPlugin(" com.android.library" )
@@ -18,8 +19,20 @@ if (isKotlinAndroid) println("kotlin android")
1819val javaVersion = JavaVersion .VERSION_17
1920val javaVersionInt = javaVersion.majorVersion.toInt()
2021
22+ val commitCountExec = providers.exec {
23+ executable(" git" )
24+ args(" rev-list" , " --count" , " HEAD" , projectDir.absolutePath)
25+ }
26+ val commitCount = commitCountExec.standardOutput.asText.get().trim().toInt()
27+
28+ val commitHashExec = providers.exec {
29+ executable(" git" )
30+ args(" rev-parse" , " --short" , " HEAD" )
31+ }
32+
33+ val commitHash = commitHashExec.standardOutput.asText.get().trim()
34+
2135if (isAndroid || isAndroidLib) {
22-
2336 val android = extensions.getByType<BaseExtension >()
2437 with (android) {
2538
@@ -41,11 +54,20 @@ if (isAndroid || isAndroidLib) {
4154 }
4255 defaultConfig {
4356 signingConfig = signingConfigs.getByName(" release" )
44- proguardFiles(getDefaultProguardFile(" proguard-android-optimize.txt" ), " ../proguard-rules.pro" )
45-
46- if (isAndroidLib) {
47- consumerProguardFiles(" consumer-rules.pro" )
48- }
57+ }
58+ } else {
59+ defaultConfig {
60+ signingConfig = signingConfigs.getByName(" debug" )
61+ }
62+ }
63+
64+ defaultConfig {
65+ versionCode = commitCount
66+ versionName = " $commitCount -$commitHash "
67+
68+ proguardFiles(getDefaultProguardFile(" proguard-android-optimize.txt" ), " ../proguard-rules.pro" )
69+ if (isAndroidLib) {
70+ consumerProguardFiles(" consumer-rules.pro" )
4971 }
5072 }
5173
@@ -73,6 +95,49 @@ if (isAndroid || isAndroidLib) {
7395 }
7496}
7597
98+ if (isAndroid) {
99+ val android = extensions.getByType<BaseExtension >()
100+
101+ tasks.create(" createGithubRelease" ) {
102+ val file = rootProject.file(" local.properties" )
103+ val properties = Properties ()
104+ properties.load(file.inputStream())
105+
106+ val repo = properties[" github_repo" ]
107+ check(repo != null && repo is String ) { " github_repo not provided in local.properties " }
108+ val token = properties[" github_api_key" ]
109+ check(token != null && token is String ) { " github_api_key not provided in local.properties" }
110+
111+ dependsOn(" assembleRelease" )
112+
113+ doFirst {
114+ val packageRelease = project.tasks.getByName<DefaultTask >(" packageRelease" )
115+
116+ val outputs = packageRelease.outputs.files
117+ val apks = outputs.filter { it.isDirectory }.flatMap { it.listFiles { file -> file.extension == " apk" }!! .toList() }
118+
119+ val github = GitHub .connectUsingOAuth(token)
120+ val repository = github.getRepository(repo)
121+
122+ val tagName = " ${android.namespace} -v$commitCount "
123+ val name = " ${project.name} -v$commitCount "
124+
125+ if (repository.getReleaseByTagName(tagName) != null ) {
126+ println (" Release $name already exists" )
127+ return @doFirst
128+ }
129+
130+ val release = repository.createRelease(tagName).name(name).draft(true ).create()
131+
132+ apks.forEach {
133+ release.uploadAsset(" ${project.name} -v$commitCount .apk" , it.inputStream(), " application/vnd.android.package-archive" )
134+ }
135+
136+ println (" Created release ${release.name} : ${release.htmlUrl} " )
137+ }
138+ }
139+ }
140+
76141tasks.withType<KotlinCompile > {
77142 kotlinOptions.jvmTarget = javaVersion.toString()
78143}
0 commit comments