Search code examples
javaandroid-studiojruby

Using JRuby in Android Studio


I'm trying to use JRuby in projects created in Android studio. I have built the latest (as of 9/10/2014) JRuby, and have completely updated Android Studio. I have a working app that I can use to test. I have a copy of jruby.jar in the MyApp/app/libs directory. Once there I right-clicked on jruby.jar and selected "Add as Library", which presumably did something (though what I am not certain), and have added the line "compile files('libs/jruby.jar')" to the build.gradle that is in MyApp/app/.

In one of the class files I have "import org.jruby.embed.ScriptingContainer;", which Android Studio says is fine (if I hadn't added jruby.jar as a library I couldn't include this without an error). Then in one of the working methods I say "ScriptingContainer container = new ScriptingContainer();". When I run this method the app will crash. I won't try to include the whole error message, but the important part seems to be:

 Caused by: java.lang.ExceptionInInitializerError
        at org.jruby.embed.internal.AbstractLocalContextProvider.<init>(AbstractLocalContextProvider.java:42)
        at org.jruby.embed.internal.SingletonLocalContextProvider.<init>(SingletonLocalContextProvider.java:88)
        at org.jruby.embed.ScriptingContainer.getProviderInstance(ScriptingContainer.java:248)
        at org.jruby.embed.ScriptingContainer.<init>(ScriptingContainer.java:228)
        at org.jruby.embed.ScriptingContainer.<init>(ScriptingContainer.java:185)

and

 Caused by: java.lang.RuntimeException: unsupported Java version: 0.9
        at org.jruby.RubyInstanceConfig.initGlobalJavaVersion(RubyInstanceConfig.java:1858)
        at org.jruby.RubyInstanceConfig.<clinit>(RubyInstanceConfig.java:1608)

At least, those are the parts that make sense to me. I hav no idea why I would get the error "java.lang.RuntimeException: unsupported Java version: 0.9", since I am using 1.8.0_20. The rest of the errors I cannot follow. When I click on any of the files giving me an error (say AbstractLocalContextProvider.java:42) Android Studio will show the file but also say "Sources not found". That could be the problem, but I don't know why it can't find the sources.

Thanks for any help,

Brian


Solution

  • Uwe Kubosch (‏@donv70) was able to tell me the solution to this via Twitter, so credit to him, but I wanted to post the solution here in case anyone else needs it.

    It was Java version that was causing the problem. In Ruboto the problem is solved here: https://github.com/ruboto/ruboto/blob/master/assets/src/org/ruboto/JRubyAdapter.java#L137 and for use in Android Studio it is necessary only to include the line: System.setProperty("jruby.bytecode.version", "1.6"); The number of course can be 1.6, 1.7, or 1.8, depending on your version of Java.