Android Studio集成AnyChart等图表库全部报红该如何解决
问题描述
使用最新版Android Studio,按照操作步骤尝试在项目中集成AnyChart、MPAndroidChart、Gruzer三款不同的图表库,所有库都无法正常使用,代码出现红线报错。
根目录build.gradle代码
buildscript { repositories { google() mavenCentral() maven { url 'https://jitpack.io' } } dependencies { classpath "com.android.tools.build:gradle:7.0.3" classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } task clean(type: Delete) { delete rootProject.buildDir }
app模块build.gradle依赖配置
dependencies { implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.2' implementation 'com.google.firebase:firebase-firestore:24.0.0' testImplementation 'junit:junit:' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation platform('com.google.firebase:firebase-bom:29.0.1') implementation 'com.google.firebase:firebase-analytics' implementation 'androidx.multidex:multidex:2.0.1' implementation 'com.android.support:multidex:1.0.3' implementation 'com.github.Gruzer:simple-gauge-android:0.3.1' implementation 'com.github.AnyChart:AnyChart-Android:1.1.2' implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' }
报错情况
主活动文件中所有图表库都出现相同的红线报错,报错截图如下:
解决方案
- 第一步:修改settings.gradle配置,AGP7.0+版本中,应用依赖的仓库需要配置在
dependencyResolutionManagement节点下,仅配置在buildscript的repositories中无法拉取应用依赖。找到项目根目录的settings.gradle文件,添加jitpack仓库配置:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url 'https://jitpack.io' } } }
- 第二步:移除冲突依赖,项目为AndroidX架构,同时依赖AndroidX和support版本的multidex会产生类冲突,删除app模块build.gradle中的这行配置:
implementation 'com.android.support:multidex:1.0.3'
- 第三步:补全缺失的依赖版本,junit依赖未填写版本号会导致依赖解析失败,修改对应的配置为:
testImplementation 'junit:junit:4.13.2'
- 第四步:替换兼容AndroidX的MPAndroidChart版本,当前使用的v3.0.3版本是support库版本,和AndroidX不兼容,修改对应的依赖配置为:
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
- 第五步:同步项目并清理缓存,修改完所有gradle配置后,点击Android Studio右上角的「Sync Project with Gradle Files」按钮同步项目,同步完成后点击File → Invalidate Caches,勾选清除文件系统缓存和本地历史,重启Android Studio即可。
内容的提问来源于stack exchange,提问作者Josh Simon
相关产品推荐
相关产品推荐

