Created
August 7, 2025 11:02
-
-
Save Pratham-Sortly/35827bcfa4b9dc302aee34d078da7199 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.android.build.api.variant.ApplicationAndroidComponentsExtension | |
| import com.android.build.gradle.internal.dsl.BaseAppModuleExtension | |
| import extension.getLibsFromVersionCatalog | |
| import org.gradle.api.JavaVersion | |
| import org.gradle.api.Plugin | |
| import org.gradle.api.Project | |
| import org.gradle.api.artifacts.VersionCatalog | |
| import org.gradle.kotlin.dsl.configure | |
| class AppLevelPlugin : Plugin<Project> { | |
| override fun apply(project: Project) = with(project) { | |
| val libs = getLibsFromVersionCatalog() | |
| configurePlugins(libs) | |
| extensions.configure<BaseAppModuleExtension> { | |
| addSdkAndVersion(libs) | |
| createFlavours() | |
| addBuildFeatures() | |
| createBuildTypes() | |
| addCompileOptions() | |
| addLintSettings() | |
| addTestOptions() | |
| } | |
| extensions.configure<ApplicationAndroidComponentsExtension> { | |
| configureIgnoredVariants() | |
| } | |
| } | |
| private fun Project.configurePlugins(libs: VersionCatalog) { | |
| plugins.apply(libs.findPlugin("android-application").get().get().pluginId) | |
| plugins.apply(libs.findPlugin("kotlin-android").get().get().pluginId) | |
| plugins.apply(libs.findPlugin("kotlin-compose").get().get().pluginId) | |
| } | |
| private fun BaseAppModuleExtension.addSdkAndVersion(libs: VersionCatalog) { | |
| compileSdk = libs.findVersion("android.compileSdk").get().requiredVersion.toInt() | |
| defaultConfig { | |
| minSdk = libs.findVersion("android.minSdk").get().requiredVersion.toInt() | |
| targetSdk = libs.findVersion("android.targetSdk").get().requiredVersion.toInt() | |
| targetSdkPreview = | |
| libs.findVersion("android.targetSdk").get().requiredVersion.toString() | |
| versionCode = libs.findVersion("app.version.code").get().requiredVersion.toInt() | |
| versionName = libs.findVersion("app.version.name").get().requiredVersion | |
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | |
| } | |
| } | |
| private fun BaseAppModuleExtension.createFlavours() { | |
| flavorDimensions += "version" | |
| productFlavors { | |
| create("dev") { | |
| applicationIdSuffix = ".dev" | |
| versionNameSuffix = "-dev" | |
| } | |
| create("qa") { | |
| applicationIdSuffix = ".qa" | |
| versionNameSuffix = "-qa" | |
| } | |
| create("prod") { | |
| applicationIdSuffix = "" | |
| } | |
| } | |
| } | |
| private fun BaseAppModuleExtension.addBuildFeatures() { | |
| buildFeatures { | |
| buildConfig = true | |
| compose = true | |
| } | |
| } | |
| private fun BaseAppModuleExtension.createBuildTypes() { | |
| buildTypes { | |
| create("staging") { | |
| isDebuggable = true | |
| isShrinkResources = false | |
| isMinifyEnabled = false | |
| buildConfigField("boolean", "ENABLE_ANALYICS", "false") | |
| } | |
| getByName("release") { | |
| isDebuggable = false | |
| isShrinkResources = true | |
| isMinifyEnabled = true | |
| buildConfigField("boolean", "ENABLE_ANALYICS", "true") | |
| proguardFiles( | |
| getDefaultProguardFile("proguard-android-optimize.txt"), | |
| "proguard-rules.pro" | |
| ) | |
| } | |
| } | |
| } | |
| private fun BaseAppModuleExtension.addCompileOptions() { | |
| compileOptions { | |
| sourceCompatibility = JavaVersion.VERSION_11 | |
| targetCompatibility = JavaVersion.VERSION_11 | |
| } | |
| } | |
| private fun BaseAppModuleExtension.addLintSettings() { | |
| lint { | |
| showAll = true | |
| abortOnError = false | |
| } | |
| } | |
| private fun BaseAppModuleExtension.addTestOptions() { | |
| testOptions { | |
| unitTests.isReturnDefaultValues = true | |
| } | |
| } | |
| private fun ApplicationAndroidComponentsExtension.configureIgnoredVariants() { | |
| beforeVariants { variantBuilder -> | |
| if (variantBuilder.productFlavors.contains("version" to "dev") && | |
| variantBuilder.buildType == "release") { | |
| variantBuilder.enable = false | |
| } | |
| if (variantBuilder.productFlavors.contains("version" to "qa") && | |
| variantBuilder.buildType == "release") { | |
| variantBuilder.enable = false | |
| } | |
| if (variantBuilder.productFlavors.contains("version" to "prod") && | |
| variantBuilder.buildType == "debug") { | |
| variantBuilder.enable = false | |
| } | |
| if (variantBuilder.productFlavors.contains("version" to "prod") && | |
| variantBuilder.buildType == "staging") { | |
| variantBuilder.enable = false | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment