集成华为AppGallery SDK同步失败:com.android.tools.build:gradle未配置
解决华为AGC SDK同步报错:com.android.tools.build:gradle is not set in the build.gradle
我尝试用华为AppGallery创建应用,添加SDK后执行同步操作时出现错误:Error: com.android.tools.build:gradle is no set in the build.gradle。已经重装所有组件,但错误仍存在,无法定位问题。相关build.gradle代码如下:
build.gradle (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() //Add the Maven address. maven {url 'https://developer.huawei.com/repo/'} } dependencies { classpath 'com.huawei.agconnect:agcp:1.5.2.300' } } plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false }
build.gradle (app)
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.first.a1806957' compileSdk 32 defaultConfig { applicationId "com.first.a1806957" minSdk 21 targetSdk 32 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 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300' } apply plugin: 'com.huawei.agconnect'
问题原因
项目级build.gradle的buildscript依赖块中,缺少了Android Gradle Plugin的显式声明。虽然你在plugins块里指定了插件版本,但当前使用的华为AGCP插件版本需要在buildscript中明确引入com.android.tools.build:gradle才能正常工作。
修复方案
修改项目级build.gradle的buildscript依赖部分,添加与plugins块版本一致的Android Gradle Plugin依赖:
buildscript { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } dependencies { classpath 'com.android.tools.build:gradle:7.3.1' // 新增这一行 classpath 'com.huawei.agconnect:agcp:1.5.2.300' } }
可选优化
你还可以将app级build.gradle末尾的插件应用方式,改为符合新版Gradle规范的plugins块声明:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'com.huawei.agconnect' version '1.5.2.300' // 替换原有的apply plugin语句 }
完成以上修改后,重新同步项目即可解决报错。
内容的提问来源于stack exchange,提问作者KareemRj
相关产品推荐
相关产品推荐

