Menu

Learning Kotlin

By André Jaenisch on 22.05.2023. About 6 minutes reading time. This text is estimated to be fairly difficult to understand.

In the past two weeks I started something new to bridge the time until I can secure a job. So in between talking to recruiters I picked up projects that were wabbering in my head since years.

One of them is learning Android app development. Last time I touched that subject it was only doable with Java, which I won’t get into. It would lead to all sorts of offers I have no interest in whatsoever. The reasons for it will become clear in a moment.

Being a web development person I also tried React Native, because Ionic is based on Angular which I would like to avoid if possible. Mainly because I have different opinions on how to write components.

Now there are other options like Flutter but I did not look into them.

Android Studio

So how do I develop an Android app? Well, it appears that the preferred method is to download and run Android Studio. That’s an IDE like WebStorm or PyCharm that does all sorts of stuff for you. And it suffers from the same pain points I see in every IDE I tried so far: it’s slow. It does too much and gets in my way.

That being said, I downloaded the tarball and unpacked it on this Debian machine I am using for development work. On first start it downloads who knows what so enjoy your meal while you are waiting.

I had to create a .desktop file to make the tarball appear in my application launcher:

#!/usr/bin/env xdg-open

[Desktop Entry]
Name=Android
StartupWMClass=android
Comment=Develop Android applications.
GenericName=Android Studio
Icon=android
Type=Application
Categories=Development;
Path=$HOME/src/android/AndroidStudioProjects/
Terminal=false
Icon=$HOME/bin/tar/android-studio/bin/studio.png
Exec=$HOME/bin/tar/android-studio/bin/studio.sh

Replace $HOME with the absolute part. I have the convention to place downloaded binaries in a bin directory and categorise it by format. In the same spirit my source code is organised in src within my $HOME and then sorted by programming language. I have other Android apps cloned there as well. So in order to separate them from my own I have an additional directory level for my work.

Now I learned Kotlin by watching videos and reading things up. From the official documentation as well as Stack Overflow (where else?).

App idea

I won’t go into too much details about the app itself at this point. If you are interested, write me an email. I am looking for testers who know how to load a debug APK on their phone and provide feedback. Yes, technical experience required at this point. I am aware that there are solutions to distribute it in a more elegant way.

Suffice to say that the app will provide aid in chores at home. I found a niche in which I don’t see many apps - even on Google Play Store!

Constraints

In the beginning I am focussing on F-Droid compatibility, however. I miss so many apps there that my source code will be Free Software. That won’t prevent me from selling it but allows others to learn and improve it.

Also I want to avoid Google services and components as far as I can. Turns out that limits my choices vastly. It really surprises me that there is so little choice here.

But this can be a good thing. It sparks creativity. I am aware that a consistent look and feel is more important for native apps than it is online.

The bottom line will be using LinearLayout and ConstraintLayout for the most part and use some widget.

I am also working offline completely (at this point) by providing the person the ability to manage the aspects of the app themselves instead of hosting it online. That’s also a bonus on privacy.

Luckily Android ships SQLite natively so I can use a CRUD architecture with a database backend. Will be fine.

Learnings

So I know that I/O is an asynchronous operation that should not block the main thread. Looking up how tos pointed me towards Coroutines in Kotlin. So I learned that. I also learned how to wire them up with partials in a RecyclerView for performance reasons. Not that I believe I will reach a limit that warrants these widget but I want to follow Best Practices while I am it.

I was pleasantly surprised to learn that Kotlin has a recommended code format that will lead to a consistent structure across applications. I am using Android Studio for formatting. Also creating new files, imports and refactoring is done through it.

Drawbacks

My machine is a little dated. It’s a ThinkPad X250 with four cores and 3.5 GB of RAM. Turns out Java is even more hungry than Chromium! During development my machine is repeatedly freezing. I have to hard reboot to get back to work.

Android Studio does not like that. I observed loss of work. Sometimes my changes wasn’t saved. Other times the whole file got wiped. Luckily I have git to save me. So once I have done my things in Android Studio I close the program and kill Java process from the command line (hey, why should it free resources? It’s an IDE, d’oh) and open the files in NeoVim.

I had the clever idea to install a language server for Kotlin but that yielded the same performance characteristics. So plain formatting only.

Running Android Studio alone is already taxing. I tried to use the emulator but no dice. So I am using the gradle wrapper to build a debug build and load it on my Android phone using ADB:

./gradlew assembleDebug
./gradlew --stop
adb -d install app/build/outputs/apk/debug/app-debug.apk
adb logcat

Oh, and gradle is spanning as many workers as cores are available and keeps them around for reuse afterwards. So I have to stop them (instead of outright killing those processes). Not cool but better than this overblown IDE.

A logcat can be used to see the stack trace and logging messages (of everything that’s running on Android, so quite noisy).

I am quite disappointed that even though Android has documentation on accessibility I barely see it reflected in the learning material I came across. I only stumbled upon it because I was looking for roles to mark up my elements for headlines, buttons and the like. It appears that it can’t beat HTML on this front. Nevertheless there are still things that can’t be done. But if you don’t include them in your videos and documentation developers won’t know about it.

Speaking of documentation: It’s lacking. See, I’m used to non-inclusive documentation by Google at this point since I have to use Angular for projects and it’s the same over there. That is, the code snippets are incomplete, they don’t tell you where to put the code into, how to name files, in which directory et cetera. I have to figure it out by myself using the bits that I can find. So disappointing but what do you expect from a multi-million company?

Conclusion

So I see an opportunity to educate on Kotlin Android app development while figuring out the parts I wish I knew as I go. This will help me scratch my own itch while perhaps even make some money on the side. I invite you to join me.