Android应用本地构建正常但GitHub Actions构建失败排查
本地Android构建正常但GitHub Actions中依赖解析失败(DTO模块配置问题)
问题场景
本地执行以下命令可正常编译Android项目:
ANDROID_HOME=/.../Library/Android/sdk gradle clean assembleDebug
但通过以下GitHub Actions配置执行相同构建时失败:
on: push: branches: - '**' jobs: test: name: Analyze Android runs-on: ubuntu-latest steps: - name: Checkout App uses: actions/checkout@v2 - uses: actions/setup-java@v3 with: distribution: temurin java-version: 11 - name: Setup Gradle uses: gradle/gradle-build-action@v2 - name: Setup Android SDK uses: android-actions/setup-android@v2 - name: Assemble run: | ./gradlew clean assembleDebug
错误日志
FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':app:mergeDebugAssets'. > Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'. > Could not resolve project :DTO. Required by: project :app project :app > project :UI project :app > project :Data project :app > project :Domain > No matching configuration of project :DTO was found. The consumer was configured to find a runtime of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but: - None of the consumable configurations have attributes.
环境对比
本地与GitHub Actions的Gradle版本一致:
- 本地环境:Gradle 7.5.1,JVM 15.0.2,Mac OS X
- GitHub Actions环境:Gradle 7.5.1,JVM 11.0.16,Linux
DTO模块使用java-library+kotlin.jvm插件,其他Android模块使用com.android.library+kotlin.android插件,尝试转换DTO模块配置后问题仍存在。
问题原因
核心矛盾是Android模块的依赖请求属性与纯Java/Kotlin模块的可消费配置属性不匹配:
- Android模块(
com.android.library插件)在解析依赖时,会自动附加BuildTypeAttr、AgpVersionAttr、kotlin.platform.type等专属属性; - 纯Java/Kotlin模块(
java-library+kotlin.jvm插件)的默认可消费配置(如apiElements、runtimeElements)不会携带这些Android专属属性; - 本地构建时,Gradle缓存可能掩盖了属性不匹配的问题(例如缓存了已兼容的依赖解析结果),而GitHub Actions是全新的干净环境,缓存不存在,因此属性匹配失败直接暴露。
解决方案
方案1:为DTO模块添加必要属性
在DTO模块的build.gradle(或build.gradle.kts)中,手动为可消费配置添加Android模块所需的属性,让两边属性匹配:
plugins { id 'java-library' id 'kotlin.jvm' } // 为所有配置添加Kotlin平台类型属性 configurations.all { attributes.attribute( org.jetbrains.kotlin.platform.type, org.jetbrains.kotlin.platform.jvm.JvmPlatformType.INSTANCE ) } // 为可消费的API和Runtime配置添加BuildType和AGP版本属性 def buildTypeAttr = project.objects.named(com.android.build.api.attributes.BuildTypeAttr, "debug") def agpVersionAttr = project.objects.named(com.android.build.api.attributes.AgpVersionAttr, "7.2.1") configurations.apiElements { attributes.attribute(com.android.build.api.attributes.BuildTypeAttr, buildTypeAttr) attributes.attribute(com.android.build.api.attributes.AgpVersionAttr, agpVersionAttr) } configurations.runtimeElements { attributes.attribute(com.android.build.api.attributes.BuildTypeAttr, buildTypeAttr) attributes.attribute(com.android.build.api.attributes.AgpVersionAttr, agpVersionAttr) }
方案2:在Android模块中放宽依赖属性要求
如果不想修改DTO模块的配置,可以在Android模块的依赖声明中,明确指定属性匹配规则,忽略不必要的属性差异:
// 在app/UI/Data/Domain模块的build.gradle中 implementation(project(":DTO")) { attributes { // 匹配Kotlin JVM平台类型 attribute( org.jetbrains.kotlin.platform.type, org.jetbrains.kotlin.platform.jvm.JvmPlatformType.INSTANCE ) // 显式指定BuildType属性(与当前模块一致) attribute( com.android.build.api.attributes.BuildTypeAttr, project.objects.named(com.android.build.api.attributes.BuildTypeAttr, "debug") ) } }
方案3:全局配置属性兼容性规则
在项目根目录的build.gradle中添加全局解析策略,自动处理Android模块与纯Java/Kotlin模块的属性匹配:
subprojects { configurations.all { resolutionStrategy { eachDependency { details -> // 匹配DTO模块 if (details.requested.name == "DTO") { attributes { attribute( org.jetbrains.kotlin.platform.type, org.jetbrains.kotlin.platform.jvm.JvmPlatformType.INSTANCE ) attribute( com.android.build.api.attributes.BuildTypeAttr, project.objects.named(com.android.build.api.attributes.BuildTypeAttr, "${project.android.buildTypes.debug.name}") ) } } } } } }
内容的提问来源于stack exchange,提问作者Jackie
相关产品推荐
相关产品推荐

