Adding a Third Party Library to the Android Host

Discussion about using Moai SDK - post questions, bugs and issues here.

Moderators: seebs, franciscotufro

Adding a Third Party Library to the Android Host

Postby sgeos » Sat Jul 07, 2012 6:31 am

I have managed to add Lua-SQLite3 to the OSX and iOS hosts, but extending the Android host is not as straightforward as I had anticipated. I am building libluasqlite3.o without any problems, but nm indicates that it does not have any symbols. I am not sure if the problem is the object file or nm. I get a java.lang.UnsatisfiedLinkError when I try to run my APK on the simulator.

Code: Select all
  1. $ ./run-host.sh

  2. # snip initialization

  3. E/AndroidRuntime(  986): FATAL EXCEPTION: main

  4. E/AndroidRuntime(  986): java.lang.UnsatisfiedLinkError: AKUExtLoadLuasqlite3

  5. E/AndroidRuntime(  986):        at com.ziplinegames.moai.Moai.AKUExtLoadLuasqlite3(Native Method)

  6. E/AndroidRuntime(  986):        at com.ziplinegames.moai.Moai.init(Moai.java:283)

  7. E/AndroidRuntime(  986):        at com.sennue.test.MoaiActivity.onCreate(MoaiActivity.java:70)

  8. E/AndroidRuntime(  986):        at android.app.Activity.performCreate(Activity.java:4465)

  9. E/AndroidRuntime(  986):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

  10. E/AndroidRuntime(  986):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

  11. E/AndroidRuntime(  986):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

  12. E/AndroidRuntime(  986):        at android.app.ActivityThread.access$600(ActivityThread.java:123)

  13. E/AndroidRuntime(  986):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

  14. E/AndroidRuntime(  986):        at android.os.Handler.dispatchMessage(Handler.java:99)

  15. E/AndroidRuntime(  986):        at android.os.Looper.loop(Looper.java:137)

  16. E/AndroidRuntime(  986):        at android.app.ActivityThread.main(ActivityThread.java:4424)

  17. E/AndroidRuntime(  986):        at java.lang.reflect.Method.invokeNative(Native Method)

  18. E/AndroidRuntime(  986):        at java.lang.reflect.Method.invoke(Method.java:511)

  19. E/AndroidRuntime(  986):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

  20. E/AndroidRuntime(  986):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

  21. E/AndroidRuntime(  986):        at dalvik.system.NativeStart.main(Native Method)

  22. ^C

  23. $


I think this is a JNI problem. I found a few threads on the net that cover the JNI (link 1, link 2, link 3), but it is not immediately obvious how I need to apply this information to enhance the Android host. What do I need to do to add a third party library to the Android host?
Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: Adding a Third Party Library to the Android Host

Postby ibisum » Sun Jul 08, 2012 1:41 pm

You need to add the JNI side of AKUExtLoadLuasqlite3 .. in a .java file, added to the MOAI host build project. That .java file has to follow the naming conventions used throughout the library interfaces - best would be to have a look at an existing working example from the 3rdparty tree, and see how its mapped. You have to finish the JNI work, it seems.
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Adding a Third Party Library to the Android Host

Postby sgeos » Sun Jul 08, 2012 3:38 pm

Where is the JNI side of AKUExtLoadLuacrypto() defined? It did not appear to be in the 3rdparty tree, nor did it appear to be in src/aku or the Android host. The function is declared in src/aku/AKU-luaext.cpp. I'm sure I've missed something simple and I will keep looking.
Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: Adding a Third Party Library to the Android Host

Postby ibisum » Mon Jul 09, 2012 1:35 am

I'm actually finding it in the Android host template code, which is used by make-host.sh to produce android-host/ (or 'unknown-host' as its called) according to configuration - so some code does not get included if its not configured for inclusion.

Code: Select all
  1.  

  2. w1x@w1x0-dev:~/moai-dev-1.2/ant$ grep -ir AKUExtLoadLuacrypto *

  3. host-source/source/project/src/moai/Moai.java:  protected static native void    AKUExtLoadLuacrypto                     ();

  4. host-source/source/project/src/moai/Moai.java:                  AKUExtLoadLuacrypto ();

  5. libmoai/jni/src/moai.cpp:       extern "C" void Java_com_ziplinegames_moai_Moai_AKUExtLoadLuacrypto ( JNIEnv* env, jclass obj ) {

  6. libmoai/jni/src/moai.cpp:               AKUExtLoadLuacrypto ();

  7.  



By the way: maybe thats a missing step for you, and my rule/advice when working on extending the Android host: ALWAYS modify the Android template code, then re-make the Android project, to test your Android/JNI work. You can re-build the JNI libs and copy them into a 'test Android project' while you do development, but don't forget to re-generate a brand new Android project from scratch, to make sure you haven't broken the make-host.sh script with a change.

Its maybe a little hassle, but remember that there is the host-making phase in the build of the SDK, and if you don't "test through that" you can run into troubles [like, overwriting your valuable code next time you use make-host.sh, if you haven't been already using it..]

(PS- I'm working from repo (never the SDK) ..)
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Adding a Third Party Library to the Android Host

Postby sgeos » Mon Jul 09, 2012 2:46 am

Thanks. I managed to sort out the initialization problem. FWIW, the following is my new friend:

Code: Select all
  1. grep -r1 'whatever_I_am_looking_for' ./dir_it_ought_to_be_in

Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: Adding a Third Party Library to the Android Host

Postby stanziano » Wed May 08, 2013 11:30 am

I'm trying to add LuaSQLite3 to an Android host...I followed your instructions and checked the work made by sgeos in its fork ( https://github.com/sgeos/moai-dev/tree/master/ant ).
I built the host and compiled an example using sqlite3 ( https://github.com/Sennue/rainbow-bubbles from sgeos repositories)..no problems till here.
When I run the app on the device this error message occurs:
Code: Select all
  1. 05-08 18:09:54.831 25712 25712 I MoaiLog : MoaiRenderer runScripts: Running main                                                                                                    .lua script

  2. 05-08 18:09:54.841 25712 25712 I MoaiLog : Database.lua:22: module 'sqlite3' not                                                                                                     found:

  3. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no field package.preload['sqlite                                                                                                    3']

  4. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file 'sqlite3'

  5. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file 'sqlite3.lua'

  6. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file './lua-modules/moai/sqli                                                                                                    te3.lua'

  7. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file './sqlite3.so'

  8. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file '/usr/local/lib/lua/5.1/                                                                                                    sqlite3.so'

  9. 05-08 18:09:54.841 25712 25712 I MoaiLog :      no file '/usr/local/lib/lua/5.1/                                                                                                    loadall.so'

  10. 05-08 18:09:54.841 25712 25712 I MoaiLog : stack traceback:

  11. 05-08 18:09:54.841 25712 25712 I MoaiLog :      [C] ?

  12. 05-08 18:09:54.841 25712 25712 I MoaiLog :      [C] in function 'require'

  13. 05-08 18:09:54.841 25712 25712 I MoaiLog :      Database.lua:22 in main chunk

  14. 05-08 18:09:54.841 25712 25712 I MoaiLog :      [C] in function 'require'



Thus I tried to copy sqlite3.lua, sqlite3.so and libluasqlite3-loader.lua in the source folder of the app but now the error is:

Code: Select all
  1.  

  2. 05-08 18:25:26.386 28424 28424 I MoaiLog : MoaiRenderer runScripts: Running main.lua script

  3. 05-08 18:25:26.396 28424 28424 I MoaiLog : libluasqlite3-loader.lua:47: dynamic libraries not enabled; check your Lua installation

  4. 05-08 18:25:26.396 28424 28424 I MoaiLog : stack traceback:

  5. 05-08 18:25:26.396 28424 28424 I MoaiLog :      [C] ?

  6. 05-08 18:25:26.396 28424 28424 I MoaiLog :      [C] in function 'assert'

  7. 05-08 18:25:26.396 28424 28424 I MoaiLog :      libluasqlite3-loader.lua:47 in function 'load_libluasqlite3'

  8. 05-08 18:25:26.396 28424 28424 I MoaiLog :      sqlite3.lua:49 in main chunk

  9. 05-08 18:25:26.396 28424 28424 I MoaiLog :      [C] in function 'require'

  10. 05-08 18:25:26.396 28424 28424 I MoaiLog :      Database.lua:22 in main chunk

  11. 05-08 18:25:26.396 28424 28424 I MoaiLog :      [C] in function 'require'

  12. 05-08 18:25:26.396 28424 28424 I MoaiLog :      ProgramState.lua:21 in main chunk

  13. 05-08 18:25:26.396 28424 28424 I MoaiLog :      [C] in function 'require'

  14. 05-08 18:25:26.396 28424 28424 I MoaiLog :      main.lua:20 in main chunk

  15.  

  16.  



Does anybody have a suggestion?
stanziano
 
Posts: 10
Joined: Fri May 03, 2013 3:43 am

Re: Adding a Third Party Library to the Android Host

Postby stanziano » Fri May 10, 2013 8:09 am

Problem fixed: the error was using the wrong library and loader files.
The correct ones are here:
https://github.com/sgeos/moai-dev/blob/ ... loader.lua
https://github.com/sgeos/moai-dev/blob/ ... qlite3.lua

Always by sgeos...Thank you very much sgeos!!

It would be really nice to include these changes in the official version and provide a brief documentation!

LuaSQLite3 API support is a MUST in my opinion!
stanziano
 
Posts: 10
Joined: Fri May 03, 2013 3:43 am


Return to Moai SDK

Who is online

Users browsing this forum: No registered users and 1 guest

x