Good morning
I am trying to create my first .apk (android app).
I have managed to write a simple console based app using the Nim scripting language. (Managed to compile and test it on a Docker ubuntu image - to create a Windows 10 executable, also a working MacOS binary that executes correctly.)
However, now I want to see if I can compile and package my nim program to an APK that i can copy to an android device and run.
I read this: https://nim-lang.github.io/Nim/nimc.html#cross-compilation-for-android
I managed to successfully run this : nim c -c --cpu:arm --os:android -d:androidNDK --noMain:on hello.nim
But now I need guidance (exact steps to follow) to do the follow: (extract from the guide)? P.S I want to do this on the same Docker Ubuntu image (I connect to the container using VS Code)
Add the generated C files to CMake build script in your Android project. Then do the final compile with Android Studio which uses Gradle to call CMake to compile the project.
I am one step closer, I created a new docker container - after pulling this image: docker pull bitriseio/android-ndk
Then I installed the following in that container:
apt-get update apt-get install clang apt-get install mingw-w64 apt-get install -y curl curl https://nim-lang.org/choosenim/init.sh -sSf | sh apt-get install gcc-arm-linux-gnueabihf export PATH=/root/.nimble/bin:$PATH
I then managed to compile my nim program successfully using this line
nim c --cpu:arm --os:linux --parallelBuild:1 --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" ./hello.nim
root@df06d6ea4b99:/src# nim c --cpu:arm --os:linux --parallelBuild:1 --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" "NimProject/hello.nim" Hint: used config file '/root/.choosenim/toolchains/nim-1.0.6/config/nim.cfg' [Conf] Hint: used config file '/src/NimProject/nim.cfg' [Conf] Hint: system [Processing] Hint: widestrs [Processing] Hint: io [Processing] Hint: hello [Processing] Hint: arm-linux-gnueabihf-gcc -c -w --sysroot= -I/root/.choosenim/toolchains/nim-1.0.6/lib -I/src/NimProject -o /root/.cache/nim/hello_d/stdlib_io.nim.c.o /root/.cache/nim/hello_d/stdlib_io.nim.c [Exec] Hint: arm-linux-gnueabihf-gcc -c -w --sysroot= -I/root/.choosenim/toolchains/nim-1.0.6/lib -I/src/NimProject -o /root/.cache/nim/hello_d/stdlib_system.nim.c.o /root/.cache/nim/hello_d/stdlib_system.nim.c [Exec] Hint: arm-linux-gnueabihf-gcc -c -w --sysroot= -I/root/.choosenim/toolchains/nim-1.0.6/lib -I/src/NimProject -o /root/.cache/nim/hello_d/@mhello.nim.c.o /root/.cache/nim/hello_d/@mhello.nim.c [Exec] Hint: [Link] Hint: operation successful (14503 lines compiled; 1.093 sec total; 16MiB peakmem; Debug Build) [SuccessX]
but now what do i have to do to package this binary that was created to get an APK file that I can copy to my android device?
Good morning! Congratulations on taking the first steps towards creating your Android app with Nim. Compiling your Nim program to an APK involves several steps, including integrating your C files with an Android project and finalizing the build using Android Studio.
To help you compile and package your Nim program into an APK, follow these steps:
Set Up Your Android Project:
Create a new Android project in Android Studio. Include a native C++ project setup by selecting the appropriate option during project creation. Add Generated C Files:
After running the Nim command (nim c -c --cpu:arm --os:android -d:androidNDK --noMain:on hello.nim), locate the generated C files. Copy these files into the cpp directory of your Android project, typically found under app/src/main/cpp. Configure CMake:
In your Android project, edit the CMakeLists.txt file to include the generated C files. You may need to specify the correct paths and ensure all dependencies are accounted for. Set up the appropriate build configurations for your target architecture (ARM in this case). Build the APK:
Sync the Gradle project in Android Studio to ensure all configurations are updated. Build the project by selecting "Build > Make Project" or using the corresponding Gradle task. Once the build is complete, locate your APK file in the app/build/outputs/apk directory. Test the APK:
Transfer the APK file to your Android device and install it. You can use ADB (Android Debug Bridge) or any other preferred method. For more detailed guidance and resources on creating Android apps with Nim, click here.
This process will allow you to create and test your first Android app using Nim. If you encounter any specific issues or have further questions, feel free to ask!