本文介绍了用测试类编译jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

我尝试过:

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

它精确地创建了定义:一个带有测试源而不是编译类的jar.用测试类创建jar时,编译后的类在哪里,应该依靠哪个任务?

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.

下面是经过修改的整个build.gradle.这是Google的排球库.

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 提到的解决方案 Android + Kotlin 如下:

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

这篇关于用测试类编译jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:44