Android Studio:找不到构建变体错误

问题描述 投票:0回答:9

我是 android 开发的新手,我开始从头开始开发我在网上购买的项目,按照文档,我遇到了一个错误,说没有找到“应用程序”的变体。检查构建文件以确保至少存在一种变体。

这里是build.gradle代码

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
    applicationId "com.app-10.app"
    minSdkVersion 23
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Third Party Libraries
//Glide library for image fetching from the web
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
//Material library for styling blocks
implementation 'com.google.android.material:material:1.0.0'
// Google Gson
implementation 'com.google.code.gson:gson:2.8.5'
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
// Android-SpinKit
implementation 'com.github.ybq:Android-SpinKit:1.4.0'

implementation files('libs/YouTubeAndroidPlayerApi.jar')

// gotev/android-upload-service
implementation "net.gotev:uploadservice:3.5.2"

//A fast circular ImageView perfect for profile images
implementation 'de.hdodenhof:circleimageview:3.0.1'

implementation 'org.apache.commons:commons-io:1.3.2'

}
android android-studio build android-gradle-plugin variant
9个回答
74
投票

我刚刚解决了同样的问题:

工具 -> SDK 管理器

验证 Android 10.0 的 SDK 平台包(API 级别 29,就像您在 gradle 文件中定义的那样)被选中。 如果没有,请检查它并应用更改。接受许可条款,安装包,然后 File -> Sync Project with Gradle Files(或再次打开项目)


7
投票

就我而言,这是因为我添加了

flavourDimensions
而不是将其添加到任何
productFlavors

我的案例中的

build.gradle
应用程序级别的示例:

flavorDimensions "stage", "mode"

和我的

productFlavors

productFlavors {
    dev {
        dimension "stage"
        //noinspection DevModeObsolete
        minSdkVersion 21
        versionNameSuffix "-dev"
        applicationIdSuffix '.dev'
        resConfigs "en", "xxhdpi"
    }
    
    prod {
        dimension "stage"
        minSdkVersion 21
        versionNameSuffix "-prod"
        applicationIdSuffix '.prod'
    }
}

正如你在这里看到的,我没有在我的任何

flavorDimensions
productFlavors中使用
dimension
“模式”
。所以,当我尝试同步我的 gradle 时。它给了我那个错误。

所以我的解决方案是从

"mode"
中删除
flavorDimensions


4
投票

进入SDK管理器(Ctrl+Shift+A然后编写SDK管理器),安装当前项目的android版本,在这种情况下我安装了从5.0开始的所有可用选项


3
投票

我替换自:

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

我的 Android Studio v3.0.1:

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

但最后我通过

解决了它

您需要检测并设置合适的 sdk

我的 sdk 是 30.0.2 在我安装 29.0.2 之后这个错误消失了


2
投票

可能会帮助某人,就我而言,我只需要更新 Gradle。

Android Studio 弹出一个警告,所以我更新了它,然后正常工作🤷🏻u200d♀️


2
投票

对我来说,问题是我没有在项目的根文件夹中打开项目,而是打开了“app”文件夹。这两个文件夹都有 gradle 图标,所以在一段时间没有进行任何 Android 开发后很容易犯这个错误。


1
投票

当构建类型在模块之间不匹配时,您可能会在具有多个模块的项目中收到“未找到变体”错误。

就我而言,这是在尝试向现有 Android 项目添加 macrobenchmark 时发生的。 按照本文档中的说明,我向项目添加了一个新模块

macrobenchmark
。 在某些时候,我误解了指令:我在模块
benchmark
build.gradle
中添加了一个新的构建类型
app
,但没有在模块
build.gradle
macrobenchmark
中添加相同的构建类型。 从那时起,任何 将项目与 gradle 文件同步 的尝试都会失败,并显示以下错误消息:

找不到“macrobenchmark”的变体。检查构建文件以确保至少存在一种变体。

解决方案很简单(但并非微不足道):使构建类型恢复同步。在我的例子中,将缺少的构建类型

benchmark
添加到模块
build.gradle
macrobenchmark
中。


0
投票

尝试

npm i [email protected]

0
投票

我有同样的错误,在 AGP 升级后,通过在活动 ABI 中选择 arm64 将其出售:build variant screenshot

不知道为什么在 AGP 升级后出现在“x86”上。

© www.soinside.com 2019 - 2024. All rights reserved.