You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助解决:com.android.build.gradle.AppExtension获取libraryVariants属性失败

Fixing "Could not get unknown property 'libraryVariants' for AppExtension" Error

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's build.gradle (or build.gradle.kts if 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, libraryVariants won't work here.

  • Replace libraryVariants with applicationVariants
    Find wherever you're using libraryVariants in 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 to com.android.library and remove the applicationId line from your defaultConfig block (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:45:21