Skip to main content
deleted 1 character in body; edited title
Source Link

What is the new way to have convention plugin for librarycommon extension in a jetpack compose project?

I have used the Convention plugin for **`ApplicationExtension`** and **`LibraryExtension`**, but the new version of **Android Gradle Plugin(AGP)** seems to be unable to support some extensions like the **Library**Common Extension**.
How can I fix this problem in the AGP 9.0.0?

What is the new way to have convention plugin for library extension in a jetpack compose project?

I have used the Convention plugin for **`ApplicationExtension`** and **`LibraryExtension`**, but the new version of **Android Gradle Plugin(AGP)** seems to be unable to support some extensions like the **Library Extension**.
How can I fix this problem in the AGP 9.0.0?

What is the new way to have convention plugin for common extension in a jetpack compose project?

I have used the Convention plugin for **`ApplicationExtension`** and **`LibraryExtension`**, but the new version of **Android Gradle Plugin(AGP)** seems to be unable to support some extensions like the **Common Extension**.
How can I fix this problem in the AGP 9.0.0?

Source Link

What is the new way to have convention plugin for library extension in a jetpack compose project?

I have used the Convention plugin for **`ApplicationExtension`** and **`LibraryExtension`**, but the new version of **Android Gradle Plugin(AGP)** seems to be unable to support some extensions like the **Library Extension**.
How can I fix this problem in the AGP 9.0.0?

class FlavorsConventionPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        project.plugins.withId("com.android.library") {
            project.extensions.configure<LibraryExtension> {
                configureFlavors(this, project)
            }
        }
        project.plugins.withId("com.android.application") {
            project.extensions.configure<ApplicationExtension> {
                configureFlavors(this, project)
            }
        }
    }
}

And this is the Flavor information file and the configureFlavors function

enum class Dimension {
    TEST
}

@Suppress("EnumEntryName")
enum class Flavor(
    val dimension: Dimension,
    val applicationIdSuffix: String? = null,
    val versionNameSuffix: String? = null
) {
    dev(
        dimension = Dimension.TEST,
        applicationIdSuffix = ".dev",
        versionNameSuffix = "-dev"
    ),
    uat(
        dimension = Dimension.TEST,
        applicationIdSuffix = ".uat",
        versionNameSuffix = "-uat"
    ),
    prod(dimension = Dimension.TEST),
}

fun configureFlavors(
    commonExtension: CommonExtension<*, *, *, *, *, *>,
    project: Project,
    flavorConfigurationBlock: ProductFlavor.(flavor: Flavor) -> Unit = {},
) {
    val props = loadFlavorProperties(project)
    commonExtension.apply {
        Dimension.entries.forEach { flavorDimension ->
            flavorDimensions += flavorDimension.name
        }

        productFlavors {
            Flavor.entries.forEach { flavor ->
                register(flavor.name) {
                    dimension = flavor.dimension.name

                    val walletUrl = props["$flavor.WALLET_BASE_URL"] as? String
                    val authUrl = props["$flavor.AUTH_BASE_URL"] as? String

                    if (this@apply is ApplicationExtension && this is ApplicationProductFlavor) {
                        if (flavor.applicationIdSuffix != null)
                            applicationIdSuffix = flavor.applicationIdSuffix

                        if (flavor.versionNameSuffix != null)
                            versionNameSuffix = flavor.versionNameSuffix
                    }
                    buildConfigField("String", "WALLET_BASE_URL", "\"$walletUrl\"")
                    buildConfigField("String", "AUTH_BASE_URL", "\"$authUrl\"")
                    buildFeatures.buildConfig = true
                    flavorConfigurationBlock(this, flavor)
                }
            }
        }
    }
}

private fun loadFlavorProperties(project: Project): Properties {
    val props = Properties()
    val file = project.rootProject.file("flavors.properties")
    if (file.exists()) {
        props.load(file.inputStream())
    } else {
        throw GradleException("Missing flavors.properties file at project root.")
    }
    return props
}