Using External JAR libraries with Android

All things Storyboard for the Android platform.

Moderator: jamiev

Using External JAR libraries with Android

Postby thomas » Tue Nov 19, 2013 8:52 am

We have had a number of requests asking about how external Java libraries (JAR files) can
be used with the Storyboard Lua integration on Android.

It is possible to do so, but it requires a bit of preparation work to convert the JAR files from
their generic Java bytecode to the Android specific DEX format. This sample demonstrates
how those libraries can be converted and how to use and reference them from a very simple
Storyboard application.

Thomas

--- From the README.txt ---

This is a sample demonstrating how an external Java JAR library file
can be used by a Storyboard application via the Lua/Java interface.

The archive AJavaLib.zip contains an Eclipse project containing a simple
Java library (ajavalib.jar)

The archive CallJava.zip contains a Storyboard project containing a simple
Storyboard project that has a text area that responds to a press and
calls to a Lua function to get a result to be displayed on screen.

The string to be displayed is obtained by calling through to a Java
function contained in ajavalib.jar in the "CrankTime" class. The loading
of the external library is performed by the loadClassFromJar() Lua
function in external_jar_loader.lua file.

Storyboard Activity Flow:

Storyboard Press Event: gre.press
> Lua Function: get_crank_time()
> Lua Function: loadClassFromJar()
> Java Object Instance: new CrankTime()
> Java Method: CrankTime::whatTimeIsIt()
> Storyboard Data Set: gre.set_data()

In order to prepare the standard Java JAR library to be used in an Android
project, it must first be converted to a DEX format. The Makefile in
the "java" directory of the Storyboard project contains the specific
details of the conversion.

The Android external library loading process is described in the header of
the external_jar_loader.lua file and uses the Android DexClassLoader API:
http://developer.android.com/reference/ ... oader.html
Attachments
StoryboardAndroidJARLoadSample.zip
(153.54 KiB) Downloaded 669 times
thomas
 
Posts: 133
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Using External JAR libraries with Android

Postby DarkWishMaster » Thu Nov 21, 2013 12:18 pm

I have this class:
Code: Select all
package com.mylib;

public class Bird {
   
   protected String name = "";
   
   public Bird(String name){
      this.name = name;
   }
   
   public void set_name(String name){
      this.name = name;
   }
   
   public void print_name(){
      System.out.println(this.name);
   }
   
   public String get_name(){
      return this.name;
   }
}


and I don't know why this doesn't work. I don't see the problem, maybe you can help me, thank you.
Code: Select all
local className = "com.mylib.Bird"
   local myClass = loadClassFromJar(className, jarFile)
   if(myClass == nil) then
      set_msg("No CrankTime class available")
      return
   end
   set_msg("This message is shown")
   --Instantiate a new instance of our class
   local bird = luajava.new(myClass) -- error here?
   set_msg("This message is not shown,so there must be an error in the last statement ")

       bird.set_name("Eagle")
       set_msg(bird:get_name())
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Using External JAR libraries with Android

Postby thomas » Thu Nov 21, 2013 1:21 pm

I am guessing that it doesn't work because you are effectively calling:

new Bird()

You don't have a constructor that takes no arguments, just one that takes a string.

Thomas
thomas
 
Posts: 133
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Using External JAR libraries with Android

Postby DarkWishMaster » Fri Nov 22, 2013 2:34 am

Indeed, that was the problem... thank you!

local bird = luajava.new(myClass)

can I somehow add here arguments to constructor?
local bird = luajava.new(myClass, args) or
local bird = luajava.newInstance(myClass, args)
looks like it doesn't work
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Using External JAR libraries with Android

Postby thomas » Fri Nov 22, 2013 8:53 am

Is 'args' in your example a table or a list of arguments? It should be a list
of arguments, so:

luajava.new(myClass, myFirstArg, mySecondArg, ...)

Thomas
thomas
 
Posts: 133
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Using External JAR libraries with Android

Postby DarkWishMaster » Thu Nov 28, 2013 4:05 am

Hello, I'm trying to load a class in lua like this:
fro example:
Code: Select all
import android.app.Activity;
import android.os.Bundle;

public class  myClass extends Activity {
   
   private String info;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
         
      otherClass newClass = new otherClass(this);
      newClass.dosomething();


When I try to load it in lua with external jar loader I've got:
Have DexClassLoader, loading package_name.myClass
so the bug I guess is here:
Code: Select all
 
local myClass = dexCL:loadClass(className);

What can be the issue? I tried my app as android application, which starts an activity, sets the layout etc. But I need to perform the same in thing in lua, and for this class to work I must provide to it a context
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Using External JAR libraries with Android

Postby thomas » Thu Nov 28, 2013 8:05 am

I'm not sure what you mean by "for this class to work I need to provide it a context".

If you mean a Context in the Android API sense, then you can use the NativeActivity
to get access to a Context:

http://developer.android.com/reference/ ... ivity.html

If you mean context in the sense of 'some sense of where the application is running
and it's stored preferences' then the gre.SCRIPT_ROOT variable can provide the location
of the source files and you can use Android persistent storage classes for preferences.

Thomas
thomas
 
Posts: 133
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Using External JAR libraries with Android

Postby DarkWishMaster » Fri Nov 29, 2013 9:13 am

I just tried to make a SQLite database, and SQLiteHelper constructor request a context to bound database to. So I gave it luajava.nativeActivity() and it works, thank you!
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Using External JAR libraries with Android

Postby DarkWishMaster » Thu Dec 05, 2013 6:20 am

How do I pass a variable to java method?
I have a method which takes as argument a long value, and I have in my lua script:
local number = 10
myObj:callMethod(number), and it doesn't work
but when I pass like this:
myObj:callMethod(10) it works fine.
or:
local hello = "Hello, world"
myObj:printHello(hello) - error
myObj:printHello("Hello, world!") - works fine
I understand that lua variables have different types, but how I convert them to java native data type so I can pass them to my methods?
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Using External JAR libraries with Android

Postby thomas » Thu Dec 05, 2013 8:53 am

You can try explicitly converting it via tonumber() or alternatively you can wrap it in a Java Integer
object (although that should not be necessary). Does it happen to be that this is an overloaded method
with multiple input types?

Thanks
Thomas
thomas
 
Posts: 133
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Next

Return to Storyboard for Android

Who is online

Users browsing this forum: No registered users and 1 guest

cron