roboscala / sbt-robovm

An sbt plugin for iOS development in Scala
BSD 2-Clause "Simplified" License
108 stars 16 forks source link

sbt-robovm

sbt-robovm is a plugin for the Scala build tool that aims to make it as simple as possible to compile Scala (and Java) code to binaries for iOS, linux, and OSX using RoboVM

Setup

  1. Install Xcode 6
  2. Install JDK 7
  3. Install sbt
  4. See roboscala-samples for example on how to use and configure

Add the Plugin

First, add the plugin to your project by appending addSbtPlugin("org.roboscala" % "sbt-robovm" % "1.12.0") into the project/plugins.sbt file. The file name (not extension) may actually be different, but such is the convention. The plugin's version is in sync with the RoboVM version it uses, so it should always be clear which RoboVM is being used.

Project Creation

All you have to do to use the plugin, is to add iOSRoboVMSettings key (or nativeRoboVMSettings if you are creating a native project) to your build.sbt file.

If you are creating a multi-project build, prepend that to your settings Seq:

lazy val myproject = Project(id = "myproject", base = file("myproject"), settings = iOSRoboVMSettings ++ Seq(
    /* More settings */
))

Tasks

There are different tasks defined for iOS and native console projects.

Shared

iOS

Native

Settings

As with tasks, there are some settings that are only meaningful in iOS projects. Some settings are actually implemented as tasks.

Shared

iOS Only

Debugging (licensed only)

Line numbers will be enabled automatically when the license is entered (see robovmLicense task).

To use the RoboVM debugger, prefix your task invocations with debug: (example: $ sbt myproject/debug:ipadSim). This sets the scope to the Debug configuration, in which the debugger is enabled and the debug port is set, by default to 5005. Running with the debugger enabled will allow you to connect to a running application with a java debugger.

Using the debugger from IntelliJ IDEA 14

  1. Create a new "Remote" Run/Debug configuration
    • Top bar, right next to the "Make" button -> "Edit configurations" -> "+" -> "Remote"
    • All settings can be left to their default values
  2. Run the project in debug mode from SBT (for example $ sbt debug:ipadSim)
  3. Make sure the configuration from step 1 is selected and press the "Debug" button
  4. IntelliJ will connect to the running application and you can start debugging like you are used to with standard Java debugging

Application execution will pause before your main method and wait for the debugger to attach. Then it will continue normally.

Interface Builder (licensed only)

This plugin offers a basic integration with XCode's Interface Builder. There are some excellent tutorials on how to use IB with IntelliJ on RoboVM website. Getting familiar with them is recommended, since the workflow in sbt is similar.

In the core of this feature is an interactive interfaceBuilder command. Run the command inside your iOS project, it will generate XCode project and open it in the Interface Builder. Then it will watch your code sources and when any of them change, it will recompile the project and update the XCode project accordingly. XCode will show new IBOutlets and IBActions very shortly after that.

You will also notice, that the prompt in the sbt console will change to "interfaceBuilder >". That notes that you are in a special mode, where the interfaceBuilder command is still running, but you can still run any commands/tasks as usual, so you can, for example, run the ipadSimulator task to quickly view your changes on device. Pressing enter, without any command, will exit the interfaceBuilder mode and you will be back to standard sbt prompt.

Because interfaceBuilder is a command and not a task (for technical reasons), it can not be scoped. Therefore, doing something like myProject/interfaceBuilder will not work. To work around this, use project myProject command first, to switch active project to that and then run interfaceBuilder. If you need even more granular scoping, use the robovmIBScope setting.

Tips

Hacking on the plugin

If you need to make modifications to the plugin itself, you can compile and install it locally:

$ git clone git://github.com/roboscala/sbt-robovm.git
$ cd sbt-robovm
$ sbt publish-local

When testing your changes, it is useful to publish locally with different version than what is officially used. That is because if you have already used the official version, your testing projects will most likely use that and not your modified version. To workaround that, change in sbt-robovm's build.sbt:

    version := roboVMVersion.value,

to

    version := roboVMVersion.value + "-YOUR_SUFFIX",

and in the project/plugins.sbt of your (testing) project, instead of standard installation:

// Necessary only when testing with RoboVM snapshot build, such as 1.11.1-SNAPSHOT
resolvers += Resolver.sonatypeRepo("snapshots")

addSbtPlugin("org.roboscala" % "sbt-robovm" % "1.12.0-YOUR_SUFFIX" changing())

Unless you need to use the SNAPSHOT version of RoboVM, it is easier to work with stable version, because for SNAPSHOT dependencies, sbt has to check for new version each run, which adds latency to testing.

However, when working on the plugin, you want it to be "redownloaded" each time it changes, and the changing() in addSbtPlugin line does exactly that. But don't worry, thanks to it being installed/published locally, the latency of these checks is negligible.

Contributing

Reporting any issues you encounter helps. If you want to help improving the plugin, feel free to make a PR.

Projects using the plugin

libgdx-sbt-project.g8 (Uses older version)