Ubuntu for mobile (Android) Java development

This blog is about setting up a proper Linux Ubuntu environment to use when developing mobile applications for Android. Goal is to use 100% Java using JavaFX.

Some while ago, my laptop crashed enormously. Luckily, it also had a SSD drive which still functioned. Here are some steps I took to get this laptop up and running for mobile Java development running Ubuntu.

First step is to goto https://ubuntu.com/download/desktop and download the iso image. Then proceed to https://rufus.ie/en/ to get the (portable) version of Rufus. Format a USB stick, put the iso image on the stick using Rufus. Actually, https://phoenixnap.com/kb/install-ubuntu-24-04 is a great guideline to install Linux. Most difficult part for me was to get my laptop to boot from USB (as you can imagine that should be quite easy, you understand installing Linux is easy).

Install some java distributions:

apt install openjdk-17-jre-headless
apt install openjdk-21-jre-headless
apt install openjdk-17
apt install openjdk-21

Of course, download IntelliJ from https://www.jetbrains.com/idea/download/?section=linux and install it for example in /pub/software (and create a symbolic link called ‘idea’ to it).

Not really necessary, but sometimes useful, I installed SQL Developer (choose other platforms and just download the zip version). I’ve unpacked it in /pub/software/sqldeveloper. To get it to work using Java 17:

vi /pub/software/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf

And put this on the second or third line

SetJavaHome /usr/lib/jvm/java-17-openjdk-amd64

The mentioned Java versions are for many reasons useful to have present on your system, but to develop mobile applications on your system, I use GraalVM with Gluon to build native images you can deploy and run on your phone. It is my experience that it’s a very delicate process to get all your add-ons versions aligned properly. So, next step is to goto https://github.com/gluonhq/graal/releases and download some latest stable and latest dev versions.

Besides the GraalVM with Gluon builds, it’s also useful to have a GraalVM JDK present (without Gluon), use https://github.com/graalvm/graalvm-ce-builds/releases for this. I’ve downloaded and extracted them in /pub/software.

Besides the JDK, it’s also required to download Android NDK (for C or C++ usage) and the Android SDK (because I don’t want to use Android Studio). I’ve downloaded them to /pub/android from https://developer.android.com/ndk/downloads and https://developer.android.com/studio/index.html#command-line-tools-only

When you’ve installed your favorite IntelliJ version, it comes with a Maven version (mine has 3.9.9 shipped), maybe download the 3.8.8 version as well from https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/ and put it in /pub/software. It might look like the following now.

Some packages needed for JavaFX in the future:

apt-get install libfreetype6-dev
apt-get install libgtk-3-dev

I use the following startup script to launch IntellilJ


#!/bin/bash
export M2_HOME="/pub/software/idea/plugins/maven/lib/maven3"
export GRAALVM_HOME="/pub/software/graalvm-gluon-23"
export JAVA_HOME="/pub/software/graalvm-gluon-23"
export ANDROID_NDK="/pub/android/ndk"
export ANDROID_SDK="/pub/android/sdk"
export ANDROID_HOME="/pub/android/sdk"
export ANDROID_SDK_ROOT="/pub/android/sdk"
export ANDROID_NDK_ROOT="/pub/android/ndk"
# export AR="aarch64-linux-android-ar" # export CC="aarch64-linux-android30-clang" # export CXX="aarch64-linux-android30-clang++"
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin:$GRAALVM_HOME/bin:$M2_HOME/bin:$ANDROID_NDK:$ANDROID_SDK:$PATH"
cd ~/.m2 rm settings.xml ln -sf $M2_HOME/conf/settings.xml settings.xml
cd /pub/software/idea/bin ./idea

The AA CC and CXX exports are necessary when you want to compile to native libraries. You’ll also notice the link from ~/.m2 to the Maven settings. This because IntelliJ sometimes resets the Maven settings back to ~/.m2

If you start IntelliJ using this script, open a terminal and type the following:

native-image --version
mvn -version
java -version

When upgrading any part of your setup, make sure to keep the old versions and create a new startup script. Using this method, you can be sure to build the newer version of your mobile app alongside a current version which works.

First step is actually get it to work on your mobile phone, that’s the difficult one. After that, nothing is holding you back.

IntelliJ toolbar back and forward to file button

I’m a fan of using the Ctrl-Alt-Left and Ctrl-Alt-Right keyboard shortcuts, but in some environments (remote desktop connection within a Citrix connection) they just do not work and without enough privileges to change the desktop shortcuts for Linux, the following is nifty.

If not so already, make the toolbar visible: View -> Appearance -> Toolbar

Next, rightclick the Toolbar and select Customize Toolbar. In the Main Toolbar section, below General Actions press Add (Action) or on the dashed line, select add action below from the rightclick-mouse-menu.

Open the Main Menu, open the Navigate Menu. The first 2 options are Back and Forward : ) Hopefully, you have now this:

Generic with check and catch

Suppose you have a @RestController with several calls to a repository (e.g. different attribute searches) and return the results in a list.

It might be useful to create a generic method in your controller so you don’t have that much duplicate code. In the method you can check for content, no content or some error.

Code can look like this:

protected <T> ResponseEntity<List<T>> getListEntriesWithCatch(String argument, Function<String, List<T>> retrievalFunction) {
    try {
        List<T> items = new ArrayList<>(retrievalFunction.apply(argument));
        return items.isEmpty()
                ? new ResponseEntity<>(HttpStatus.NO_CONTENT)
                : new ResponseEntity<>(items, HttpStatus.OK);
    } catch (Exception e) {
        logger.warn("Fetching list entries failes", e);
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

Now you can call it with the repository as argument, like this:

return this.<DomainClass>getListEntriesWithCatch(name, domainClassRepository::findByAttribute);

The generic method is protected, so it can be tested more easily. And because it accepts any repository, you can create a custom function in your test instead of mocking your repo.

@Test
void testGetListEntriesWithCatch() {
MyController controller = new MyController();
Function<String, List<String>> retrievalFunction = arg -> Arrays.asList(COLUMN1, COLUMN2);
ResponseEntity<List<String>> result = controller.getListEntriesWithCatch(SCHEMANAME, retrievalFunction);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals(2, Objects.requireNonNull(result.getBody()).size());
}

changing git ssl to git https

git config –global –unset http.proxy

git config –global credential.helper “cache –timeout=36000”

git config –global credential.helper wincred

git remote -v

git remote set-url origin https://gitlab.server.com/prj/module.git

git pull

fatal: unable to access ‘https://gitlab.server.com/prj/module.git/’: SSL certificate problem: unable to get local issuer certificate

git config –global http.sslVerify false

Username for ‘https://gitlab.server.com’: username
Password for ‘https://username@gitlab.server.com’:
Already up-to-date.

Optional or orElse

What’s more elegant

Optional<Header> resultcode = Arrays.stream(response.getAllHeaders())
        .filter(h -> "X-Header-Result".equals(h.getName()))
        .findFirst();

return resultcode.isPresent() ? resultcode.get().getValue() : "";

Or

String resultcode = Arrays.stream(response.getAllHeaders())
        .filter(h -> "X-Header-Result".equals(h.getName()))
        .findFirst().map(NameValuePair::getValue)
        .orElse("");

return resultcode;