GitHub Actions构建Android多模块应用失败:services模块无匹配配置
问题详情
本地执行gradlew assembleDebug --stacktrace可正常构建多模块Android应用,但在GitHub Actions中构建时失败,报错信息如下:
Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
Could not resolve project :services.
Required by:
project :app
No matching configuration of project :services was found. The consumer was configured to find an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
- None of the consumable configurations have attributes.
- Try:
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
相关配置文件
services模块gradle配置
import dependencies.Apollo import dependencies.Dependencies import dependencies.RoomDependencies import dependencies.Versions plugins { id("com.android.library") id("kotlin-android") id("com.apollographql.apollo3") id("org.jlleitschuh.gradle.ktlint") id 'kotlin-kapt' } apollo { useVersion2Compat() generateKotlinModels.set(true) srcDir("src/main/graphql") } android { compileSdkVersion Versions.compilesdk buildToolsVersion Versions.buildTools kotlinOptions { jvmTarget = '11' freeCompilerArgs = ["-Xallow-result-return-type"] } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } buildTypes { release { } debug { } } } repositories { mavenCentral() } java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } dependencies { kapt("com.apollographql.apollo3:apollo-runtime:3.2.1") implementation(Apollo.APOLLO_CACHE) implementation(Apollo.APOLLO_SQL) implementation project( ":linguisticdomain") // retrofit implementation(Dependencies.retrofit) implementation("com.google.android.gms:play-services-location:18.0.0") // room implementation(RoomDependencies.room_ktx) implementation(RoomDependencies.room_runtime) kapt(RoomDependencies.room_compiler) implementation(RoomDependencies.ROOM_TEST) }
app模块gradle配置
import dependencies.* import dependencies.dependencies.SupportDependencies import dependencies.dependencies.TestDependencies plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id "androidx.navigation.safeargs" id 'dagger.hilt.android.plugin' id 'com.google.gms.google-services' // Google Services plugin id 'com.google.firebase.crashlytics' id 'org.jlleitschuh.gradle.ktlint' } android { compileSdkVersion Versions.compilesdk buildToolsVersion Versions.buildTools buildFeatures { compose true dataBinding = true viewBinding = true } kotlinOptions { jvmTarget = '11' freeCompilerArgs = ["-Xallow-result-return-type"] } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } defaultConfig { applicationId Application.id minSdkVersion Versions.minsdk targetSdkVersion Versions.targetsdk versionCode Application.version_code versionName "1.0.2" testInstrumentationRunner AndroidTestDependencies.instrumentation_runner // ... // Set the properties within `local.properties` into a `Properties` class so that values // within `local.properties` (e.g. Maps API key) are accessible in this file. Properties properties = new Properties() if (rootProject.file("local.properties").exists()) { properties.load(rootProject.file("local.properties").newDataInputStream()) } // Inject the Maps API key into the manifest manifestPlaceholders = [mapsApiKey: properties.getProperty("MAPS_API_KEY", "")] } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' manifestPlaceholders = [crashlyticsCollectionEnabled: "true"] } debug { manifestPlaceholders = [crashlyticsCollectionEnabled: "true"] } } composeOptions { kotlinCompilerVersion Versions.kotlin kotlinCompilerExtensionVersion Compose.version } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project( ":linguisticdomain") implementation project( ":services") // Kotlin implementation Dependencies.kotlin_standard_library implementation Dependencies.ktx //implementation Dependencies.kotlin_extensions //support implementation SupportDependencies.appcompat implementation SupportDependencies.constraintlayout implementation SupportDependencies.material_design implementation SupportDependencies.legacy_support implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0' //test testImplementation TestDependencies.junit4 androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' //navigation implementation Dependencies.navigation_fragment implementation Dependencies.navigation_ui //ktx implementation Dependencies.fragment_ktx //apollo implementation("com.apollographql.apollo3:apollo-runtime:3.2.1") implementation(Apollo.APOLLO_CACHE) implementation(Apollo.APOLLO_SQL) compileOnly "org.jetbrains:annotations:16.0.1" //Build interactive settings screens without needing to interact with device storage or manage the UI. implementation Dependencies.android_preferences testCompileOnly "org.jetbrains:annotations:16.0.1" //debugging implementation Dependencies.timber //view_pager2 implementation Dependencies.view_pager2 // retrofit implementation Dependencies.retrofit implementation Dependencies.converter_moshi implementation "com.squareup.moshi:moshi:1.9.2" implementation "com.squareup.moshi:moshi-kotlin:1.9.2" implementation "com.squareup.retrofit2:converter-scalars:2.3.0" implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2' implementation 'com.facebook.android:facebook-android-sdk:5.15.3' implementation Dependencies.kotlin_coroutines_android implementation 'com.facebook.android:facebook-login:5.15.3' implementation 'com.google.code.gson:gson:2.8.6' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1' implementation 'com.github.bumptech.glide:glide:4.12.0' implementation Compose.compose_ui implementation Compose.compose_tooling implementation Compose.compose_foundation implementation Compose.compose_material implementation Compose.compose_material_icons implementation Compose.compose_material_icons_ext implementation Compose.compose_activity implementation Compose.compose_viewModel implementation Compose.compose_livedata implementation Compose.constraint_layout implementation Compose.compose_run_time implementation "androidx.navigation:navigation-compose:2.6.0-alpha03" implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1" implementation 'androidx.hilt:hilt-navigation-compose:1.0.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' implementation 'com.sagar:coroutinespermission:2.0.3' // UI Tests // androidTestImplementation 'androidx.ui:ui-test:1.0.0-alpha05' androidTestImplementation TestDependencies.arch_core_testing implementation "com.google.dagger:hilt-android:2.42" kapt "com.google.dagger:hilt-android-compiler:2.42" implementation 'androidx.hilt:hilt-work:1.0.0' //this gets warning https://stackoverflow.com/questions/49602410/when-should-we-use-android-arch-lifecyclecompiler-or-android-arch-lifecycleco // kapt "androidx.lifecycle:lifecycle-compiler:2.2.0" implementation Dependencies.work_manager implementation Dependencies.lottie_compose implementation Dependencies.lottie implementation Accompanist.insets implementation Accompanist.navigation_animation implementation Accompanist.coil implementation GoogleMaps.maps implementation GoogleMaps.mapsKtx implementation "com.github.PhilJay:MPAndroidChart:v3.1.0-alpha" implementation "io.github.vanpra.compose-material-dialogs:datetime:0.4.0" //firebase implementation platform('com.google.firebase:firebase-bom:28.1.0') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-crashlytics-ktx' implementation 'com.google.firebase:firebase-analytics-ktx' implementation("com.google.firebase:firebase-iid") implementation 'com.google.firebase:firebase-messaging:23.0.0' implementation 'com.google.firebase:firebase-core:20.0.2' //pusher beams implementation 'com.pusher:push-notifications-android:1.6.2' // Activity Compose implementation "androidx.activity:activity-compose:1.5.1" // CameraX implementation "androidx.camera:camera-camera2:1.0.2" implementation "androidx.camera:camera-lifecycle:1.0.2" implementation "androidx.camera:camera-view:1.0.0-alpha31" // qrcode implementation('com.journeyapps:zxing-android-embedded:4.1.0') { transitive = false } implementation 'com.google.zxing:core:3.3.3' implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3' } kapt { correctErrorTypes true generateStubs = true }
GitHub Actions脚本
name: Distribute to app distribution on: pull_request: jobs: distribute: runs-on: ubuntu-latest container: alvrme/alpine-android:android-33-jdk11 steps: - uses: actions/checkout@v2 - name: Cache Gradle uses: actions/cache@v1 with: path: ~/.gradle/caches/ key: cache-clean-gradle-${{ matrix.os }}-${{ matrix.jdk }} - name: Cache Gradle Wrapper uses: actions/cache@v1 with: path: ~/.gradle/wrapper/ key: cache-clean-wrapper-${{ matrix.os }}-${{ matrix.jdk }} - name: Grant Permission to Execute run: chmod +x gradlew - name: Distribute app with 🔥 App Distribution 🚀 run: | extras fastlane fastlane android increment_release_name fastlane android distribute
解决方法
1. 移除services模块中重复的Java版本配置
services模块中同时通过android.compileOptions和独立java块配置了Java版本,这会导致Gradle混淆模块类型,无法生成Android库所需的配置属性。删除以下代码块:
java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 }
2. 完善services模块的buildTypes配置
给空的buildTypes添加基础配置,确保Gradle能识别对应构建类型的属性:
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { minifyEnabled false } }
3. 修复GitHub Actions缓存配置
当前缓存key使用了未定义的matrix变量,导致缓存失效或无法命中。修改为固定key或匹配环境的变量:
- name: Cache Gradle uses: actions/cache@v1 with: path: ~/.gradle/caches/ key: cache-gradle-android33-jdk11-${{ github.sha }} - name: Cache Gradle Wrapper uses: actions/cache@v1 with: path: ~/.gradle/wrapper/ key: cache-wrapper-android33-jdk11
4. 确保GitHub Actions环境组件完整
在构建前添加步骤安装对应版本的Android构建工具:
- name: Install Android Build Components run: sdkmanager "build-tools;${{ env.BUILD_TOOLS_VERSION }}" "platforms;android-${{ env.COMPILE_SDK_VERSION }}"
需要在Workflow开头定义环境变量,对应项目中的版本:
env: COMPILE_SDK_VERSION: 33 BUILD_TOOLS_VERSION: 33.0.2
总结
最核心的问题是services模块中重复的Java版本配置导致Gradle无法正确识别为Android库,进而缺失构建所需的属性。优先移除多余的java块,再配合其他调整即可解决构建失败问题。
内容的提问来源于stack exchange,提问作者David Ibrahim

