4

I am defining a gradle task "launchIPad2Simulator" that is subclassing another already defined task "launchIPadSimulatorfrom" in robovm gradle plugin. The goal is to set the project properties which are defining which simulator will run.

// Run the IPad2 simulator
task launchIPad2Simulator2(type: org.robovm.gradle.tasks.IPadSimulatorTask) {

  project.setProperty("robovm.device.name", "iPad-2")
  project.setProperty("robovm.arch", "x86")
} 

But the problem is, I must first define the properties in the gradle.properties file. They don't even need to have any value assigned. The whole content of the gradle.properties file:

robovm.device.name
robovm.arch

I would rather have gradle.properties file empty, but if the above task is then run, the error: Error:(112, 0) No such property: robovm.device.name for class: org.gradle.api.internal.project.DefaultProject_Decorated is shown.

Also if properties are only defined in task as following (gradle.properties is empty), they are ignored.

// Run the IPad2 simulator
task launchIPad2Simulator2(type: org.robovm.gradle.tasks.IPadSimulatorTask) {

  project.properties.put("robovm.device.name", "iPad-2")
  project.properties.put("robovm.arch", "x86")
}

So what is the correct way to dynamically set the project properties in subclassed task?

=== Edit ===

Ok now I see that setting the project properties is also not good, because in multiple tasks it gets overwritten. So maybe this shouldn't be project properties in first place.

The temp solution now is using command line invocation of tasks:

// simulator with properties launched from command line
task launchIPad2Simulator1(type: Exec) {
  commandLine 'gradle', '-Probovm.device.name=iPad-2', '-Probovm.arch=x86', 'launchIPadSimulator'
}
RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36

1 Answers1

13

try

task launchIPad2Simulator2(type: org.robovm.gradle.tasks.IPadSimulatorTask) {
  project.ext."robovm.device.name" = "iPad-2"
  project.ext."robovm.arch" = "x86"
}

this is the gradle syntax to add dynamic properites to the project object.

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • Thanks. With this syntax I can add the project properties dynamically. But now I have the problem when I define multiple task. Lets say I have launchIPad2Simulator and then I define another one launchIPadAirSimulator. The properties that are set in last one also override ones for running the iPad2 simulator. This is probably because they are defined as project properties. – RenatoIvancic Aug 10 '15 at 11:18
  • it would be way easier to use if the IPadSimulatorTask allows setting properties directly on the task instead of using Project Properties here. one workaround for you might be to put the project property setting in a doFirst{} block. This way the properties are set just before the task is used. This should allow you to run multiple instances of org.robovm.gradle.tasks.IPadSimulatorTask. I suggest you raise an issue for this issue at the robovm guys. – Rene Groeschke Aug 10 '15 at 11:34
  • As you replied, the IPadSimulatorTask should allow to set the task properties. I made a fork of robovm gradle project and tried it out, it is working as expected. – RenatoIvancic Aug 19 '15 at 08:53