Using JRuby to directly call Android Java Methods
Asked Answered
V

1

7

I've been recently exploring how to access Android's Libraries via JRuby in SL4A. I know it is possible to design apps with Ruboto, but I just want to right a simple script to access APIs that current SL4A doesn't offer. I can import normal jars and such but I haven't been able to get Android's API. In specific I want to access 'android.nfc'. Is there a way do accomplish this that I haven't figured out yet or is it possible to not work; SL4A does state that JRuby offers a direct API bridge though.

Thanks, Clement

Varese answered 1/10, 2012 at 18:39 Comment(1)
The android.nfc package is part of android.jar, so you should be able to access it. Is it the getSystemService(NFC_SERVICE) call that you are having trouble with?Alphabetical
C
0

Can you include your Jruby code? Are you building your classpath properly and then doing the java_import as well?

I have a ruby file just for loading up my jars into my classpath that I require in every .rb file called java_jars.rb it contains the following:

Dir["target/dependency/\*.jar"].each { |jar| require jar }

then in the main ruby file do this:

include Java
require 'java_jars'
java_import 'com.package.name.blah.ApiClassName'

def methodName
    apiObject = com.package.name.blah.ApiClassName.new
    # don't forget methodName becomes method_name in JRuby
    apiObject.method_name
end
Chary answered 24/4, 2013 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.