diff --git a/build.gradle.kts b/build.gradle.kts index 0bcb28a..eb186cb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "eu.nanogiants" -version = "2.1.1" +version = "2.1.2" repositories { google() @@ -16,7 +16,7 @@ repositories { } dependencies { - implementation("com.android.tools.build:gradle:4.0.0") + compileOnly("com.android.tools.build:gradle:4.0.0") } tasks { diff --git a/src/main/kotlin/eu/nanogiants/gradle/Versioning.kt b/src/main/kotlin/eu/nanogiants/gradle/Versioning.kt index 00ac780..22edd62 100644 --- a/src/main/kotlin/eu/nanogiants/gradle/Versioning.kt +++ b/src/main/kotlin/eu/nanogiants/gradle/Versioning.kt @@ -12,7 +12,7 @@ object Versioning { internal fun getVersionCode() = getCommitCount().apply { println("VersionCode $this") } - internal fun getVersionName(checkBranch: Boolean = false): String { + internal fun getVersionName(checkBranch: Boolean): String { return (if (checkBranch) getBranchNameOrTag() else getTag()).apply { println("VersionName $this") } } diff --git a/src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt b/src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt index 19c01a6..517e395 100644 --- a/src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt +++ b/src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt @@ -11,10 +11,8 @@ import eu.nanogiants.gradle.ext.addRenameArtifactAction import eu.nanogiants.gradle.ext.addRenameMappingAction import eu.nanogiants.gradle.ext.generateOutputName 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.util.Locale @@ -25,59 +23,63 @@ class VersioningPlugin : Plugin { private val assembleRegex = "^(:.*:)*assemble.*".toRegex() override fun apply(project: Project) { - val appExtension: AppExtension - try { - appExtension = project.extensions.getByType(AppExtension::class.java) - } catch (e: UnknownDomainObjectException) { - throw GradleException("Project '${project.name}' is not an Android module. Can't access 'android' extension.") - } - - val extension = project.extensions.create("versioning", VersioningPluginExtension::class.java) - - project.tasks.register("printVersions") { - it.group = "Versioning" - it.description = "Prints the Android version information." - it.doLast { - extension.getVersionName() - extension.getVersionCode() + with(project) { + val extension = 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() + } } - } - project.afterEvaluate { - val baseName = project.convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: project.name + pluginManager.withPlugin("com.android.application") { + val appExtension = extensions.getByType(AppExtension::class.java) + val baseName = convention.findPlugin(BasePluginConvention::class.java)?.archivesBaseName ?: name - project.tasks.configureEach { task -> - if (task.name.matches(bundleRegex)) { - val variantName = task.name.substringAfter("bundle").decapitalize(Locale.ROOT) + 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 = variant.generateOutputName(baseName, "aab") - val bundleOutputPath = "${project.buildDir.absolutePath}/outputs/bundle/$variantName/" + 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/" - task.addRenameArtifactAction(bundleName, newBundleName, bundleOutputPath) + task.addRenameArtifactAction(bundleName, newBundleName, bundleOutputPath) - val newMappingName = variant.generateOutputName(baseName, "txt", "aab") - task.addRenameMappingAction(variant, newMappingName) + if (variant.buildType.isMinifyEnabled) { + variant.mappingFileProvider.orNull?.firstOrNull()?.let { + val newMappingName = variant.generateOutputName(baseName, "txt", "aab") + task.addRenameMappingAction(it, newMappingName) + } + } + } } - } - } 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 = variant.generateOutputName(baseName, "apk") - - task.addRenameArtifactAction(apkName, newApkName, apkOutputPath) + } 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) + } + + if (variant.buildType.isMinifyEnabled) { + variant.mappingFileProvider.orNull?.firstOrNull()?.let { + val newMappingName = variant.generateOutputName(baseName, "txt", "apk") + task.addRenameMappingAction(it, newMappingName) + } + } } - - val newMappingName = variant.generateOutputName(baseName, "txt", "apk") - task.addRenameMappingAction(variant, newMappingName) } } } diff --git a/src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt b/src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt index d9a171a..7293daf 100644 --- a/src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt +++ b/src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt @@ -5,7 +5,6 @@ package eu.nanogiants.gradle.ext -import com.android.build.gradle.api.ApplicationVariant import org.gradle.api.Task import java.io.File @@ -23,20 +22,16 @@ internal fun Task.addRenameArtifactAction(oldOutput: String, newOutput: String, } } -internal fun Task.addRenameMappingAction(variant: ApplicationVariant, newOutput: String) { - if (variant.buildType.isMinifyEnabled) { - variant.mappingFileProvider.orNull?.firstOrNull()?.let { mappingFile -> - println(newOutput) +internal fun Task.addRenameMappingAction(oldOutput: File, newOutput: String) { + println(newOutput) - doLast { - val newFile = File(mappingFile.absolutePath.replaceAfterLast("/", newOutput)) - val success = mappingFile.renameTo(newFile) - if (success) { - println("Created file://${newFile.absolutePath}") - } else { - logger.error("Renaming mapping.txt to $newOutput failed.") - } - } + doLast { + val newFile = File(oldOutput.absolutePath.replaceAfterLast("/", newOutput)) + val success = oldOutput.renameTo(newFile) + if (success) { + println("Created file://${newFile.absolutePath}") + } else { + logger.error("Renaming mapping.txt to $newOutput failed.") } } } \ No newline at end of file