You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Android库的unitTest与androidTest源码集发布至Maven仓库

实现Android测试共享库的发布与依赖

核心问题

你当前的Gradle配置仅打包了库的**主源码集(main目录)**内容,而测试用的共享类、工具类等位于test(单元测试)和androidTest(Instrumented测试)源码集,并未被包含到发布的Artifact中,导致其他项目依赖后无法访问这些测试工具。

解决方案步骤

1. 调整源码集与发布Variant配置

在android块中,添加针对测试源码集的发布Variant配置,让Gradle识别并打包test和androidTest目录下的代码:

android {
    // 保留原有namespace、compileSdk、defaultConfig等配置
    
    publishing {
        // 打包单元测试(test源码集)的Release变体
        singleVariant("releaseUnitTest") {
            withSourcesJar() // 可选:发布源码包,方便其他项目调试
            withJavadocJar() // 可选:发布文档包
        }
        // 打包Instrumented测试(androidTest源码集)的Release变体
        singleVariant("releaseAndroidTest") {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

2. 修改依赖配置

将测试工具依赖从testImplementation/androidTestImplementation改为api,确保依赖该库的项目能自动传递这些测试依赖,无需重复声明:

dependencies {
    // 移除主源码集的无关依赖(如果你的库仅用于测试,不需要main目录代码)
    // implementation 'androidx.core:core-ktx:1.9.0' ... 这些可以删除
    
    // 单元测试工具所需依赖(api传递给依赖项目)
    api 'junit:junit:4.13.2'
    api "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    
    // Instrumented测试工具所需依赖(api传递给依赖项目)
    api 'androidx.test.ext:junit:1.1.3'
    api 'androidx.test.espresso:espresso-core:3.4.0'
    
    // Compose相关测试依赖
    api 'androidx.compose.material:material:1.3.0'
    api 'androidx.core:core-ktx:1.9.0'
}

3. 配置测试库的Maven发布

在afterEvaluate块中,创建针对测试Variant的Maven Publication,指定发布的GroupId、ArtifactId和Version:

afterEvaluate {
    publishing {
        publications {
            // 单元测试库的发布配置
            testUtils(MavenPublication) {
                from components.releaseUnitTest
                groupId = 'com.example.MyLibrary'
                artifactId = 'testing-utils'
                version = '1.0'
            }
            // Instrumented测试库的发布配置
            androidTestUtils(MavenPublication) {
                from components.releaseAndroidTest
                groupId = 'com.example.MyLibrary'
                artifactId = 'testing-utils-android'
                version = '1.0'
            }
        }
    }
}

4. 发布与依赖使用

  • 执行Gradle发布任务(如publishTestUtilsPublicationToMavenLocal或publishAndroidTestUtilsPublicationToMavenLocal)将库发布到本地Maven仓库,或配置远程仓库发布。
  • 其他项目中依赖:
    // 单元测试依赖
    testImplementation "com.example.MyLibrary:testing-utils:1.0"
    // Instrumented测试依赖
    androidTestImplementation "com.example.MyLibrary:testing-utils-android:1.0"
    

可选优化:同一Artifact区分测试类型

如果希望用同一个ArtifactId,可通过classifier区分单元测试和Instrumented测试版本:

afterEvaluate {
    publishing {
        publications {
            testUtils(MavenPublication) {
                from components.releaseUnitTest
                groupId = 'com.example.MyLibrary'
                artifactId = 'testing-utils'
                version = '1.0'
                classifier = 'test'
            }
            androidTestUtils(MavenPublication) {
                from components.releaseAndroidTest
                groupId = 'com.example.MyLibrary'
                artifactId = 'testing-utils'
                version = '1.0'
                classifier = 'androidTest'
            }
        }
    }
}

此时其他项目依赖方式:

testImplementation "com.example.MyLibrary:testing-utils:1.0:test"
androidTestImplementation "com.example.MyLibrary:testing-utils:1.0:androidTest"

内容的提问来源于stack exchange,提问作者Vahid Garousi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:45:46