为Spoon Gradle Plugin设置applicationApk和instrumentationApk

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

我可以从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来设置包并启动其他types的测试。

 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  ... 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  BUILD SUCCESSFUL Total time: 13.289 secs 

请查看我对您的不同问题的回答,解释您如何配置任务有什么问题 。

您可以在更改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

我们需要考虑参数的注释

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

由于它们被标记为@InputFile ,我们应该能够创建一个自定义任务,该任务取决于使用input.files属性传递输入的Spoon

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

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

UPDATE

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

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

我有同样的问题:

…为属性’applicationApk’指定的…不存在

我们用它来从“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}"