Where to put android library files

Moderators: LCMark, LCfraser

Post Reply
patrickshing
Posts: 4
Joined: Tue Apr 17, 2018 3:31 pm

Where to put android library files

Post by patrickshing » Tue Apr 17, 2018 3:46 pm

We are trying to embed an RFID reader to our android apps, starting to dive in LCB, just finished the Hello World exercise.

Then, we put all android library files in the working folder, and add in these 3 lines in our lcb file:
use com.livecode.foreign
use com.livecode.java
use MyService.java

(MyService.java being one of the source android files provided by the RFID vendor)

When we click the Test button, LCB issues an error: Unable to find imported module 'MyService.java'

So, where should we put the library files?

Please give a direction to proceed.


Thanks group :)

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 160
Joined: Thu Apr 18, 2013 2:48 pm

Re: Where to put android library files

Post by livecodeali » Tue Apr 17, 2018 11:11 pm

At the moment, LCB extensions can only interface with compiled java classes through foreign handler bindings.
So, you will need to compile all the source files into a .jar file (or download a .jar file provided by the vendor) and put it in a folder code/jvm-android next to the lcb file. Then, you will be able to call public methods in classes in the compiled jar.

For example, say the MyService class has a constructor and a public method void doSomething(), and suppose the package name is com.vendor.RFID, then you could create an instance of the class and call doSomething using foreign handler bindings:

Code: Select all

__safe foreign handler JNI_MyServiceConstructor() returns JObject binds to "java:com.vendor.RFID.MyService>new()"
__safe foreign handler JNI_MyServiceDoSomething(in pService as JObject) returns nothing binds to "java:com.vendor.RFID.MyService>doSomething()V"

handler CreateServiceAndCallDoSomething()
    variable tService as JObject
    put JNI_MyServiceConstructor() into tService
    JNI_MyServiceDoSomething(tService)
end handler

patrickshing
Posts: 4
Joined: Tue Apr 17, 2018 3:31 pm

Re: Where to put android library files

Post by patrickshing » Tue Apr 24, 2018 7:28 am

I'm still stuck in linking up with android library!

Using a simple java:
=================================================
package com.example.uhfreader816ubt;

public class HelloAndroid
{
public static String sayHelloAndroid()
{
return "Hello Android!";
}
}
==================================================

I've built the .jar file and put it in code/jvm-android folder alongside my lcb file:
===================================================
library com.livecode.library.uhfreader

metadata version is "1.0.0"
metadata author is "Patrick Shing"
metadata title is "UHF Reader"

use com.livecode.foreign
use com.livecode.java

__safe foreign handler JNI_SayHello() returns JString binds to "java:com.example.uhfreader816ubt.HelloAndroid>sayHelloAndroid()Ljava/lang/String;"

public handler sayHelloAndroid() returns String
return StringFromJString(JNI_SayHello())
end handler

public handler sayHello() returns String
return "hello world"
end handler

end library
========================================================

everything seems smooth in the IDE, no errors; I select the "UHF Reader" in the standalone options and build the apk file.

the working folder structure:
working folder.png
working folder.png (29.69 KiB) Viewed 292 times
seems being deployed into My Livecode Extensions folder correctly:
My LiveCode Extensions folder.png
My LiveCode Extensions folder.png (41.67 KiB) Viewed 292 times
But in the apps, I can only get the straight forward "hello world", and the "Hello Android" seems never got returned (only blank, nothing returns)!

Since the codes had been reduced to an absolute minimum, I feel that I cannot get the jar file embedded in the apk! Please advise how should I move forward.

Thanks.

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 160
Joined: Thu Apr 18, 2013 2:48 pm

Re: Where to put android library files

Post by livecodeali » Fri Apr 27, 2018 1:11 pm

Your code and everything looks correct, with the exception of

Code: Select all

__safe foreign handler JNI_SayHello() returns JString binds to "java:com.example.uhfreader816ubt.HelloAndroid>sayHelloAndroid()Ljava/lang/String;"
As the sayHelloAndroid method is static, your binding string should be

Code: Select all

java:com.example.uhfreader816ubt.HelloAndroid>sayHelloAndroid()Ljava/lang/String;!static

patrickshing
Posts: 4
Joined: Tue Apr 17, 2018 3:31 pm

Re: Where to put android library files

Post by patrickshing » Mon Apr 30, 2018 3:24 am

Excellent! It works now!

Thank you very much for leading me into the door of android library with LC Builder!

Will have some crazy busy days into my RFID project ...

Post Reply

Return to “LiveCode Builder”