Motivation
Today at Red Hat we have another “Day of Learning”. To this day I have never touched Android development, just installing various flavours and configuring it. But I’ve been curious about it for a while now, mostly to be able to fix a little thing here and there in all the great things available on F-Droid. So today was an excellent opportunity!
SDK Installation
The first thing to do is to install Android Studio. This is quite straightforward – download the tarball, unpack it, and run studio.sh
inside it. It even bundles a Java Runtime Environment, so I was quite surprised that it was not missing any dependency even on my radically minimal system (I fully expected having to install tons of stuff in toolbox).
However, I only have sway+wayland these days, and after the initial splash screen Studio just greets you with an empty window. A quick internet search revealed the _JAVA_AWT_WM_NONREPARENTING=1
trick, that makes it kinda sorta start – but none of the popup windows work, and typing into the editor is utterly slow. So, adding back all the X.org/i3 packages, do an OSTree rebuild and reboot, and voilà, Studio starts properly!
The first thing that it does is to download another few GBs of Android SDK, emulator images and such – so I did all that as a separate user, to avoid littering my home directory and making my backup explode.
Hello World!
I followed the Build your first App tutorial, which is quite nice – very hands-on, and you only need to read through the absolute minimum of concepts. A lot of what you do is still mysterious of course – I particularly struggled with the constraint layout editor. But by and large it makes sense, and Kotlin (which I also touched for the very first time today) is nicely clean to read and write – it feels like an useful mix of Java structure, with cleaned up syntax and some JavaScript concepts. The whole Studio integration with type hints, completion, creating emulators for various devices, running the emulator, and installing the built packages into the emulator and my old Nexus 4 phone is very smooth and comfortable. Honestly I expected no less, given how popular it is 🙂 Thus, getting from zero to a Hello World app is quite a nice experience.
The only hiccup that I encountered was that adb devices
kept saying no permissions
for my Nexus 4 – turns out that one needs to switch the USB mode from “charging” to “MTP”. After that, things work great!
I then went on to complete the tutorial with adding a simple UI with a second intent and some interaction (commit), plus some more RTFM about details about App fundamentals.
I then wanted to add a button to take a picture with the default camera app and show the thumbnail. Allegedly that’s fairly simple, and indeed it does switch to the camera app just fine. But once I click on the OK button in the camera app and it returns to my app, the intent’s result code is always -1 and I get no data. I suspected a missing camera permission, but adding android.permission.CAMERA
doesn’t help. I gave up here, mostly because I ran out of time. (commit)
Taking it apart a bit
I work on the command line and in vim all day long, so naturally I wanted to explore how development feels there. So I removed all runtime data, builds (with git clean -fdx
), emulators and settings (rm -r ~/.android)
, etc. and started over. One piece of generic setup is to have Java set up properly, I just point it to the bundled one from Studio:
export JAVA_HOME=~/android-studio/jre
export ANDROID_SDK_ROOT=~/Android/Sdk
Fortunately the documentation for that is really good as well – e. g. it’s easy to build an emulator image for a chosen model and API:
~/Android/Sdk/tools/bin/avdmanager create avd -n emu -d pixel -k "system-images;android-29;google_apis;x86"
~/Android/Sdk/tools/bin/avdmanager list
After that, the emulator can be run with
~/Android/Sdk/emulator/emulator -avd emu &
Building a project in the project directory is also simple:
./gradlew build
And so is installing it into the device (emulator or real phone):
./gradlew installDebug
I haven’t yet found out how to select a target device, as I had the emulator and the Nexus 4 online at the same time. So I just disconnected/stopped the one I didn’t want.
Look at another project
Finally I took a look at an existing app – the first one that came to my mind was Anuto, a really cute little tower defence game that I already sank many hours into. I went through its repository, and now the general layout and what happens makes some vague sense to me. I also tried to build it (with gradle on the command line), which at first failed:
Failed to install the following Android SDK packages as some licences have not been accepted.
Surely there’s a way to overcome that on the CLI, but I just loaded it in Studio once, downloaded the API (the project uses 28, while my hello world uses 29), and after that everything worked fine. I could make a simple modification to the game, installDebug
it, and I saw the result on my phone.
Conclusion
I had no particular goal or reason for this, other than “just for fun” – my developer home is clearly on the Linux desktop and server. But I do enjoy the many nice free apps on AOSP/Android, so if I ever run into a little bug that itches me, I now at least have the tools and initial knowledge to try and scratch it.
It did take most of the workday, but it was enjoyable and worth the effort. Happy hacking!