Things Learned on Android
Programming for mobile devices is fun; it lets you bring your working code along with you. It’s a whole new definition of portable(the old definition)!
After coding C++ on desktop PCs for over a decade, I’ve become used to several tools and idiosyncracies that have no analog in the Android development ecosystem. In the android-gl and FlickerCladding projects, I have attempted to extend the general OpenGL app framework of RiftSkeleton onto this new platform with much success. This has resulted in some oddities in how source is organized, but it does work, making development a pleasure.
Over the course of a few months working with the NDK, I’ve learned a few things.
Delete the emulator
The first thing to know is that the emulator is worse than useless. Do not install it. It’s completely obviated by the desktop build target. If any emulated devices pop up unbidden in adb, run adb kill-server
to make them go away.
Info on the web about this is out-of-date
Searching for info on any of this stuff is horrendously difficult because the tools are in heavy flux. Check the date on the StackOverflow answers you find; if it’s before 2016 they are probably useless.
I find the same problem with Unity 5: it’s great to have all this message traffic and a vibrant community offering help on the web, until the way things are done changes and all that old info is useless. By then you’re flooded with it, it only serves to confuse and you can’t make it go away!
Building with Gradle
Gradle is the new tool for building Android Studio projects that takes the place of make
or ant
or any of probably about dozens of other tools. It’s where you control headers and libraries, compiler flags, pre- and post-build steps, and more. I personally find searching for info about it to be a large pain, and I am hoping gradle build files can be generated by a tool like CMake so I won’t have to bother learning it. Now that I’m wishing for new build tools, how about a replacement for CMake? It does manage to hit (almost)every platform, but that language is far from pretty. Gradle uses proper java, which has a nice elegance to it. Maybe something that uses lua?
Hierarchical config
build.gradle
files can include one another hierarchically somehow. You want the one under app/
.
Including C++’s STL
If you want to use the STL, you have to add it to build.gradle
:
android.ndk {
stl = "stlport_static"
}
There is also another implementation called gnustl
. Why you would want to use one over the other, I am not sure. I can only guess that they are both incomplete.
Opening files from device local storage
If you want to read or write files on the device’s local storage, you must add an explicit flag granting permission for the app to do so(as of Android K or so).
Add the lines:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to AndroidManifest.xml. adb
is an incredibly handy tool for push
ing and pull
ing files to and from the device to your development PC.
More permissions
Permissions have been tightening down in the later Android releases(K,L,M,N). After an app has been granted file read/write permissions via the manifest, the device user may still need to manually grant it access to the filesystem. If this prompt is not displayed automatically, you’ll have to go into Settings somewhere and find the likely tremendous list of apps installed on your device. Find the app in question and flip its toggle switch to on to allow it filesystem access.
Handling screen rotation
Add this line to the manifest in the activity
node:
android:configChanges="orientation|keyboardHidden|screenSize"
If it’s not there, the GL surface will be destroyed and re-created on each rotation, which is awkward.