请求协助解决:com.android.build.gradle.AppExtension获取libraryVariants属性失败
Got it, let's sort this out right away. The error you're seeing happens because you're trying to use a property that doesn't exist in the app module's Android plugin context.
Why this happens
libraryVariants is a property exclusive to Android Library modules (the ones using the com.android.library plugin). Your app module uses the com.android.application plugin, which doesn't expose this property—instead, it uses applicationVariants for the same kind of variant-related operations.
Step-by-step fixes
Check your module type first
Open your app module'sbuild.gradle(orbuild.gradle.ktsif you're using Kotlin DSL) and look at the top. You should see something like this for an app module:plugins { id 'com.android.application' // ... other plugins like kotlin-android }If that's the case,
libraryVariantswon't work here.Replace
libraryVariantswithapplicationVariants
Find wherever you're usinglibraryVariantsin your android block and swap it out. For example, if you had code like this:android { // ... your existing config (compileSdk, defaultConfig, etc.) libraryVariants.all { variant -> // Your logic for variants (e.g., signing, output naming) } }Change it to:
android { // ... your existing config applicationVariants.all { variant -> // Keep your existing logic here—just the property name changes } }If you meant to make this a library module
If you actually intended for this module to be a library (not an app), switch the plugin tocom.android.libraryand remove theapplicationIdline from yourdefaultConfigblock (library modules don't need an application ID). Your top plugin block would look like:plugins { id 'com.android.library' // ... other plugins }
After making these changes, sync your Gradle project (click the "Sync Now" prompt in Android Studio) and the error should disappear.
内容的提问来源于stack exchange,提问作者mCharles

