【问题标题】:Compile jar with test classes用测试类编译 jar
【发布时间】:2016-05-30 22:40:16
【问题描述】:

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

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

下面是我修改的整个 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'

【问题讨论】:

标签: android android-gradle-plugin


【解决方案1】:

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

【讨论】:

  • TYVM... 这意味着我可以保留模块的 internal 可见性修饰符,同时在整个项目中提供抽象测试类。封装很重要,这很有帮助。
  • 从终端运行 ./gradlew build 会导致构建失败,因为 NoClassDefFoundError 用于从另一个模块的 unitTestJar 使用的帮助程序类。关于如何解决的任何想法?
【解决方案2】:

看起来在这里发布一个问题会给解决问题带来一些启发......无论如何,有解决方案:

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

configurations {
    tests
}

artifacts {
    tests testClassesJar
}

棘手的部分是我找不到任何关于如何使用 gradle 的 DSL 变量(即 android.sourceSets.test.output)引用测试类路径的参考资料。使用这个相对路径看起来很合理。

在引用项目中必须添加一行:

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

【讨论】:

  • 终于有人向我展示了如何在 Android 上执行此操作!请注意,路径build/intermediates/classes/test/releasedebug 结尾,而不是使用assembleDebug 构建。 dependsOn: 'compileReleaseUnitTestSources' 意味着什么?
  • 输出文件路径是什么?
  • 输出文件路径是什么?
  • 我收到了ClassNotFoundException
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多