android-plugins: convert to full plugins

This commit is contained in:
Harsh Shandilya 2022-06-04 15:24:46 +05:30
parent fee7510496
commit 504c8b466c
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
22 changed files with 299 additions and 228 deletions

View file

@ -28,7 +28,27 @@ gradlePlugin {
plugins {
register("versioning") {
id = "com.github.android-password-store.versioning-plugin"
implementationClass = "versioning.VersioningPlugin"
implementationClass = "dev.msfjarvis.aps.gradle.versioning.VersioningPlugin"
}
register("android-application") {
id = "com.github.android-password-store.android-application"
implementationClass = "dev.msfjarvis.aps.gradle.ApplicationPlugin"
}
register("android-library") {
id = "com.github.android-password-store.android-library"
implementationClass = "dev.msfjarvis.aps.gradle.LibraryPlugin"
}
register("published-android-library") {
id = "com.github.android-password-store.published-android-library"
implementationClass = "dev.msfjarvis.aps.gradle.PublishedAndroidLibraryPlugin"
}
register("rename-artifacts") {
id = "com.github.android-password-store.rename-artifacts"
implementationClass = "dev.msfjarvis.aps.gradle.RenameArtifactsPlugin"
}
register("sentry") {
id = "com.github.android-password-store.sentry"
implementationClass = "dev.msfjarvis.aps.gradle.SentryPlugin"
}
}
}
@ -47,7 +67,7 @@ configurations.all {
val overridenVersion = System.getenv(overrideName)
if (!overridenVersion.isNullOrEmpty()) {
project.logger.lifecycle(
"Overriding dependency ${requested.group}:${requested.name} to version ${overridenVersion}"
"Overriding dependency ${requested.group}:${requested.name} to version $overridenVersion"
)
useVersion(overridenVersion)
}

View file

@ -1,72 +0,0 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
@file:Suppress("UnstableApiUsage")
import flavors.FlavorDimensions
import flavors.ProductFlavors
import signing.configureBuildSigning
import snapshot.SnapshotExtension
plugins {
id("com.android.application")
id("com.github.android-password-store.android-common")
}
fun Project.isSnapshot(): Boolean {
with(project.providers) {
val workflow = environmentVariable("GITHUB_WORKFLOW")
val snapshot = environmentVariable("SNAPSHOT")
return workflow.isPresent || snapshot.isPresent
}
}
android {
val minifySwitch = project.providers.environmentVariable("DISABLE_MINIFY")
adbOptions.installOptions("--user 0")
dependenciesInfo {
includeInBundle = false
includeInApk = false
}
buildFeatures {
viewBinding = true
buildConfig = true
}
buildTypes {
named("release") {
isMinifyEnabled = !minifySwitch.isPresent
setProguardFiles(
listOf(
"proguard-android-optimize.txt",
"proguard-rules.pro",
"proguard-rules-missing-classes.pro",
)
)
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}")
}
named("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
isMinifyEnabled = false
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "true")
}
}
flavorDimensions.add(FlavorDimensions.FREE)
productFlavors {
register(ProductFlavors.FREE) {}
register(ProductFlavors.NON_FREE) {}
}
project.configureBuildSigning()
}
dependencies {
extensions.add("snapshot", SnapshotExtension::class.java)
the<SnapshotExtension>().snapshot = isSnapshot()
}

View file

@ -1,43 +0,0 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
@file:Suppress("UnstableApiUsage")
import com.android.build.gradle.TestedExtension
import flavors.configureSlimTests
extensions.configure<TestedExtension> {
setCompileSdkVersion(31)
defaultConfig {
minSdk = 23
targetSdk = 31
}
sourceSets {
named("main") { java.srcDirs("src/main/kotlin") }
named("test") { java.srcDirs("src/test/kotlin") }
named("androidTest") { java.srcDirs("src/androidTest/kotlin") }
}
packagingOptions {
resources.excludes.add("**/*.version")
resources.excludes.add("**/*.txt")
resources.excludes.add("**/*.kotlin_module")
resources.excludes.add("**/plugin.properties")
resources.excludes.add("**/META-INF/AL2.0")
resources.excludes.add("**/META-INF/LGPL2.1")
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
testOptions {
animationsDisabled = true
unitTests.isReturnDefaultValues = true
}
configureSlimTests()
}

View file

@ -1,9 +0,0 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
plugins {
id("com.android.library")
id("com.github.android-password-store.android-common")
}

View file

@ -1,34 +0,0 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.kotlin.dsl.provideDelegate
plugins {
id("com.github.android-password-store.android-library")
id("com.vanniktech.maven.publish.base")
id("signing")
}
group = requireNotNull(project.findProperty("GROUP"))
version = requireNotNull(project.findProperty("VERSION_NAME"))
@Suppress("UnstableApiUsage")
mavenPublishing {
publishToMavenCentral(SonatypeHost.DEFAULT)
signAllPublications()
configure(AndroidSingleVariantLibrary())
pomFromGradleProperties()
}
afterEvaluate {
signing {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
}
}

View file

@ -1,22 +0,0 @@
import artifacts.CollectApksTask
import artifacts.CollectBundleTask
import com.android.build.api.artifact.SingleArtifact
plugins { id("com.android.application") }
androidComponents {
onVariants { variant ->
project.tasks.register<CollectApksTask>("collect${variant.name.capitalize()}Apks") {
variantName.set(variant.name)
apkFolder.set(variant.artifacts.get(SingleArtifact.APK))
builtArtifactsLoader.set(variant.artifacts.getBuiltArtifactsLoader())
outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
}
project.tasks.register<CollectBundleTask>("collect${variant.name.capitalize()}Bundle") {
variantName.set(variant.name)
versionName.set(android.defaultConfig.versionName)
bundleFile.set(variant.artifacts.get(SingleArtifact.BUNDLE))
outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
}
}
}

View file

@ -1,37 +0,0 @@
@file:Suppress("PropertyName")
import flavors.FlavorDimensions
import flavors.ProductFlavors
import io.sentry.android.gradle.extensions.InstrumentationFeature
plugins {
id("com.android.application")
id("io.sentry.android.gradle")
}
val SENTRY_DSN_PROPERTY = "SENTRY_DSN"
val SENTRY_UPLOAD_MAPPINGS_PROPERTY = "sentryUploadMappings"
android {
androidComponents {
onVariants(selector().withFlavor(FlavorDimensions.FREE to ProductFlavors.NON_FREE)) { variant ->
val sentryDsn = project.providers.environmentVariable(SENTRY_DSN_PROPERTY)
if (sentryDsn.isPresent) {
variant.manifestPlaceholders.put("sentryDsn", sentryDsn.get())
}
}
}
}
sentry {
autoUploadProguardMapping.set(
project.providers.gradleProperty(SENTRY_UPLOAD_MAPPINGS_PROPERTY).isPresent
)
ignoredBuildTypes.set(setOf("debug"))
ignoredFlavors.set(setOf(ProductFlavors.FREE))
tracingInstrumentation {
enabled.set(true)
features.set(setOf(InstrumentationFeature.FILE_IO))
}
autoInstallation.enabled.set(false)
}

View file

@ -0,0 +1,47 @@
package dev.msfjarvis.aps.gradle
import com.android.build.gradle.TestedExtension
import dev.msfjarvis.aps.gradle.flavors.configureSlimTests
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
@Suppress("UnstableApiUsage")
object AndroidCommon {
fun configure(project: Project) {
project.extensions.configure<TestedExtension> {
setCompileSdkVersion(31)
defaultConfig {
minSdk = 23
targetSdk = 31
}
sourceSets {
named("main") { java.srcDirs("src/main/kotlin") }
named("test") { java.srcDirs("src/test/kotlin") }
named("androidTest") { java.srcDirs("src/androidTest/kotlin") }
}
packagingOptions {
resources.excludes.add("**/*.version")
resources.excludes.add("**/*.txt")
resources.excludes.add("**/*.kotlin_module")
resources.excludes.add("**/plugin.properties")
resources.excludes.add("**/META-INF/AL2.0")
resources.excludes.add("**/META-INF/LGPL2.1")
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
testOptions {
animationsDisabled = true
unitTests.isReturnDefaultValues = true
}
project.configureSlimTests()
}
}
}

View file

@ -0,0 +1,81 @@
@file:Suppress("UnstableApiUsage")
package dev.msfjarvis.aps.gradle
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import dev.msfjarvis.aps.gradle.flavors.FlavorDimensions
import dev.msfjarvis.aps.gradle.flavors.ProductFlavors
import dev.msfjarvis.aps.gradle.signing.configureBuildSigning
import dev.msfjarvis.aps.gradle.snapshot.SnapshotExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.the
@Suppress("Unused")
class ApplicationPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.apply(AppPlugin::class)
AndroidCommon.configure(project)
project.extensions.getByType<BaseAppModuleExtension>().run {
val minifySwitch = project.providers.environmentVariable("DISABLE_MINIFY")
adbOptions.installOptions("--user 0")
dependenciesInfo {
includeInBundle = false
includeInApk = false
}
buildFeatures {
viewBinding = true
buildConfig = true
}
buildTypes {
named("release") {
isMinifyEnabled = !minifySwitch.isPresent
setProguardFiles(
listOf(
"proguard-android-optimize.txt",
"proguard-rules.pro",
"proguard-rules-missing-classes.pro",
)
)
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}")
}
named("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
isMinifyEnabled = false
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "true")
}
}
flavorDimensions.add(FlavorDimensions.FREE)
productFlavors {
register(ProductFlavors.FREE) {}
register(ProductFlavors.NON_FREE) {}
}
project.configureBuildSigning()
}
project.dependencies {
extensions.add("snapshot", SnapshotExtension::class.java)
the<SnapshotExtension>().snapshot = project.isSnapshot()
}
}
private fun Project.isSnapshot(): Boolean {
with(project.providers) {
val workflow = environmentVariable("GITHUB_WORKFLOW")
val snapshot = environmentVariable("SNAPSHOT")
return workflow.isPresent || snapshot.isPresent
}
}
}

View file

@ -0,0 +1,15 @@
package dev.msfjarvis.aps.gradle
import com.android.build.gradle.LibraryPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
@Suppress("Unused")
class LibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.apply(LibraryPlugin::class)
AndroidCommon.configure(project)
}
}

View file

@ -0,0 +1,40 @@
@file:Suppress("UnstableApiUsage")
package dev.msfjarvis.aps.gradle
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import com.vanniktech.maven.publish.MavenPublishBasePlugin
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.plugins.signing.SigningExtension
import org.gradle.plugins.signing.SigningPlugin
@Suppress("Unused")
class PublishedAndroidLibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.run {
apply(LibraryPlugin::class)
apply(MavenPublishBasePlugin::class)
apply(SigningPlugin::class)
}
project.extensions.getByType<MavenPublishBaseExtension>().run {
publishToMavenCentral(SonatypeHost.DEFAULT)
signAllPublications()
configure(AndroidSingleVariantLibrary())
pomFromGradleProperties()
}
project.afterEvaluate {
project.extensions.getByType<SigningExtension>().run {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
}
}
}
}

View file

@ -0,0 +1,37 @@
package dev.msfjarvis.aps.gradle
import com.android.build.api.artifact.SingleArtifact
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import dev.msfjarvis.aps.gradle.artifacts.CollectApksTask
import dev.msfjarvis.aps.gradle.artifacts.CollectBundleTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
@Suppress("Unused")
class RenameArtifactsPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.withPlugin("com.android.application") {
val android = project.extensions.getByType<BaseAppModuleExtension>()
project.extensions.getByType<ApplicationAndroidComponentsExtension>().run {
onVariants { variant ->
project.tasks.register<CollectApksTask>("collect${variant.name.capitalize()}Apks") {
variantName.set(variant.name)
apkFolder.set(variant.artifacts.get(SingleArtifact.APK))
builtArtifactsLoader.set(variant.artifacts.getBuiltArtifactsLoader())
outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
}
project.tasks.register<CollectBundleTask>("collect${variant.name.capitalize()}Bundle") {
variantName.set(variant.name)
versionName.set(android.defaultConfig.versionName)
bundleFile.set(variant.artifacts.get(SingleArtifact.BUNDLE))
outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
}
}
}
}
}
}

View file

@ -0,0 +1,48 @@
package dev.msfjarvis.aps.gradle
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import dev.msfjarvis.aps.gradle.flavors.FlavorDimensions
import dev.msfjarvis.aps.gradle.flavors.ProductFlavors
import io.sentry.android.gradle.SentryPlugin
import io.sentry.android.gradle.extensions.InstrumentationFeature
import io.sentry.android.gradle.extensions.SentryPluginExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType
@Suppress("Unused")
class SentryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.withPlugin("com.android.application") {
project.extensions.getByType<ApplicationAndroidComponentsExtension>().run {
onVariants(selector().withFlavor(FlavorDimensions.FREE to ProductFlavors.NON_FREE)) {
variant ->
val sentryDsn = project.providers.environmentVariable(SENTRY_DSN_PROPERTY)
if (sentryDsn.isPresent) {
variant.manifestPlaceholders.put("sentryDsn", sentryDsn.get())
}
}
}
project.plugins.apply(SentryPlugin::class)
project.extensions.getByType<SentryPluginExtension>().run {
autoUploadProguardMapping.set(
project.providers.gradleProperty(SENTRY_UPLOAD_MAPPINGS_PROPERTY).isPresent
)
ignoredBuildTypes.set(setOf("debug"))
ignoredFlavors.set(setOf(ProductFlavors.FREE))
tracingInstrumentation {
enabled.set(true)
features.set(setOf(InstrumentationFeature.FILE_IO))
}
autoInstallation.enabled.set(false)
}
}
}
private companion object {
private const val SENTRY_DSN_PROPERTY = "SENTRY_DSN"
private const val SENTRY_UPLOAD_MAPPINGS_PROPERTY = "sentryUploadMappings"
}
}

View file

@ -1,4 +1,4 @@
package artifacts
package dev.msfjarvis.aps.gradle.artifacts
import com.android.build.api.variant.BuiltArtifactsLoader
import java.nio.file.Files

View file

@ -1,4 +1,4 @@
package artifacts
package dev.msfjarvis.aps.gradle.artifacts
import java.nio.file.Files
import java.nio.file.StandardCopyOption

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package flavors
package dev.msfjarvis.aps.gradle.flavors
object FlavorDimensions {
const val FREE = "free"

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package flavors
package dev.msfjarvis.aps.gradle.flavors
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package signing
package dev.msfjarvis.aps.gradle.signing
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import java.util.Properties

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package snapshot
package dev.msfjarvis.aps.gradle.snapshot
abstract class SnapshotExtension {
abstract var snapshot: Boolean

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package versioning
package dev.msfjarvis.aps.gradle.versioning
const val VERSIONING_PROP_FILE = "version.properties"
const val VERSIONING_PROP_VERSION_NAME = "versioning-plugin.versionName"

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package versioning
package dev.msfjarvis.aps.gradle.versioning
import com.android.build.gradle.internal.plugins.AppPlugin
import com.vdurmont.semver4j.Semver

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
package versioning
package dev.msfjarvis.aps.gradle.versioning
import com.vdurmont.semver4j.Semver
import org.gradle.api.DefaultTask