Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Jun 7, 2020
2 parents a8d2151 + c62e851 commit 850a273
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 53 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Android Versioning Gradle Plugin [ ![Download](https://api.bintray.com/packages/appcom-interactive/android/android-versioning/images/download.svg) ](https://bintray.com/appcom-interactive/android/android-versioning/_latestVersion)

This plugin automatically generates your Android versionName and versionCode using Git. It also appends the version and variant names to your ABB/APK artifacts.
This plugin automatically generates your Android versionName and versionCode using Git. It also appends the version and variant names to your APK/AAB and obfuscation mapping artifacts.

## Usage

Expand All @@ -12,7 +12,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'eu.nanogiants:android-versioning:2.0.1'
classpath 'eu.nanogiants:android-versioning:2.1.0'
}
}
```
Expand All @@ -30,20 +30,17 @@ android {
}
}
```
**Use the plugin by referencing the versioning extension.**

* `getVersionCode()` returns the current Git commit count
* `getVersionName()` returns the latest Git tag of your repository
#### Use the plugin by referencing the versioning extension.
* `versioning.getVersionCode()` returns the current Git commit count
* `versioning.getVersionName()` returns the latest Git tag

#### Optional:
* `getVersionName(checkBranch: Boolean)` if `checkBranch` is set to *true* the plugin will check if the current branch is `release/x.x.x` or `hotfix/x.x.x` and use the naming instead the latest tag.
* `versioning.getVersionName(checkBranch: Boolean)` if `checkBranch` is set to *true* the plugin will check if the current branch is `release/x.x.x` or `hotfix/x.x.x` and use the branch name instead the latest tag.

### Artifact naming

The plugin will automatically set the APK and AAB naming for all assemble and bundle tasks. This will also work if you do not use the versioning extension in the defaultConfig. You can still use the default `archivesBaseName` property.
The plugin will automatically rename APK, AAB and Mapping.txt files for all assemble and bundle tasks. This will also work if you do not use the versioning extension in the defaultConfig. You can still use the default `archivesBaseName` property.

#### Example:

Build Variant `productionStoreDebug`
```groovy
android {
Expand All @@ -57,9 +54,13 @@ Artifacts:
MyAppName-production-store-3.9.0-3272-debug.apk
MyAppName-production-store-3.9.0-3272-debug.aab
```
#### Note:
Since Android Studio does not know about the artifact renaming, the `locate` or `analyze` links in the event log and notifications will stop working. To compensate that, the plugin prints file URI for every renamed artifact.

#### Optional:
You can define a comma separated list of buildTypes (e.g. debug) be excluded from the artifact naming.
You can define a comma separated list of buildTypes (e.g. debug) to be excluded from the artifact naming.
```groovy
// app build.gradle
versioning {
excludeBuildTypes = "debug" {
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "eu.nanogiants"
version = "2.0.1"
version = "2.1.0"

repositories {
google()
Expand Down
91 changes: 50 additions & 41 deletions src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
import com.android.build.gradle.internal.dsl.DefaultConfig
import eu.nanogiants.gradle.ext.addRenameArtifactAction
import eu.nanogiants.gradle.ext.addRenameMappingAction
import eu.nanogiants.gradle.ext.listContains
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.plugins.BasePluginConvention
import java.io.File
import java.util.Locale

@ExperimentalStdlibApi
Expand Down Expand Up @@ -43,44 +44,41 @@ class VersioningPlugin : Plugin<Project> {
}
}

project.tasks.all { task ->
if (task.name.matches(bundleRegex)) {
val variantName = task.name.substringAfter("bundle").decapitalize(Locale.ROOT)

appExtension.applicationVariants.all { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val artifactBaseName = project.convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName
?: project.name
val bundleName = "$artifactBaseName-${variant.baseName}.aab"
val bundleOutputPath = "${project.buildDir.absolutePath}/outputs/bundle/$variantName/"
val newOutputName = getOutputName(artifactBaseName, variant, appExtension.defaultConfig, "aab")
println(newOutputName)

val renameAabTask =
project.tasks.register("versioningPluginRename${variant.name.capitalize(Locale.ROOT)}Aab") {
it.doLast {
val success = File(bundleOutputPath + bundleName).renameTo(File(bundleOutputPath + newOutputName))
if (success) {
println("Renamed $bundleName to $newOutputName")
} else {
project.logger.error("Renaming $bundleName to $newOutputName failed.")
}
}
}
task.finalizedBy(renameAabTask)
project.afterEvaluate {
val baseName = project.convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: project.name

project.tasks.configureEach { task ->
if (task.name.matches(bundleRegex)) {
val variantName = task.name.substringAfter("bundle").decapitalize(Locale.ROOT)

appExtension.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val bundleName = "$baseName-${variant.baseName}.aab"
val newBundleName = getOutputName(baseName, variant, appExtension.defaultConfig, "aab")
val bundleOutputPath = "${project.buildDir.absolutePath}/outputs/bundle/$variantName/"

task.addRenameArtifactAction(project, bundleName, newBundleName, bundleOutputPath)

val newMappingName = getOutputName(baseName, variant, appExtension.defaultConfig, "txt", "aab")
task.addRenameMappingAction(project, variant, newMappingName)
}
}
}
} else if (task.name.matches(assembleRegex)) {
val variantName = task.name.substringAfter("assemble").decapitalize(Locale.ROOT)

appExtension.applicationVariants.all { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val artifactBaseName = project.convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName
?: project.name
val newOutputName = getOutputName(artifactBaseName, variant, appExtension.defaultConfig, "apk")
println(newOutputName)
variant.outputs.all {
(it as BaseVariantOutputImpl).outputFileName = newOutputName
} else if (task.name.matches(assembleRegex)) {
val variantName = task.name.substringAfter("assemble").decapitalize(Locale.ROOT)

appExtension.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val apkOutputPath = "${project.buildDir.absolutePath}/outputs/apk/$variantName/"

variant.outputs.configureEach {
val apkName = (it as BaseVariantOutputImpl).outputFileName
val newApkName = getOutputName(baseName, variant, appExtension.defaultConfig, "apk")

task.addRenameArtifactAction(project, apkName, newApkName, apkOutputPath)
}

val newMappingName = getOutputName(baseName, variant, appExtension.defaultConfig, "txt", "apk")
task.addRenameMappingAction(project, variant, newMappingName)
}
}
}
Expand All @@ -92,7 +90,8 @@ class VersioningPlugin : Plugin<Project> {
artifactBaseName: String,
variant: ApplicationVariant,
defaultConfig: DefaultConfig,
extension: String
extension: String,
suffix: String = ""
): String {
return StringBuilder().apply {
append(artifactBaseName)
Expand All @@ -106,8 +105,18 @@ class VersioningPlugin : Plugin<Project> {
append(defaultConfig.versionCode.toString())
append("-")
append(variant.buildType.name)
append(".")
append(extension)
if (suffix.isNotEmpty()) {
append("-")
append(suffix)
}
if (extension == "apk" && !variant.isSigningReady) {
append("-unsigned.apk")
} else if (extension == "txt") {
append("-mapping.txt")
} else {
append(".")
append(extension)
}
}.toString()
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/eu/nanogiants/gradle/ext/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Created by NanoGiants GmbH on 06-06-2020.
* Copyright © 2020 NanoGiants GmbH. All rights reserved.
*/

package eu.nanogiants.gradle.ext

import java.io.BufferedReader
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Created by NanoGiants GmbH on 06-06-2020.
* Copyright © 2020 NanoGiants GmbH. All rights reserved.
*/

package eu.nanogiants.gradle.ext

import com.android.build.gradle.api.ApplicationVariant
import org.gradle.api.Project
import org.gradle.api.Task
import java.io.File

internal fun Task.addRenameArtifactAction(project: Project, oldOutput: String, newOutput: String, outputPath: String) {
println(newOutput)

doLast {
val newFile = File(outputPath + newOutput)
val success = File(outputPath + oldOutput).renameTo(newFile)
if (success) {
println("Created file://${newFile.absolutePath}")
} else {
project.logger.error("Renaming $oldOutput to $newOutput failed.")
}
}
}

internal fun Task.addRenameMappingAction(project: Project, variant: ApplicationVariant, newOutput: String) {
if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let { mappingFile ->
println(newOutput)

doLast {
val newFile = File(mappingFile.absolutePath.replaceAfterLast("/", newOutput))
val success = mappingFile.renameTo(newFile)
if (success) {
println("Created file://${newFile.absolutePath}")
} else {
project.logger.error("Renaming mapping.txt to $newOutput failed.")
}
}
}
}
}

0 comments on commit 850a273

Please sign in to comment.