7

I'm having trouble using Catalog Version in gradle, which I'm trying to apply to my project. I took an example from here https://github.com/android/nowinandroid but when I get an error like in :

Extension of type 'LibraryExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, LibrariesForLibs, VersionCatalogsExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaPluginExtension, JavaToolchainService, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, ApplicationAndroidComponentsExtension, KotlinAndroidProjectExtension, KotlinTestsRegistry]
    at org.gradle.internal.extensibility.ExtensionsStorage.getHolderByType(ExtensionsStorage.java:88)
    at org.gradle.internal.extensibility.ExtensionsStorage.configureExtension(ExtensionsStorage.java:70)
    at org.gradle.internal.extensibility.DefaultConvention.configure(DefaultConvention.java:189)
    at AndroidLibraryConventionPlugin.apply(AndroidLibraryConventionPlugin.kt:30)
    at AndroidLibraryConventionPlugin.apply(AndroidLibraryConventionPlugin.kt:10)

The code I'm trying to implement is like this:

internal fun Project.configureKotlinAndroid(
    commonExtension: CommonExtension<*, *, *, *>,
) {
    commonExtension.apply {
        compileSdk = 32

        defaultConfig {
            minSdk = 21
        }

        compileOptions {
            compileOptions {
                sourceCompatibility = JavaVersion.VERSION_1_8
                targetCompatibility = JavaVersion.VERSION_1_8
            }
        }

        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    }

    val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

    dependencies {

    }
}

and after that it is called here

class AndroidLibraryConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                apply("com.android.application")
                apply("org.jetbrains.kotlin.android")
            }

            extensions.configure<LibraryExtension> {
                configureKotlinAndroid(this)
                defaultConfig.targetSdk = 32
            }

            val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
            dependencies {

            }
        }
    }
}

1 Answers1

1

you need to add gradle library into your buildSrc gradle file dependencies:

compileOnly("com.android.tools.build:gradle:7.4.0")

then it will import LibraryExtension class

Özgür
  • 71
  • 7
  • This answer is somewhat correct. Just to add some more detail, in your build-logic convention module's gradle, you need to set the gradle dependecy: ``` dependencies { compileOnly(libs.android.gradlePlugin) }``` – willieswanjala Apr 26 '23 at 15:21