android - 用测试类编译 jar

标签 android android-gradle-plugin

如何在android中编译带有测试类的jar?我正在使用 android gradle 插件 1.3.1:

classpath 'com.android.tools.build:gradle:1.3.1'

我试过:

task testSourcesJar(type: Jar) {
    from android.sourceSets.test.java.srcDirs
}

它创建的正是定义的内容:一个包含测试源的 jar,而不是编译类。编译的类在哪里?我应该依赖哪个任务来创建带有测试类的 jar?

我需要它来为另一个项目准备测试 Artifact ,该项目必须扩展一些测试类。

下面是我修改后的整个 build.gradle。这是 google 的 volley 库。

// NOTE: The only changes that belong in this file are the definitions
// of tool versions (gradle plugin, compile SDK, build tools), so that
// Volley can be built via gradle as a standalone project.
//
// Any other changes to the build config belong in rules.gradle, which
// is used by projects that depend on Volley but define their own
// tools versions across all dependencies to ensure a consistent build.
//
// Most users should just add this line to settings.gradle:
//     include(":volley")
//
// If you have a more complicated Gradle setup you can choose to use
// this instead:
//     include(":volley")
//     project(':volley').buildFileName = 'rules.gradle'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.library'

repositories {
    jcenter()
}

android {
    compileSdkVersion 22
    buildToolsVersion = '22.0.1'
}


task testSourcesJar(type: Jar, dependsOn: 'testClasses') {
    from android.sourceSets.test.java.srcDirs
}

configurations {
    testArtifacts
}

artifacts {
    testArtifacts testSourcesJar
}

apply from: 'rules.gradle'

最佳答案

Tomek Jurkiewicz提到的解决方案Android + Kotlin 看起来像这样:

task jarTests(type: Jar, dependsOn: "assembleDebugUnitTest") {
    getArchiveClassifier().set('tests')
    from "$buildDir/tmp/kotlin-classes/debugUnitTest"
}

configurations {
    unitTestArtifact
}

artifacts {
    unitTestArtifact jarTests
}

将要使用依赖项的项目的 Gradle:

testImplementation project(path: ':shared', configuration: 'unitTestArtifact')

关于android - 用测试类编译 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486982/

相关文章:

java - 发送日期参数到另一个Activity但值为零//android studio

android - Braintree 从 Gradle 中排除库

Android:能够为不受支持的 Android 版本安装应用程序

Android Gradle 4 升级问题(aapt、windowEnterAnimationt)——看起来像 "aapt2 issue"

android - Activity 未完成

android - 如何在 10 秒后取消警报?

android - 如果我将我的应用分发到其他应用商店,Google 的应用内购买是否仍然有效

android - 使用新版本的 gradle android 插件检测未使用的资源误报

android - 创建新项目时 Gradle 默认依赖项

android - Unity3D 修复了 TimeStep - Android FPS 问题