Tutorials for creating Native Bindings? - Lua

456 views
Skip to first unread message

João Neto

unread,
Nov 4, 2013, 8:41:44 AM11/4/13
to rob...@googlegroups.com
Hi, I'm trying to add Lua as RoboVM Binding, without success. 

I need to use it like LuaJava, following the Lua Java Tutorial for LibGDX:

Someone can help me, with a tutorial for this?

Thanks.

Niklas Therning

unread,
Nov 5, 2013, 3:15:10 AM11/5/13
to João Neto, rob...@googlegroups.com
Looks like the LuaJava project provides JNI bindings already for LUA so all you have to do is to modify it to compile for iOS and create a static library instead of a dynamic library. You can then link in this static library when you compile your app using RoboVM.


--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

João Neto

unread,
Nov 5, 2013, 5:23:08 AM11/5/13
to rob...@googlegroups.com, João Neto
Master Niklas, thanks for the answer. 
Can you post here an example that how i can create the static library?

Thanks.

Niklas Therning

unread,
Nov 6, 2013, 4:20:57 AM11/6/13
to João Neto, rob...@googlegroups.com
Try this in a Terminal:

mkdir /tmp/lua
cd /tmp/lua

# Download and build lua 5.1.4 for armv7 and i386
tar xvfz lua-5.1.4.tar.gz
cd lua-5.1.4
mkdir -p ios-armv7
bash -c 'cd ios-armv7 && xcrun clang -arch armv7 -mthumb -c -O2 -Wall -DLUA_USE_LINUX -miphoneos-version-min=5.0 -isysroot $(xcode-select -print-path)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk ../src/lapi.c ../src/lcode.c ../src/ldebug.c ../src/ldo.c ../src/ldump.c ../src/lfunc.c ../src/lgc.c ../src/llex.c ../src/lmem.c ../src/lobject.c ../src/lopcodes.c ../src/lparser.c ../src/lstate.c ../src/lstring.c ../src/ltable.c ../src/ltm.c ../src/lundump.c ../src/lvm.c ../src/lzio.c ../src/lauxlib.c ../src/lbaselib.c ../src/ldblib.c ../src/liolib.c ../src/lmathlib.c ../src/loslib.c ../src/ltablib.c ../src/lstrlib.c ../src/loadlib.c ../src/linit.c'
ar rcu ios-armv7/liblua.a ios-armv7/*.o
mkdir -p ios-i386
bash -c 'cd ios-i386 && xcrun clang -arch i386 -c -O2 -Wall -DLUA_USE_LINUX -miphoneos-version-min=5.0 -isysroot $(xcode-select -print-path)/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk ../src/lapi.c ../src/lcode.c ../src/ldebug.c ../src/ldo.c ../src/ldump.c ../src/lfunc.c ../src/lgc.c ../src/llex.c ../src/lmem.c ../src/lobject.c ../src/lopcodes.c ../src/lparser.c ../src/lstate.c ../src/lstring.c ../src/ltable.c ../src/ltm.c ../src/lundump.c ../src/lvm.c ../src/lzio.c ../src/lauxlib.c ../src/lbaselib.c ../src/ldblib.c ../src/liolib.c ../src/lmathlib.c ../src/loslib.c ../src/ltablib.c ../src/lstrlib.c ../src/loadlib.c ../src/linit.c'
ar rcu ios-i386/liblua.a ios-i386/*.o
lipo -create ios-armv7/liblua.a ios-i386/liblua.a -output ../liblua.a
cd ..

# Download and build luajava 1.1 for armv7 and i386
tar xvfz luajava-1.1.tar.gz
cd luajava-1.1
make luajava-1.1.jar
cp luajava-1.1.jar ..
javah -o src/c/luajava.h -classpath luajava-1.1.jar org.keplerproject.luajava.LuaState
xcrun clang -arch i386 -c -miphoneos-version-min=5.0 -isysroot $(xcode-select -print-path)/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk -Isrc/c/ -I$(/usr/libexec/java_home)/include -I$(/usr/libexec/java_home)/include/darwin -I../lua-5.1.4/src/ src/c/luajava.c -o luajava-i386.o
xcrun clang -arch armv7 -c -miphoneos-version-min=5.0 -isysroot $(xcode-select -print-path)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk -Isrc/c/ -I$(/usr/libexec/java_home)/include -I$(/usr/libexec/java_home)/include/darwin -I../lua-5.1.4/src/ src/c/luajava.c -o luajava-armv7.o
lipo -create luajava-i386.o luajava-armv7.o -output ../luajava.o
cd ..

You will now have liblua.a, luajava-1.1.jar and luajava.o in /tmp/lua. The JAR file should be added to your project's classpath. The .a and .o files have to be added to your robovm.xml file in a <libs> section:

<libs>
  <lib>liblua.a</lib>
  <lib>luajava.o</lib>
</libs>

Let me know how it works out.

João Neto

unread,
Nov 6, 2013, 7:11:21 PM11/6/13
to rob...@googlegroups.com, João Neto
Hi, thanks for the answer. I try and post here if works or not.

Bob.Liu

unread,
Nov 27, 2013, 2:07:01 AM11/27/13
to rob...@googlegroups.com, João Neto
Following the example, I have got the 3 files ( liblua.a, luajava-1.1.jar and luajava.o) . And I have added the .a and .o files to my robovm.xml file.
But there is an exception when I load the library with the "System.loadLibrary" method . I think that, the library now is static and I should not use this method . 
I also try to run my program without loading the library, that's failed.  


So I am confused that  how to use the static library ? 



在 2013年11月6日星期三UTC+8下午5时20分57秒,Niklas Therning写道:

Niklas Therning

unread,
Nov 28, 2013, 2:21:14 AM11/28/13
to Bob.Liu, rob...@googlegroups.com, João Neto
You should not use System.loadLibrary(). What is the error you get when you try without loadLibrary()?

Bob.Liu

unread,
Nov 28, 2013, 3:55:59 AM11/28/13
to rob...@googlegroups.com, Bob.Liu, João Neto
The error message is "Could not find LuaJavaAPI class" . Just when I call LuaStateFactory.newLuaState() method.



在 2013年11月28日星期四UTC+8下午3时21分14秒,Niklas Therning写道:

Niklas Therning

unread,
Nov 28, 2013, 5:22:41 AM11/28/13
to Bob.Liu, rob...@googlegroups.com, João Neto
Try to add

  <forceLinkClasses>
    <pattern>org.keplerproject.luajava.**</pattern>
  </forceLinkClasses>

to your robovm.xml

Bob.Liu

unread,
Nov 29, 2013, 4:25:57 AM11/29/13
to rob...@googlegroups.com, Bob.Liu
It works exactly correct. 
Ah hahhh, That is amazing !

Thank you very much !


在 2013年11月28日星期四UTC+8下午6时22分41秒,Niklas Therning写道:
Message has been deleted

Hatef Tadayon

unread,
Feb 17, 2014, 2:20:00 PM2/17/14
to rob...@googlegroups.com, Bob.Liu, João Neto
Hi guys,

I've followed these steps but i keep getting this exception:


Exception in thread "main" java.lang.UnsatisfiedLinkError: Couldn't load luajava: findLibrary returned null 

at java.lang.Runtime.loadLibrary(Runtime.java)

at java.lang.System.loadLibrary(System.java) 

at org.keplerproject.luajava.LuaState.<clinit>(LuaState.java) 

at org.keplerproject.luajava.LuaStateFactory.newLuaState(LuaStateFactory.java) 

at com.me.mygdxgame.LoadScript.<init>(LoadScript.java) 

at com.me.mygdxgame.MyGdxGame.create(MyGdxGame.java) 

at com.badlogic.gdx.backends.iosrobovm.IOSGraphics.draw(IOSGraphics.java) 

at com.badlogic.gdx.backends.iosrobovm.IOSGraphics$1.draw(IOSGraphics.java) 

at org.robovm.cocoatouch.uikit.UIView$Callbacks.draw(UIView.java) 

at org.robovm.cocoatouch.uikit.UIApplication.UIApplicationMain(Native Method) 

at org.robovm.cocoatouch.uikit.UIApplication.main(UIApplication.java) 

at com.me.mygdxgame.RobovmLauncher.main(RobovmLauncher.java)


can you please help me? where do i put the luajava-1.1.jar file?

Niklas Therning

unread,
Feb 18, 2014, 1:34:26 AM2/18/14
to Hatef Tadayon, rob...@googlegroups.com, Bob.Liu, João Neto
The problem with System.loadLibrary() and static JNI has annoyed me for quite some time. So I just made a fix which will make System.loadLibrary() succeed if a static library with a matching name has been linked into the executable. The issue is here: https://github.com/robovm/robovm/issues/271.

The way this works is that the name (with '.a' extension and 'lib' prefix stripped) of any static library linked in at compile time will be saved in the executable. When System.loadLibrary() is called at runtime the first thing it will do is to check against the list of linked in static libraries. If a match is found System.loadLibrary() happily returns without throwing any exception. So as long as the static library has the same name as the dynamic library (except for the extension) System.loadLibrary() will be happy.

Hatef, please wait for the next nightly build to appear over at http://download.robovm.org (20140219). Then follow the instructions to build the static libraries as described earlier in this thread. Then create a libluajava.a static library:

  ar rcv libluajava.a luajava.o

You should have something like this in your robovm.xml (all paths are relative to the robovm.xml file's location):

  <libs>
    <lib>liblua.a</lib>
    <lib>libluajava.a</lib>
  </libs>

Also add

  <forceLinkClasses>
    <pattern>org.keplerproject.luajava.**</pattern>
  </forceLinkClasses>

to your robovm.xml

Then add the luajava-1.1.jar file to your build path and it should work.


Hatef Tadayon

unread,
Feb 18, 2014, 7:56:03 AM2/18/14
to rob...@googlegroups.com, Hatef Tadayon, Bob.Liu, João Neto
Thank you so much Niklas. Thank you for all your great work on RoboVM.
When the nightly is release, do I just switch out the libs for the RoboVM project in my libgdx framework with the new libs from the nightly?

Niklas Therning

unread,
Feb 20, 2014, 1:46:21 AM2/20/14
to Hatef Tadayon, rob...@googlegroups.com, Hatef Tadayon, Bob.Liu, João Neto
You should probably get the libgdx nightly as well. And you should also update the RoboVM for Eclipse plugin to the latest nightly using http://download.robovm.org/nightlies/eclipse/site.xml as update site. Unfortunately the build server which builds the nightlies is down at the moment. So there's no version available yet which has this fix. Hopefully there will be a build tonight.

sunny...@gmail.com

unread,
May 4, 2014, 12:23:49 PM5/4/14
to rob...@googlegroups.com
i have followed the steps and put everything in place, when i try to run a helloworld script, the following exception still occurs:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Couldn't load luajava-1.1: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java)
at java.lang.System.loadLibrary(System.java)
at org.keplerproject.luajava.LuaState.<clinit>(LuaState.java)
at org.keplerproject.luajava.LuaStateFactory.newLuaState(LuaStateFactory.java)

my robovm.xml look like this:
...
 <lib>build/libs/ios/liblua.a</lib>
 <lib>build/libs/ios/libluajava.a</lib>
....
<pattern>org.keplerproject.luajava.**</pattern>

i'm using robovm 0.0.11

sunny...@gmail.com

unread,
May 5, 2014, 12:30:34 AM5/5/14
to rob...@googlegroups.com
solved by the following steps:
1. include luajava source code in libgdx core project instead of luajava-1.1.jar

2. change LuaState.java:
from:
private final static String LUAJAVA_LIB = "luajava-1.1";
to:
private final static String LUAJAVA_LIB = "luajava";

3. in robovm.xml:
 <lib>build/libs/ios/liblua.a</lib>
  <lib>build/libs/ios/libluajava.a</lib>
  <lib>build/libs/ios/luajava.o</lib>

Niklas Therning

unread,
May 6, 2014, 3:32:16 AM5/6/14
to sunny...@gmail.com, rob...@googlegroups.com
It should also work if you rename the libluajava.a file to libluajava-1.1.a.


--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages