Android Studio大黄蜂版本报错:找不到android()方法求助
问题解决:Could not find method android() 错误
错误原因
根目录的build.gradle脚本中不能直接使用android{}配置块,这个配置块仅适用于模块级(如app/build.gradle)的构建脚本,根项目的Gradle上下文没有android()方法,因此触发该错误。同时你的脚本还存在语法错误:多余的闭合大括号、buildToolsVersion行末尾的无效符号。
修复步骤
1. 修正根目录build.gradle
将根目录的build.gradle修改为以下内容(移除错误的android{}块,修复语法错误):
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.1.3' apply false id 'com.android.library' version '7.1.3' apply false id 'org.jetbrains.kotlin.android' version '1.5.21' apply false } ext { compose_version = '1.0.1' } task clean(type: Delete) { delete rootProject.buildDir }
2. 将Android配置移至模块级build.gradle
把原来根脚本中的android{}块内容,移动到app/build.gradle(或你对应的模块目录下的build.gradle)中,示例如下:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 33 buildToolsVersion '33.0.0' defaultConfig { applicationId "com.yourpackage.name" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion rootProject.ext.compose_version } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } dependenciesInfo { includeInApk true includeInBundle true } ndkVersion '25.0.8775105' } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation "androidx.compose.ui:ui:$compose_version" // 根据需求添加其他依赖 }
3. 同步Gradle
点击Android Studio右上角的Sync Project with Gradle Files按钮,等待同步完成即可。
内容的提问来源于stack exchange,提问作者Wangu Ngalati
相关产品推荐
相关产品推荐

