Getting started with Java bindings

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Getting started with Java bindings

Post by KimD » Tue Apr 07, 2020 12:11 am

I have a two (hopefully modest) tasks* that I'd like to use LCB + FFI + Java to achieve on Android. Knowing almost nothing about any of these three, I thought that I'd start with a very small training exercise. I had previously completed the LCB Hello World example. So:

1) I cut-n-paste the javauuid example code provided in livecode.com/docs/9-5-0/extending-livecode/extending-livecode/

2) Using Tools > Extension Builder - this would not initially compile, but after a little research I added "use com.livecode.foreign" and "use com.livecode.java", and then it did compile & package. I'll put the final code below, but the only changes that I made are the 2 lines mentioned.

3) I managed to add javauuid to my Tools > Extension Manager > Library without any problems.

4) I then created a stack, added a button, and put this in the mouse up handler - Answer GetRandomUUIDJava()

When I click on the button in the Windows IDE - LC gives me an error message - execution error at line 4 (LCB Error in file C:/Users/Kim/Desktop/LCB_UUID/Test_UUID.lcb at line 30: Unable to bind foreign handler com.livecode.library.test_uuid.JNI_RandomUUID)

When I click on the button on my Android test device - the button just blinks but does nothing.

Should LCB + FFI + Java work on Windows? Ultimately i don't need a Windows deploy. I was only hoping to be able to test on Windows.

How should I be doing debugging LCB + FFI + Java? How can I narrow down what my Android deployment is unhappy about?

Plus - any advice on how to get around this binding error would be much appreciated.

Thanks in advance and stay safe

Kim (LC 9.5 Indy, Windows 10, Android 8.1)

* FYI - the two tasks that I'm hoping to build up to are:
- return the filepath to the Android system downloads folder (the one that gmail writes downloaded files to); and
- return a list of the IDs of bluetooth devices that are in range of the Android device

Code: Select all

library com.livecode.library.test_uuid

metadata title is "Java UUID"
metadata author is "LiveCode"
metadata version is "1.0.0"

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

// Bind to the static randomUUID() method of the java.util.UUID class
__safe foreign handler JNI_RandomUUID() returns JObject binds to "java:java.util.UUID>randomUUID()Ljava/util/UUID;!static"

// Bind to the toString() instance method of the java.util.UUID class
__safe foreign handler JNI_UUIDToString(in pUUID as JObject) returns JString binds to "java:java.util.UUID>toString()Ljava/lang/String;"

// Library public handler - this will be accessible from LiveCode
// when this library is loaded. The following comment block will
// be used to generate the documentation
/**
Returns a new random UUID.

Returns (String):
The string representation of a type 4 (pseudo randomly generated)
UUID.
*/
public handler GetRandomUUIDJava() returns String
    // Call the static randomUUID method to return an instance of
    // the UUID class
    variable tUUID as JObject
    put JNI_RandomUUID() into tUUID

    // Call the toString method on the UUID instance to obtain the
    // (java) string representation of the UUID
    variable tUUIDString as JString
    put JNI_UUIDToString(tUUID) into tUUIDString

    // Convert to a LiveCode String and return
    return StringFromJString(tUUIDString)
end handler

end library
Last edited by KimD on Wed Apr 08, 2020 5:31 am, edited 1 time in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Getting started with Java bindings

Post by bogs » Tue Apr 07, 2020 9:25 am

Heya KimD,

This isn't an answer to your question, but, would you mind putting the code into a code block? It is the button right after "quote" above the editor you reply in, just high-light all the code at the bottom of your post and click the code display button :)
Image

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings

Post by KimD » Wed Apr 08, 2020 5:32 am

Hi Bogs. Well, no other advice so far, but at least I've learnt how to create code blocks ;-) Thanks. Kim

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Getting started with Java bindings

Post by bogs » Wed Apr 08, 2020 5:55 am

Heh, I love code blocks, although I do wish they let the specific color coded formatting through as well as the ability to have the indents.

I really wish I had more to contribute to your questions, but have less than no knowledge about how LCB and the FFI works, and JAVA is one of the many languages I have never looked at, much less learned about.

Hopefully someone who has will chime in and give us a clue, though.
Image

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings

Post by KimD » Wed Apr 08, 2020 6:07 am

Thanks. I've got about 1 day on each of LCB, FFI & Java. It's a steep learning curve. BTW - is there any LC Forum that you're NOT following ;-)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Getting started with Java bindings

Post by bogs » Wed Apr 08, 2020 6:23 am

Heh, well, I'm not on the use-list for sure, it was one sign up too many at the time :D I also don't contribute at Stack Overflow, and I have no FB account (or Twitter or any of those types of things), so there are plenty of places where people are safe from me :twisted:
Image

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Getting started with Java bindings

Post by PaulDaMacMan » Wed Apr 08, 2020 10:37 pm

KimD wrote:
Tue Apr 07, 2020 12:11 am
Should LCB + FFI + Java work on Windows? Ultimately i don't need a Windows deploy. I was only hoping to be able to test on Windows.
From the Extending LiveCode Guide:
Java

Java bindings are currently supported on Android, Mac and Linux. On desktop platforms the IDE will attempt to find the correct setting for the JAVA_HOME environment variable. As this can vary from distro to distro on Linux, this may fail - in this case ensure you set JAVA_HOME in script or using the message box.
But I would think, as long as you can compile it should still work once you deploy it onto your Android device? I could be wrong. Something with binding string? Sorry, I've been mostly doing Objective C & C stuff with FFI, not JAVA.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings

Post by KimD » Thu Apr 09, 2020 12:34 am

Thanks Paul. Yes - I'd also got to thinking that Windows must be unsupported, hence my questions 2 & 3. Given that I cut-n-paste the code from the extending LC guide, I'd have hoped that it would work on Android. I've currently got no idea how to even begin to debug this. I tried capturing "the result", but that didn't work.

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Getting started with Java bindings

Post by PaulDaMacMan » Sat Apr 11, 2020 3:03 pm

KimD wrote:
Thu Apr 09, 2020 12:34 am
Given that I cut-n-paste the code from the extending LC guide, I'd have hoped that it would work on Android.
I've currently got no idea how to even begin to debug this.
I'd hope that would be the case too. I would think it should also work on any platform where a Java VM is setup properly, but I assume there's a reason they haven't officially supported JAVA dev on Windows. I've found it is a pain to try develop for Android target in LC too (not just LCB) because in a standalone app you often don't get the kind of feedback you would in the IDE. I've pretty much stopped working on the few projects for Android that I had started. But when I was, I gave up on using the Android Dev Kit device emulator and started testing my code directly on my phone (and tablet, which has been retired) as this was actually quicker due to the emulator having to translate ARM instructions to Intel. That has actually changed though, now you can build for Android X86/X86_64 in LiveCode so theoretically that would make developing and testing on the computer a bit faster.
I tried capturing "the result", but that didn't work.
Do you have any "try" statements mixed in on the script side? If not maybe add one and test "the error" and see if there's anything there. Maybe "log" statements too? Maybe when you cut and pasted there was extra characters mixed in which could screw up the binding string? If I get a chance, I'll re-setup Android Dev Kit (new laptop) and try it out to see if I get the same problems (running macOS).
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings

Post by KimD » Tue Apr 14, 2020 4:19 am

Thanks Paul.

Try & Catch don't seem to have been implemented in LCB. They don't appear in the dictionary and I get a compile error if I try to use them.

I haven't worked with Log before. I've tried a few things, based on what I read in the dictionary, but I can't work out how to output what has been logged.

Regards

Kim

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Getting started with Java bindings

Post by PaulDaMacMan » Tue Apr 14, 2020 11:20 pm

KimD wrote:
Tue Apr 14, 2020 4:19 am
Thanks Paul.

Try & Catch don't seem to have been implemented in LCB. They don't appear in the dictionary and I get a compile error if I try to use them.

I haven't worked with Log before. I've tried a few things, based on what I read in the dictionary, but I can't work out how to output what has been logged.

Regards

Kim
I didn't mean try/catch in LCB, which as you said, LCB doesn't have. I meant wrapping your LCS call to the handler on the script side with try/catch/error, so that you can see what error is happening, or if anything is happening, while it's running on your Android device. If you just have try/end try wrapped around the call on the PC IDE, it will ignore any error, without it will present the normal error and go to the script in the editor where the error occurs... If it's a standalone you don't have that, but I believe you still capture the error and put it into a field or something in your standalone so that you can see it. It would be great if there was Android/iOS versions of the IDE itself but there isn't.

Are you sure that the library is being included in the standalone apk when you build it? Your lib is installed in the IDE and it's checked in the inclusions tab?
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings - Solved

Post by KimD » Wed Apr 15, 2020 3:48 am

Thanks Paul. You solved it. I had - Standalone App Settings > Inclusions > Search for required inclusions

When I changed it to - Standalone App Settings > Inclusions > Select inclusions, and manually selected my new library

It worked fine on my Android device. I should have thought of that. Doh!

Thanks again

Kim

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Getting started with Java bindings - Solved

Post by PaulDaMacMan » Thu Apr 16, 2020 2:10 am

KimD wrote:
Wed Apr 15, 2020 3:48 am
Thanks Paul. You solved it. I had - Standalone App Settings > Inclusions > Search for required inclusions

When I changed it to - Standalone App Settings > Inclusions > Select inclusions, and manually selected my new library

It worked fine on my Android device. I should have thought of that. Doh!

Thanks again

Kim
:D
YEAH! Glad I could help! I'm sure I've forgotten to do that myself at least a few times, hah!
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

julianmoors
Posts: 5
Joined: Sun May 23, 2021 2:48 pm

Re: Getting started with Java bindings

Post by julianmoors » Sat May 29, 2021 11:07 pm

Hi Kim,

I managed to get your Java UUID test library working following your steps, but when I try to run it using a stack from within the IDE, I get the error "Could not initialise the Java Runtime Environment". I did however manage to get it working by installing the .apk onto my phone. Do you know any way to shorten this build cycle to something on the computer for testing? I don't like the idea of having to build and then install onto my hardware if I can help it. I have a Mac running macOS Big Sur with JDK 1_8, LiveCode Community 9_6_2 and Android Studio 4_2.

Regards,
Julian

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Getting started with Java bindings

Post by KimD » Sun May 30, 2021 5:25 am

Hi Julian

Sorry, but I'm not a good person to ask. A year ago I spent a few days experimenting with LCB + FFI, but it all got too hard so I moved back to regular LC. It probably would have gone better if I'd known ANYTHING about Java, but I don't so...

Like another responder to my original post I also gave up on the Android emulator - it was just too slow. Instead I create the .apk on Windows, load the apk onto GoogleDrive on Windows, load the apk off GoogleDrive onto a real Android test device, then install it. I can do this in less than 2 minutes. Would I rather that it only took 20 seconds to deploy to test - sure - but it's not a biggie. It gives me a chance to put another coffee on.

Yes - debugging using an Android device is harder than debugging in the IDE. I mostly get around this by:
- just getting my app working in the IDE before deploying to test on Android. The VAST majority of the coding mistakes that I make are not platform specific and show up just as easily on the Windows IDE.
- including lots of logging in my standalones, then adding a button that allows me to view the log on Android. I haven't (yet) encountered a problem that couldn't be identified by just smothering it in logging ;-)

Regards

Kim

Post Reply

Return to “LiveCode Builder”