Compile jar with test classes

丶灬走出姿态 提交于 2021-02-11 10:08:17

问题


How can I compile jar with test classes in android? I am using android gradle plugin 1.3.1:

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

I've tried:

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

And it creates exactly what is defined: a jar with test sources, not with compiled classes. Where are the compiled classess and on which task I should depend on to create jar with test classes?

I need it to prepare test artifact for another project, which must extend some test classes.

Below is whole build.gradle with my modifications. This is google's volley library.

// 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'

回答1:


It looks like posting a question here gives some kind of inspiration to resolve the problem... anyway, there's the solution:

task testClassesJar(type: Jar, dependsOn: 'compileReleaseUnitTestSources') {
    from 'build/intermediates/classes/test/release'
}

configurations {
    tests
}

artifacts {
    tests testClassesJar
}

tricky part is that I could not find any reference on how to refer test classes path using gradle's DSL variables (ie., android.sourceSets.test.output). Using this relative path looks resonable.

In the refering projects one have to add line:

testCompile project(path: ':volley', configuration: 'tests')



回答2:


The solution mentioned by Tomek Jurkiewicz for Android + Kotlin looks like this:

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

configurations {
    unitTestArtifact
}

artifacts {
    unitTestArtifact jarTests
}

Gradle for project that is going to use dependencies:

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


来源:https://stackoverflow.com/questions/35486982/compile-jar-with-test-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!