60

There is a relatively-new lightweight JVM called Avian that can produce executables for iOS targets.

There isn't too much documentation on the website (and not much can be found searching with Google). I was wondering if anybody was aware of a step-by-step tutorial on how to get a basic Scala program running on iOS, using Avian.

George
  • 8,368
  • 12
  • 65
  • 106
Eduardo
  • 8,362
  • 6
  • 38
  • 72
  • 5
    I'd suggest first trying some demo java app. After having it running try a demo scala app. In theory, the only requirement to running scala code is scala-library.jar. – pedrofurla Dec 03 '12 at 17:53
  • 5
    Some discussion relating to Scala: https://groups.google.com/forum/?fromgroups=#!topic/avian/Hugny4JcnDw – madoki Jan 08 '13 at 08:02
  • Do you want a complete app (including the UI) or a template for a mixed objectiveC (UI) and Java (backend) one? – psychowood Feb 05 '13 at 09:34
  • I guess there would have to be some mix with Objective-C, to be able to have a UI. – Eduardo Feb 05 '13 at 13:19

4 Answers4

7

Another alternative JVM to iOS compiler is RoboVM. Although it is at an early stage, it looks quite promising, with examples on how to compile Scala for iOS.

EDIT This was an old answer, valid at that time, but, as @JamesMoore points out, RoboVM is no more. What looks very promising now, and may well be the way to run Scala code in iOS in the near future is Scala Native

Eduardo
  • 8,362
  • 6
  • 38
  • 72
4

Compiled Scala sources are completely standard class files. You should be able to follow the instructions (look for “Embedding”) on the website without large changes, just treat scala-library.jar as a dependency of your code.

I managed to bootstrap the complete compiler and the standard library running on Avian a few days ago.

Some parts might still be a bit rough around the edges, e. g. there is one mandatory fix which will be part of the next release of Scala (2.10.1) but is not in 2.10.0. If you want to play with it right now, you need to use a nightly build until 2.10.1 is released.

If you encounter any additional issues, please report them!

soc
  • 27,983
  • 20
  • 111
  • 215
  • But, for it to run on iOS, you need more than just compiling standard class files. You need a user interface, and you need to sign the app. I was looking for a tutorial that demonstrates the basic techniques to produce a basic "hello world" that actually runs on iOS. If such document existed, I think it could create a virtuous circle: it could help a lot of people get started, and it would also boost the popularity of Avian. – Eduardo Feb 05 '13 at 08:46
3

I may not need it anymore, now that Oracle is making JavaFX open-source on iOS and Android!

EDIT: Oracle updated the article to announce that they will not release a JVM, so it looks like JavaFX+Avian may be the way to go.

Eduardo
  • 8,362
  • 6
  • 38
  • 72
  • 1
    That article also reads: "A few years ago at JavaOne 2011 we showed a demo of JavaFX running on Android and iOS devices. This was a proof-of-concept which has two essential elements. The first is a port of the Windowing / Graphics layer of JavaFX (Glass & Prism) to use iOS & Android APIs. This is what we are open-sourcing, nothing more nor less. We are not providing a JVM for these platforms, nor am I aware of any such plan, so it’s not in any way a complete solution." So to run Java/Scala on iOS we still need Avian or something similar... – Mike Spadaru Feb 23 '13 at 14:45
  • @MikeSpadaru: that was actually an update to the article. really bad news :-( – Eduardo Feb 23 '13 at 17:49
2

Running Java byte code on iOS (not-rooted) is not only running that or those JVM. As far as I understand iOS memory management doesn't allow executable memory pages to be writable in user mode. That basically prohibits any JIT compilation. So even if it's possible to run some compiled (either from Java or Scala) classes on specific Java VM I would carefully check how this VM supports Ahead-Of-Time compilation in order to be runnable on iOS. As I have seen Avian AOT works well on a desktop. For iOS you will have to check it yourself, although the project looks promising in AOT area.

vladimir
  • 2,635
  • 3
  • 23
  • 26
  • If ran on a virtual machine, the code would not reside on executable memory. (Avian compiles AOT to real machine instructions, though). The restriction with using virtual machines is not technical, but rather a licensing one. – Eduardo Apr 17 '13 at 06:44
  • @Eduardo Yes, AOT is what makes it possible to avoid having writable executable memory. Imagine you don't have AOT. Then in runtime JIT should start compile your byte-code to native and save it somewhere in memory. After doing this that memory can't be executable anymore on iOS. That is the technical reason why JIT isn't possible on iOS. As I know Xamarin.iOS is "allowed" on iOS it uses the same AOT, though. I haven't heard about any licensing process done by Xamarin. It compiles to native executable, I don't see then how it could be banned by Apple. – vladimir Apr 17 '13 at 13:01
  • Even with JIT, the Java bytecode is not translated into machine instructions and stored in executable memory. It is executed by the virtual machine itself. – Eduardo Apr 17 '13 at 13:35
  • 1
    @Eduardo I don't agree that VM just interpret byte code when we have JIT in place. At least it's 100% up to VM implementation. And from what I've seen all JITs are compiling byte code to native code. [Wikipedia](http://en.wikipedia.org/wiki/Just-in-time_compilation) might be not the best source of information however it clearly says: "the program is stored in memory as byte code, but the code segment currently running is preparatively compiled to physical machine code in order to run faster". So "compiled physical machine code" is stored in memory and then can't be executed on iOS. – vladimir Apr 17 '13 at 15:41
  • 1
    @Eduardo I should make even stronger statement: JIT compilation is a process of pre-compilation byte code into native code at runtime for performance improvements. It is in contrast with interpretation of byte-code. And then going back to my initial answer JIT isn't possible on not-rooted iOS because there you can't in runtime write some machine code to memory and then pass control to that code (by that memory address). No licensing issues so far only tech reasons. However if you're making AOT compilation of byte-code then you can avoid this memory restrictions. – vladimir Apr 17 '13 at 15:59
  • That has nothing to do with rooted or not. It is a fundamental "unix" feature that certain areas of memory only contain data and you can not execute code in that memory. You can root your iDevice as often as you want, that wont change anything. – Angel O'Sphere Nov 08 '15 at 13:50