2

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'
Tomek Jurkiewicz
  • 386
  • 5
  • 18
  • Similar problem is solved here: http://stackoverflow.com/a/5153162/1201833, but obviously it does not relate to android (no testClasses task). – Tomek Jurkiewicz Feb 18 '16 at 17:38

2 Answers2

4

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')
Nimdokai
  • 787
  • 1
  • 6
  • 18
  • TYVM... This now means I can preserve my module's `internal` visibility modifiers while providing abstract test classes throughout my project. Encapsulation is important, and this helps tremendously. – 05nelsonm Sep 27 '20 at 09:11
  • Running `./gradlew build` from terminal results in a failed build due to `NoClassDefFoundError` for the helper class being used from another modules' `unitTestJar`. Any Ideas on how to resolve? – 05nelsonm Sep 30 '20 at 09:59
2

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')
Tomek Jurkiewicz
  • 386
  • 5
  • 18