Please refer to up-to date how to guide here: https://github.com/treeform/glfm
Getting Nim on iOS was not that bad. Android was significantly harder. On iOS you just compile nim to C and include the C files in your project. On iOS you just take c, objc and cpp files and just link them together no big deal.
On Android apps are Java apps. So you have to use the Java to C JNI bridge, and a thing called native-activity and its convoluted with gradle (make tool) calling cmake (another make tool)… I have spent hours looking at link errors.
My wish is to have some thing this in the future:
nim c --os:android foo.nim and get a foo.apk that can run on emulator or a real Android phone.
same with:
nim c --os:ios foo.nim and get a foo.app that can run an emulator or a real iOS phone.
Currently there is no iOS operating system defined. I should add that...
But maybe an outside tool will be better some thing like?
nimOnMobile --os:android foo.nim that does all that android studio/xcode crap?
Should the package builder be part of nim compiler or a separate tool? Thoughts?
It looks like using standard GC I get allocation errors with:
Using default GC I get segfault on this line: https://github.com/nim-lang/Nim/blob/27b081d1f77604ee47c886e69dbc52f53ea3741f/lib/system/avltree.nim#L16 because n is null.
Using --newruntime I get out of memory as soon as I make an allocation.
Using --gc:none I get out of memory as soon as I make an allocation.
This happens with both iOS and Android in a similar way.
I think the memory allocator is not setup/working right? Any ideas of things to try?
Any output to stdout is just ignored on Android you have to use special log function: __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "message");
Is there a way to override echo function to use it instead of stdout?
Seems like if I use
--newruntime -d:useMalloc
I can get around the memory issue. I think there is some thing wrong with the memory allocator when compiling for iOS and android.I have created this PR to get around the Android echo issue: https://github.com/nim-lang/Nim/pull/12203
I have created this PR to create an iOS os target: https://github.com/nim-lang/Nim/pull/12204
This is super awesome, thank you for writing it up.
I'm curious if you've considered using SDL2 for your project, it seems to have pretty good iOS/Android support. Its readme also documents some good tips/tricks for running on Android which might be applicable to all C apps wanting to be run on it: https://github.com/zielmicha/SDL2/blob/master/README.android
Making headway on memory issues. My main problems where mostly from not calling NimMain before I do nim stuff. I think I have spend 60 hours this week looking at Nim's generated C code...
I am stuck on some memory corruption bugs now, which might be my fault.
@treeform You may find my dali project potentially interesting — though it's super early stage as of now (alpha/PoC). I managed to assemble a simple hello-world.apk purely from Nim, absolutely no JDK or Android Studio - though it required the Android NDK (i.e., the gcc cross-compiler).
I later managed to do another PoC of potentially getting rid of Android NDK, and replacing it with Zig.
Currently, I'm working on trying to make stuff more user friendly, and hopefully auto-generated.
Please note it's purely a hobby project, so I absolutely won't promise any timelines.
@lxdong thanks!
@akavel your project is super cool. I did not think that getting rid of Android Studio is possible. I was hoping to generate Android Studio project files + gradle files + cmake files and just run Android Studio via command line.
My other though was that someone would want to use nim as a cross-platform glue while still writing the UI in Java or Kotlin. So they would be using Android Studio.
Same for XCode.
@treeform I think compiling of the app and the bundling should be separate tools/steps. I've been working on wiish as I try to get my own mobile app out: https://github.com/iffy/wiish
It works somewhat well (SDL and webview apps). I'm going to be focusing more on webview apps for now (since that's what I'm making). The goal is to be able to do:
wiish init
wiish run --ios
wiish build --target ios
And end up with an iOS app. The above works today on my computer, at least :) It also somewhat works for Android. There's also a wiish doctor tool to help you get your computer ready to go.
It looks like your wiish project is exactly what I want!
I am working on my own UI thing called fidget ( https://github.com/treeform/fidget/ ) - my long term goal is to make fidget just work on Android and iOS, just like it does on Win/Mac/Linux and Web now.
I'll be looking at your thing very closely. Thank you for doing this.
Here's another way of compiling on Android natively - works great when you have an external monitor and keyboard connected to your phone (the wonders of usbc and bluetooth):
+1 for Nim on Android, didn't knew it's possible to run C on iOS/Android.
A possible option, maybe easier to achieve - to compile Nim to JavaScript and run that JavaScript with Mobile WebView Wrapper.