Ever wanted to write C code and run it on Android? Sick of multi-megabyte packages just to do the most basic of things. Well, this is a demo of how to make your own APKs and build, install and automatically run them in about 2 seconds, and with an apk size of about 25kB.
With this framework you get a demo which has:
To make a window with OpenGL ES support
Accelerometer/gyro input, multi-touch
An android keyboard for key input
Ability to store asset files in your APK and read them with AAssetManager
DISCLAIMER: I take no warranty or responsibility for this code. Use at your own risk. I’ve never released an app on the app store, so there may be some fundamental issue with using this toolset to make commercial apps!
Because sometimes you want to do things that don’t fit into the normal way of doing it and all the discussions online revolve around doing it with all the normal processes. And those processes change, making it difficult to keep up and do specific things. By using Makefiles it’s easy to see what exact commands are executed and add custom rules and tweak your build. C is a universal language. Rawdraw operates on everything from an ESP8266, to RaspberryPi, Windows Linux and now, even Android. Write code once, use it everywhere.
When you don’t fill your build process with hills of beans, you end up being left with the parts that are important, and not the frivilous parts. This makes it easier to develop, deploy, etc, because everything takes much less time.
A little bit of this also has to do to stick it to all those luddites people on the internet who post “that’s impossible” or “you’re doing it wrong” to Stack Overflow questions… Requesting permissions in the JNI “oh you have to do that in Java” or other dumb stuff like that. I am completely uninterested in your opinions of what is or is not possible. This is computer science. There aren’t restrictions. I can do anything I want. It’s just bits. You don’t own me.
P.S. If you want a bunch of examples of how to do a ton of things in C on Android that you “need” java for, scroll to the bottom of this file: https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver.c – it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land.
Most of the testing was done on Linux, however @AEFeinstein has done at least cursory testing in Windows. You still need some components of Android studio set up to use this, so it’s generally easier to just install Android studio completely, but there are instructions on sort of how to do it piecemeal for Windows.
Linux install Android Studio with NDK.
This set of steps describes how to install Android Studio with NDK support in Linux. It uses the graphical installer and installs a lot more stuff than the instructions below. You may be able to mix-and-match these two sets of instructions. For instance if you are on Linux but don’t want to sacrifice 6 GB of disk to the Googs.
# sudo apt install openjdk-11-jdk-headless adb unzip zip
Download “Command line tools only”: https://developer.android.com/studio#downloads – you can get a URL and use wget in WSL to download the tools by clicking on the “Linux” toolset, then right-clicking on the accept link and saying copy link to location. Then you can say wget <link> in WSL.
Create a folder for the Android SDK and export it. You may want to add that export to your ~/.bashrc:
You may want to copy-and-paste this project, but, you could probably use it as a submodule. You may also want to copy-and-paste the submodule.
You MUST override the app name. See in Makefile APPNAME – you should be able to include this project’s makefile and override that. You must also update AndroidManifest.xml with whatever name and org you plan to use. That means updating all three fields. Both android:name fields and the package field in the header.
Be sure to uninstall any previously installed apps which would look like this app, if you have a different build by the same name signed with another key, bad things will happen.
You can see your log with:
If your app opens and closes instantly, try seeing if there are any missing symbols:
adb logcat | grep UnsatisfiedLinkError
Because we are doing this entirelly in the NDK, with the JNI, we won’t have the luxury of writing any Java/Kotlin code and calling it. That means all of the examples online have to be heavily marshalled. In rawdraw’s EGL driver, we have many examples of how to do that. That said, you can use the following functions which get you most of the way there.