Android项目中使用GitHub仓库作为Gradle依赖遇配置缓存问题求助
问题:Fork后的GitHub仓库作为JitPack依赖引入时构建报错
我从GitHub上fork了一个项目并做了修改,想把修改后的版本作为依赖引入我的Android项目,使用的依赖配置如下:
implementation("com.github.my_user_name:library_name:master-SNAPSHOT")
JitPack上显示仓库构建正常,但运行项目时出现以下错误:
Configuration cache state could not be cached: field __librarySourceSets__ of task :app:mapDebugSourceSetPaths of type com.android.build.gradle.tasks.MapSourceSetPathsTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection > Could not resolve all files for configuration ':app:debugRuntimeClasspath.
相关Gradle配置代码
项目根目录 build.gradle.kts
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed plugins { alias(libs.plugins.androidApplication) apply false alias(libs.plugins.kotlinAndroid) apply false alias(libs.plugins.androidLibrary) apply false alias(libs.plugins.hiltAndroid) apply false alias(libs.plugins.kotlin.plugin.serialization) apply false // needs to be as the project's JVM version alias(libs.plugins.ksp) apply false alias(libs.plugins.org.jetbrains.kotlin.jvm) apply false } true // Needed to make the Suppress annotation work for the plugins block
项目根目录 settings.gradle.kts
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("https://www.jitpack.io" ) } } } rootProject.name = "MyAppNAme" include(":app") include(":feature:profile") // this is where I want to use the library
模块 :feature:profile 的 build.gradle.kts
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed plugins { alias(libs.plugins.androidLibrary) alias(libs.plugins.kotlinAndroid) alias(libs.plugins.hiltAndroid) alias(libs.plugins.kotlin.plugin.serialization) alias(libs.plugins.ksp) } android { namespace = "com.example.feature.profile" compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "17" } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" // excludes += "META-INF/DEPENDENCIES" // Avoid SRT and RTMP clash, from StreamPack pickFirsts += "**/*.so" } } buildFeatures { compose = true } composeOptions { //kotlinCompilerExtensionVersion = "1.5.4" // !important to keep this version kotlinCompilerExtensionVersion = "1.5.8" } } dependencies { implementation("com.github.my_user_name:library_name:master-SNAPSHOT") }
请问这种使用GitHub仓库作为依赖的方式是否正确?如果不正确,我哪里出错了?
解答
这种依赖引入的思路是可行的,但报错大概率是以下几个环节出了问题:
SNAPSHOT版本缓存未刷新
JitPack的master-SNAPSHOT是动态构建的,Gradle默认会缓存旧版本,导致无法拉取最新修改。可以:- 执行命令
./gradlew clean build --refresh-dependencies强制刷新依赖 - 修改依赖配置,标记该依赖为动态变化类型:
implementation("com.github.my_user_name:library_name:master-SNAPSHOT") { isChanging = true }
- 执行命令
Fork仓库的构建配置兼容性问题
虽然JitPack显示构建成功,但仓库本身的Gradle配置可能和你的项目不兼容:- 检查fork仓库的Gradle版本、Kotlin版本、Android Gradle Plugin版本,确保和你的项目(AGP对应Kotlin 1.9.x左右)匹配
- 确认fork仓库的
build.gradle中是否正确配置了maven-publish插件——JitPack需要这个插件生成可发布的构件
配置缓存冲突
报错提到配置缓存无法写入,说明依赖仓库包含Gradle无法序列化的文件集合。可以:- 临时禁用配置缓存,在
gradle.properties中添加:org.gradle.configuration-cache=false - 如果禁用后问题解决,说明仓库的构建逻辑存在不兼容配置缓存的代码,需要在fork的仓库中修复
- 临时禁用配置缓存,在
依赖传递冲突
如果fork仓库的依赖和你的项目依赖冲突,会导致debugRuntimeClasspath无法解析。可以执行./gradlew :feature:profile:dependencies查看依赖树,找出冲突依赖并排除:implementation("com.github.my_user_name:library_name:master-SNAPSHOT") { exclude(group = "冲突依赖组", module = "冲突依赖模块") }
内容的提问来源于stack exchange,提问作者code_dude
相关产品推荐
相关产品推荐

