【问题标题】:Set applicationApk and instrumentationApk for Spoon Gradle Plugin为 Spoon Gradle 插件设置 applicationApk 和 instrumentationApk
【发布时间】:2015-10-22 15:42:34
【问题描述】:

我想设置 .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 Plugin 中有可用的字段:

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

【问题讨论】:

    标签: android groovy gradle spoon android-instrumentation


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    请参阅 my answer to your different question 解释您在配置任务时出现的问题。

    【讨论】:

      【解决方案3】:

      我们需要考虑参数的注解

        /** 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"]
      }  
      

      更新

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

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

      【讨论】:

      • 能否请您再仔细看看?我已经和它斗争了几天。尝试过 Inputs.getProperties()、InputFiles.getProperties() - 也可以查找我可以设置的属性名称、applicationApk = file('myfile')、apk 和 test-apk、Application 等等。还给插件的创建者写了电子邮件,但没有回复。总是得到类似的东西:没有这样的属性:类的applicationApk:com.stanfy.spoon.gradle.SpoonExtension_Decorated
      • @F1sher 您是否尝试按照我的建议创建一个依赖于基本勺子任务的新任务?用你最好的尝试更新你的答案,我去看看
      • 对不起,我尝试尽可能多地重现我所记得的东西,我尝试过并启动它,我做了更多的变化等等。我无法使堆栈格式成为我的代码......不知道为什么我给了你屏幕。
      • @F1sher 您是否尝试过...正是我建议的代码? (您可能只需要设置正确的路径)
      • 是的。当我设置您的代码时,“输入”是灰色的,当我将光标放在它上面时,我得到:无法解析符号“输入”。在我启动它后的终端中,我可以看到这个 + 路径是 100% 正确的:评估项目“:app”时出现问题。 > 没有方法签名:org.gradle.api.internal.file.UnionFileCollection.getAt() 适用于参数类型:(java.util.ArrayList) 值:[[/Users/F1sherKK/Dev/myApp/app/build /outputs/apk/app-debug.apk, ...]] 可能的解决方案:getAt(int), getAt(java.lang.String), putAt(java.lang.String, java.lang.Object), wait( )、getAsPath()、last()
      【解决方案4】:

      我遇到了同样的问题:

      ...为属性“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}"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多