Flutter项目Gradle无法解析androidx.appcompat:appcompat:1.3.0依赖求助
Flutter Gradle依赖解析失败:无法获取androidx.appcompat:appcompat:1.3.0
问题详情
构建Flutter项目时遭遇Gradle依赖解析错误,无法拉取androidx.appcompat:appcompat:1.3.0,错误日志如下:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'. > Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'. > Could not resolve androidx.appcompat:appcompat:1.3.0. Required by: project :app
已按照Flutter官方AndroidX迁移文档添加以下配置,但问题未解决:
module: androidX: true # Add this line.
同时尝试过Stack Overflow相关问题的所有解决方案(含评论及回答),均无效,求可行解决办法。
补充配置文件内容
android\build.gradle
buildscript { ext.kotlin_version = '1.3.50' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
android\app\build.gradle
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 29 sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { disable 'InvalidPackage' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.shopx" minSdkVersion 16 targetSdkVersion 29 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { //implementation 'androidx.appcompat:appcompat:1.3.1' implementation group: 'com.google.android.material', name: 'material', version: '1.6.1' implementation 'androidx.core:core-ktx:1.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
gradle.properties
org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true
解决办法
升级Gradle工具与Kotlin版本
当前com.android.tools.build:gradle:3.5.0版本过低,无法兼容androidx.appcompat:1.3.0。修改android\build.gradle:buildscript { ext.kotlin_version = '1.5.31' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:4.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }同步compileSdkVersion版本
androidx.appcompat:1.3.0要求最低compileSdkVersion为30,修改android\app\build.gradle:android { compileSdkVersion 30 defaultConfig { targetSdkVersion 30 } }清理缓存并重新构建
执行命令清理缓存后重新构建:flutter clean ./gradlew clean build --refresh-dependencies flutter run添加镜像仓库(网络受限环境)
若无法访问官方仓库,在allprojects.repositories中添加国内镜像:allprojects { repositories { google() mavenCentral() maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' } } }
内容的提问来源于stack exchange,提问作者Eden Zane
相关产品推荐
相关产品推荐

