Skip to content

Commit

Permalink
Merge branch 'release/2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Jun 10, 2020
2 parents e725ac5 + 8d1ef72 commit 331dde7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 58 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'eu.nanogiants:android-versioning:2.1.2'
classpath 'eu.nanogiants:android-versioning:2.2.0'
}
}
```
Expand Down Expand Up @@ -45,24 +45,27 @@ Build Variant `productionStoreDebug`
```groovy
android {
defaultConfig {
archivesBaseName = "MyAppName"
archivesBaseName = "myAppName"
}
}
```
Artifacts:
```
MyAppName-production-store-3.9.0-3272-debug.apk
MyAppName-production-store-3.9.0-3272-debug.aab
myAppName-production-store-3.9.0-3272-debug.apk
myAppName-production-store-3.9.0-3272-debug.aab
myAppName-production-store-3.9.0-3272-mapping.txt
```
#### 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.
Because Android Studio does not know about the AAB renaming, the `locate` or `analyze` links in the event log and notifications will only work for APK files by default. You can set `keepOriginalArtifacts` to keep the original files. The plugin also prints the file URI for renamed artifacts.

#### Optional:
You can define a comma separated list of buildTypes (e.g. debug) to be excluded from the artifact naming.
* `excludeBuildTypes`: comma separated list of buildTypes (e.g. debug) to be excluded from the artifact naming
* `keepOriginalArtifacts`: copy artifact files instead of renaming them
```groovy
// app build.gradle
versioning {
excludeBuildTypes = "debug" {
excludeBuildTypes = "debug" // default: null
keepOriginalArtifacts = true // default: false
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "eu.nanogiants"
version = "2.1.2"
version = "2.2.0"

repositories {
google()
Expand Down
80 changes: 45 additions & 35 deletions src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package eu.nanogiants.gradle

import com.android.build.gradle.AppExtension
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
import eu.nanogiants.gradle.ext.addPrintOutputAction
import eu.nanogiants.gradle.ext.addRenameArtifactAction
import eu.nanogiants.gradle.ext.addRenameMappingAction
import eu.nanogiants.gradle.ext.generateOutputName
Expand All @@ -24,59 +25,68 @@ class VersioningPlugin : Plugin<Project> {

override fun apply(project: Project) {
with(project) {
val extension = extensions.create("versioning", VersioningPluginExtension::class.java)
val ext = extensions.create("versioning", VersioningPluginExtension::class.java)

tasks.register("printVersions") {
it.group = "Versioning"
it.description = "Prints the Android version information."
it.doLast {
extension.getVersionName()
extension.getVersionCode()
ext.getVersionName()
ext.getVersionCode()
}
}

pluginManager.withPlugin("com.android.application") {
val appExtension = extensions.getByType(AppExtension::class.java)
val baseName = convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: name
val appExt = extensions.getByType(AppExtension::class.java)

tasks.configureEach { task ->
if (task.name.matches(bundleRegex)) {
val variantName = task.name.substringAfter("bundle").decapitalize(Locale.ROOT)
appExt.applicationVariants.configureEach { variant ->
if (!ext.excludeBuildTypes.listContains(variant.buildType.name)) {
val baseName = convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: name
variant.outputs.configureEach {
val newApkName = variant.generateOutputName(baseName, "apk")
(it as BaseVariantOutputImpl).outputFileName = newApkName
}
}
}

appExtension.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val bundleName = "$baseName-${variant.baseName}.aab"
val newBundleName = variant.generateOutputName(baseName, "aab")
val bundleOutputPath = "${buildDir.absolutePath}/outputs/bundle/$variantName/"
afterEvaluate {
val baseName = convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: name
tasks.configureEach { task ->
if (task.name.matches(bundleRegex)) {
val variantName = task.name.substringAfter("bundle").decapitalize(Locale.ROOT)

task.addRenameArtifactAction(bundleName, newBundleName, bundleOutputPath)
appExt.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !ext.excludeBuildTypes.listContains(variant.buildType.name)) {
val bundleName = "$baseName-${variant.baseName}.aab"
val newBundleName = variant.generateOutputName(baseName, "aab")
val bundleOutputPath = "${buildDir.absolutePath}/outputs/bundle/${variant.name}/"

if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let {
val newMappingName = variant.generateOutputName(baseName, "txt", "aab")
task.addRenameMappingAction(it, newMappingName)
task.addRenameArtifactAction(bundleName, newBundleName, bundleOutputPath, ext.keepOriginalArtifacts)

if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let {
val newMappingName = variant.generateOutputName(baseName, "txt")
task.addRenameMappingAction(it, newMappingName, ext.keepOriginalArtifacts)
}
}
}
}
}
} else if (task.name.matches(assembleRegex)) {
val variantName = task.name.substringAfter("assemble").decapitalize(Locale.ROOT)
} 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 = "${buildDir.absolutePath}/outputs/apk/$variantName/"

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

task.addRenameArtifactAction(apkName, newApkName, apkOutputPath)
}
appExt.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !ext.excludeBuildTypes.listContains(variant.buildType.name)) {
variant.outputs.configureEach {
val apkName = (it as BaseVariantOutputImpl).outputFileName
val apkOutputPath = "${buildDir.absolutePath}/outputs/apk/$variantName/"
task.addPrintOutputAction(apkOutputPath, apkName)
}

if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let {
val newMappingName = variant.generateOutputName(baseName, "txt", "apk")
task.addRenameMappingAction(it, newMappingName)
if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let {
val newMappingName = variant.generateOutputName(baseName, "txt")
task.addRenameMappingAction(it, newMappingName, ext.keepOriginalArtifacts)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ open class VersioningPluginExtension {
fun getVersionName(checkBranch: Boolean = false) = Versioning.getVersionName(checkBranch)

var excludeBuildTypes: String? = null
var keepOriginalArtifacts: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package eu.nanogiants.gradle.ext

import com.android.build.gradle.api.ApplicationVariant

internal fun ApplicationVariant.generateOutputName(baseName: String, extension: String, suffix: String = ""): String {
internal fun ApplicationVariant.generateOutputName(baseName: String, extension: String): String {
return StringBuilder().apply {
append(baseName)
productFlavors.forEach {
Expand All @@ -20,10 +20,6 @@ internal fun ApplicationVariant.generateOutputName(baseName: String, extension:
append(versionCode.toString())
append("-")
append(buildType.name)
if (suffix.isNotEmpty()) {
append("-")
append(suffix)
}
if (extension == "apk" && !isSigningReady) {
append("-unsigned")
} else if (extension == "txt") {
Expand Down
54 changes: 44 additions & 10 deletions src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,64 @@ package eu.nanogiants.gradle.ext
import org.gradle.api.Task
import java.io.File

internal fun Task.addRenameArtifactAction(oldOutput: String, newOutput: String, outputPath: String) {
internal fun Task.addRenameArtifactAction(
oldOutput: String,
newOutput: String,
outputPath: String,
keepOriginal: Boolean
) {
println(newOutput)

doLast {
val newFile = File(outputPath + newOutput)
val success = File(outputPath + oldOutput).renameTo(newFile)
if (success) {
println("Created file://${newFile.absolutePath}")
val oldFile = File(outputPath + oldOutput)

if (oldFile.exists()) {
oldFile.copyTo(newFile, overwrite = true)
if (newFile.exists()) {
if (!keepOriginal) oldFile.delete()
println("Created file://${newFile.absolutePath}")
} else {
logger.error("Creating $newOutput from $oldOutput failed.")
}
} else {
logger.error("Renaming $oldOutput to $newOutput failed.")
if (newFile.exists()) {
println("Cached output $oldOutput was already renamed. Set 'keepOriginalArtifacts' if you like to keep those files.")
} else {
logger.error("$oldOutput and $newOutput not found. Try a clean build.")
}
}
}
}

internal fun Task.addRenameMappingAction(oldOutput: File, newOutput: String) {
internal fun Task.addRenameMappingAction(oldOutput: File, newOutput: String, keepOriginal: Boolean) {
println(newOutput)

doLast {
val newFile = File(oldOutput.absolutePath.replaceAfterLast("/", newOutput))
val success = oldOutput.renameTo(newFile)
if (success) {
println("Created file://${newFile.absolutePath}")

if (oldOutput.exists()) {
oldOutput.copyTo(newFile, overwrite = true)
if (newFile.exists()) {
if (!keepOriginal) oldOutput.delete()
println("Created $newOutput")
} else {
logger.error("Creating $newOutput from mapping.txt failed.")
}
} else {
logger.error("Renaming mapping.txt to $newOutput failed.")
if (newFile.exists()) {
println("Cached output mapping.txt was already renamed. Set 'keepOriginalArtifacts' if you like to keep those files.")
} else {
logger.error("mapping.txt and $newOutput not found. Try a clean build.")
}
}
}
}

internal fun Task.addPrintOutputAction(filepath: String, filename: String) {
println(filename)

doLast {
if (File(filepath).exists()) println("Created file://${filepath + filename}")
}
}

0 comments on commit 331dde7

Please sign in to comment.