为 Spoon Gradle 插件设置 applicationApk 和 instrumentationApk

Set applicationApk and instrumentationApk for Spoon Gradle Plugin

我想设置将用于 运行 我使用 SpoonGradlePlugin 进行测试的 .apk 文件。

我可以从 gradle 文件中以编程方式设置可用属性:

https://github.com/stanfy/spoon-gradle-plugin/blob/master/src/main/groovy/com/stanfy/spoon/gradle/SpoonExtension.groovy

但我的项目有各种风格和名称,我想测试它们。使用当前设置我得到:

* What went wrong:
A problem was found with the configuration of task ':app:spoonDebugAndroidTest'.
> File '/Users/F1sherKK/Dev/MyApp-Android/app/build/outputs/apk/app-debug.apk' specified for property 'applicationApk' does not exist.

我的构建名称是:

app-debug-androidTest-unaligned.apk
MyApp-debugA-1.2.3-201.apk
MyApp-debugB-1.2.3-201.apk
MyApp-debugC-1.2.3-201.apk

这就是为什么我想在 gradle 代码或控制台中的某处设置我的 .apk。到目前为止我发现 Spoon Gradle 插件中有可用字段:

https://github.com/stanfy/spoon-gradle-plugin/blob/master/src/main/groovy/com/stanfy/spoon/gradle/SpoonRunTask.groovy

名字:

  /** Instrumentation APK. */
  @InputFile
  File instrumentationApk

  /** Application APK. */
  @InputFile
  File applicationApk

但我无法像访问 SpoonExtension.groovy 中的属性那样访问 gradle 中的那些。

有什么方法可以设置这些字段吗?

//编辑 - 添加了一些尝试: 这是我的基本勺子配置:

spoon {
    debug = true
    baseOutputDir = file("$buildDir/spoon-log")
    if (project.hasProperty('spoonClassName')) {
        className = project.spoonClassName

        if (project.hasProperty('spoonMethodName')) {
            methodName = project.spoonMethodName
        }
    }
}

以及扩展它并覆盖 instumentationArgs 以设置包和启动其他类型测试的任务。

task spoonAllTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.sendmoney.instrumentation"]
    }
}

task spoonFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.instrumentation.flowtests"]
    }
}

现在我尝试编辑 applicationApk 或 instrumentationApk 文件:

Edit2:我尝试了新事物:

task spoonFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        inputs.property("applicationApk", "$buildDir/outputs/apk/ap12345p-debug.apk")
        inputs.property("instrumentationApk", "$buildDir/outputs/apk/ap125p-debug.apk")
        println inputs.getFiles()
        println inputs.getProperties()
        instrumentationArgs = ["package=com.azimo.sendmoney.instrumentation.flowtests"]
    }
}

终端响应:

2015-10-26 20:24:12 [SR.runTests] Executing instrumentation suite on 0 device(s).
2015-10-26 20:24:12 [SR.runTests] Application: com.azimo.sendmoney.debug1 from /Users/F1sherKK/Dev/Azimo-Android/app/build/outputs/apk/app-debug.apk
2015-10-26 20:24:12 [SR.runTests] Instrumentation: com.azimo.sendmoney.debug1.test from /Users/F1sherKK/Dev/Azimo-Android/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
:app:spoon
:app:spoonFlowTests
file collection
{instrumentationApk=/Users/F1sherKK/Dev/Azimo-Android/app/build/outputs/apk/ap125p-debug.apk, applicationApk=/Users/F1sherKK/Dev/Azimo-Android/app/build/outputs/apk/ap12345p-debug.apk}
:Azimo-Android:app:help

Welcome to Gradle 2.5.

To run a build, run gradlew <task> ...

To see a list of available tasks, run gradlew tasks

To see a list of command-line options, run gradlew --help

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL

Total time: 13.289 secs

我们需要考虑参数的注解

  /** Instrumentation APK. */
  @InputFile
  File instrumentationApk

  /** Application APK. */
  @InputFile
  File applicationApk

因为它们被标记为 @InputFile,我们应该能够创建一个自定义任务,该任务依赖于使用 [=33 传递输入的 Spoon 任务=]属性

一个粗略的例子(未经测试):

task customSpoon(type: Exec, dependsOn: spoon) {
     inputs.files ["path/instrumentation.apk", "path/debuggable.apk"]
}  

更新

作为替代方案,只需创建一个自定义任务,该任务模拟 对基本 Spoon 任务的命令行参数化调用,如下所示

task runSpoonTask(type: Exec) {
    commandLine './gradlew', 'spoon', '$buildDir/outputs/apk/ap12345p-debug.apk', '$buildDir/outputs/apk/ap125p-debug.apk'
}

修改apk名称后即可应用Spoon插件:

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
afterEvaluate {
        apply plugin: 'spoon'
        spoon {
            debug = true;
        }
    }

来源:https://github.com/stanfy/spoon-gradle-plugin/issues/70

请参阅 解释您配置任务的方式有什么问题。

我遇到了同样的问题:

... specified for property 'applicationApk' does not exist

我们用它从 "app-debug.apk" 重命名为 "currentProject-debug.apk":

applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(
                        output.outputFile.parent,
                        output.outputFile.name.replace("app", "${project.hdmiAppName}"))

            }
} 

但这似乎与 Spoon 配合使用效果更好:

defaultConfig {
    applicationId packageName
    ...
    setProperty("archivesBaseName", "${project.hdmiAppName}")
}

这会将 APK 名称的 "app" 部分重命名为您喜欢的名称。在我们的例子中,我们在属性文件中指定了 "${project.hdmiAppName}"。但我想它也可以是 "MyApp-${versionName}".