Android Studio中build.gradle添加dependencyResolutionManagement报错求助
问题解决:No signature of method: ...android() 错误
错误原因
你把dependencyResolutionManagement代码块放在了app模块的android{}内部,这是完全错误的位置:
dependencyResolutionManagement是项目级的配置,必须放在**根目录的build.gradle**文件中,而非app模块的build.gradle- 即便放在正确文件里,它也需要处于顶级作用域,不能嵌套在任何其他代码块(比如
android{}、plugins{})内部
修复步骤
第一步:修改根目录的build.gradle
在项目根目录的build.gradle中添加dependencyResolutionManagement块,位置和plugins块同级:
// 根目录 build.gradle plugins { id 'com.android.application' version '7.0.0' apply false id 'com.android.library' version '7.0.0' apply false } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() // 其他需要的仓库 } } rootProject.name = "FindYourTimetable" include ':app'
第二步:清理app模块的build.gradle
把app模块build.gradle里的dependencyResolutionManagement块完全删除,修正后的代码如下:
// app模块 build.gradle plugins { id 'com.android.application' } android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.findyourtimetable" minSdkVersion 24 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' def fragment_version = "1.5.5" implementation "androidx.fragment:fragment:$fragment_version" }
第三步:同步项目
点击Android Studio右上角的Sync Now按钮,等待Gradle同步完成即可解决错误。
内容的提问来源于stack exchange,提问作者user21240486
相关产品推荐
相关产品推荐

