Fork me on GitHub
#graalvm-mobile
<
2021-11-27
>
Benjamin C06:11:42

I am curious if this also works on Android, or if it is currently iOS only.

phronmophobic07:11:18

The demo project is iOS only, but I plan on extending it to android at some point. If someone was interested in giving it a shot, I could provide some pointers on how to get started and what the basic structure would look like.

Benjamin C16:11:23

@smith.adriane I would be interested, thanks!

phronmophobic17:11:19

Awesome! The basic plan is pretty simple. You compile your uberjar with native-image as a native library and communicate with the android app via JNI. If you search some combination of jni, android, and ndk, there should be lots of examples of working with native libraries. native-image requires a bunch of special flags, especially when targeting mobile. I followed the example from https://github.com/gluonhq/gluon-samples/tree/master/HelloGluon to find the flags for iOS. It's been a while, but there's a way to view all of the build logs and they print the full native-image command somewhere in the logs. I combined those args with the arglist that's usually required to make clojure compile. You can see the iOS script at https://github.com/phronmophobic/mobiletest/blob/main/scripts/compile-shared#L28.

phronmophobic17:11:34

I know that's a bit vague, but I'm out of town at the moment. I'm happy to answer more questions since that description is light on details.

Benjamin C18:11:02

Nice, thanks for the lead!

frankitox20:01:57

@smith.adriane I'm trying to run the compile-shared script and it throws an error while running the line

clojure -X com.phronemophobic.mobiletest/compile-interface-class
The error is
Syntax error (ClassNotFoundException) compiling at (com/phronemophobic/mobiletest.clj:1:1).
org.graalvm.nativeimage.c.function.CEntryPointLiteral
By any chance, you know what I could be doing wrong? I didn't run download-deps .

phronmophobic20:01:07

It's been a while since I've looked at this. Let me check!

phronmophobic20:01:20

I think the current error you're running into is because the graalvm classes aren't available. You need to compile using graalvm's jdk to make classes like CEntryPointLiteral available.

phronmophobic20:01:55

however, I don't think you'll get very far without doing the prerequisites, https://github.com/phronmophobic/mobiletest#prerequisites

phronmophobic20:01:03

Setting up graalvm is step #2 of the prerequisites

frankitox20:01:13

ahh I see, so before even attempting to run compile-shared I need to compile the project with graal?

phronmophobic20:01:23

compile-shared will compile the project, but compilation uses graalvm, graalvm's llvm-toolchain, and java's arm64 static libraries

frankitox20:01:01

Yes!! Thanks, I had to setup the $PATH to use graal before running clojure -X com.phronemophobic.mobiletest/compile-interface-class

frankitox20:01:42

Sorry about the dummy question 😅

phronmophobic20:01:01

are you trying to compile clojure for android?

phronmophobic20:01:51

If so, then the static libraries that download-deps fetches aren't the right ones. You'll need to get the corresponding android deps.

frankitox20:01:27

Yes, I was just trying to run native-image using the flags you suggested extracting from the HelloGluon project

frankitox20:01:54

Those static libraries are needed to run native-image or later in the process?

phronmophobic20:01:03

I think you'll get linker errors if you don't have them, but I think that's one of the last steps within the native-image compilation process

phronmophobic21:01:00

so if you get that far, you're at least headed in the right direction

frankitox20:01:20

@smith.adriane, by any chance you remember what the https://github.com/phronmophobic/mobiletest/tree/main/conf/capcache is about? Or how you generate it? while trying to adapt the code to android I tried using the capcache folder from a fresh HelloGloun project, but when running https://github.com/phronmophobic/mobiletest/blob/main/scripts/compile-shared#L28 I'm getting the error

Missing CAP cache value for: NativeCodeInfo:PosixDirectives:ConstantInfo:LC_ADDRESS
If using the capcache of mobiletest I get another error
Missing CAP cache value for: NativeCodeInfo:AArch64LibCHelperDirectives:StructInfo:CPUFeatures:StructFieldInfo:fDCPOP

phronmophobic20:01:31

I don't really know what the capcache is. I just copied the args and configs from the gluon project.

phronmophobic20:01:05

Did you compile an android project successfully with HelloGluon?

phronmophobic20:01:18

I don't remember where the capcache files come from. It's possible that they might be different depending on the compiler platform or target platform

phronmophobic20:01:01

I compiled an iOS project using the HelloGluon script and then used the same flags that the HelloGluon script used by looking at the build logs.

frankitox20:01:02

Oh alright, you copied the capcache files from a HelloGluon project?

👍 1
frankitox20:01:16

I think I compiled it successfully, but I'll check just in case

phronmophobic20:01:36

Almost all the flags I use in compile-shared are copied from the HelloGluon script. I think only flags I added for clojure are:

--report-unsupported-elements-at-runtime \
    --initialize-at-build-time="$INITIALIZE_AT_BUILD_TIME" \
    --no-fallback \
    --no-server \
... and
    -J-Dclojure.spec.skip-macros=true \
    -J-Dclojure.compiler.direct-linking=true \
    -J-Dtech.v3.datatype.graal-native=true \
# non-clojure related, but probably added by me
    -J-Xmx20G \
    -J-XX:MaxDirectMemorySize=8G \

frankitox21:01:05

Yes! Those are more or less the flags that I'm using

frankitox21:01:36

Should I first modify the code in src folder before even attempting to run native-image?

phronmophobic21:01:08

The compile script uses https://github.com/frankitox/graal-android/blob/main/src/com/phronemophobic/mobiletest.clj as the main which should work regardless of platform

frankitox21:01:00

nice, thanks!

phronmophobic21:01:32

and you would probably get a different error. Excited to see clojure on android! 🤞

frankitox21:01:09

haha very unlikely 😂, but thank you for the encouragement