如何在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/