Compile APK online

It was long time i was dreaming about ability to compile apk online. So this blog post (tutorial) will be how to prepare C9 (C9 was bought by Amazon AWS and does not really offer free containers any more) or Goorm online IDE or any similar based on ubuntu linux to be able to develop online. This should also work on local linux installation or even Windows subsystem for Linux if you are on Win10

This post will not be about “upload my zip online and get apk”. You have to set up environment in virtual server. My suggestion is go with Goorm IDE they give you up to 5 containers for free where you can set up different environment for various development things.

First of all, create new empty ubuntu box and resize it to 1Gb RAM and 5GB storage goorm gives you 1GB ram and 10GB storage out of the box. Simple APK compile stull will require about 1GB of storage, but if you decide to go on gradle dependency management system- storage will grow to 2.5GB.

Prepare your system

I prepending everyhing with sudo command, in case any permission will be missing.

So lets begin from very basic. At first you need JAVA, so add OpenJDK repository and install java

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update

Install Java headless, because you do not need GUI here

sudo apt-get install openjdk-8-jdk-headless

Download SDK zip from android home. You may use my provided link

wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

Make directory for android SDK and extract ZIP

sudo mkdir -p /opt/android-sdk
sudo unzip sdk-tools-linux-4333796.zip -d /opt/android-sdk

Now add environment variables to your system.

sudo nano /etc/profile

and at the very end add following lines

Always check if copying from website does not ruin you double-quotes

export ANDROID_HOME="/opt/android-sdk"
export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64"
export PATH=$JAVA_HOME/bin:$ANDROID_HOME/tools/:$ANDROID_HOME/platform-tools/:$PATH

Save and exit by pressing CTRL+X on keyboard and confirming that you want to save changes by Y. Restart your machine or environment.

Now you need to add SDK, Tools and platform stuff. I have chosen 24 version so i have used this command

sudo /opt/android-sdk/tools/bin/sdkmanager "tools" "platforms;android-24" "build-tools;24.0.3"

in case you want something else or want to see available list use sdkmanager –list command

Looks like everything is prepared, now need “Hello world!” android application. You may need to prepare it according structure and create all files or You can download this HelloAndroid project and extract in in your workspace.

Steps needs to be done to get APK file

  1. Generate R.java file AAPT
  2. Compile java file with javac
  3. Translate it to Dalvik Bytecode with DX
  4. Make apk with AAPT
  5. Align apk package with ZIPALIGN
  6. Sign your apk file with APKSIGNER

AS you can see, there is a step to sign you apk install file with certificate. You can create your own. In main project directory, the same where AndroidManifest.xml file exist, run following command to create private key:

keytool -genkeypair -validity 365 -keystore mykey.keystore -keyalg RSA -keysize 2048

Answer all questions and provide password.

Thats it. Now you can create APK file from command line. To create automated, here is prepared bash script witch you should save in project root directory (the same as android manifest) and run to build your project. I have chosen to name it make.sh

make.sh
#!/bin/bash
 
set -e
 
AAPT="/opt/android-sdk/build-tools/24.0.3/aapt"
DX="/opt/android-sdk/build-tools/24.0.3/dx"
ZIPALIGN="/opt/android-sdk/build-tools/24.0.3/zipalign"
APKSIGNER="/opt/android-sdk/build-tools/24.0.3/apksigner"
PLATFORM="/opt/android-sdk/platforms/android-24/android.jar"
 
echo "Cleaning..."
rm -rf obj/*
rm -rf src/in/ignas/helloandroid/R.java
rm -rf bin/*
 
echo "Generating R.java file..."
$AAPT package -f -m -J src -M AndroidManifest.xml -S res -I $PLATFORM
 
echo "Compiling..."
javac -d obj -classpath src -bootclasspath $PLATFORM -source 1.7 -target 1.7 src/in/ignas/helloandroid/MainActivity.java
javac -d obj -classpath src -bootclasspath $PLATFORM -source 1.7 -target 1.7 src/in/ignas/helloandroid/R.java
 
echo "Translating in Dalvik bytecode..."
$DX --dex --output=classes.dex obj
 
echo "Making APK..."
$AAPT package -f -m -F bin/hello.unaligned.apk -M AndroidManifest.xml -S res -I $PLATFORM
$AAPT add bin/hello.unaligned.apk classes.dex
 
echo "Aligning and signing APK..."
$ZIPALIGN -f 4 bin/hello.unaligned.apk bin/hello.apk
$APKSIGNER sign --ks mykey.keystore bin/hello.apk

In case you have used different versions in sdkmanager command- edit path in header accordingly. Just run this script with following command. If you can not run it chmod +x make.sh

./make.sh

Script will ask password for your keystore that you provided earlier.

This solution works if your application does not have any dependencies on external libs and gradle wrapper is not used. Probably, automated install script can be made but that's for another times. In case you have any suggestions, comment or anything worth to say- comment below.

Mohamed, 2019/06/01 05:21
Thank you so much for this tutorial, I followed the steps you showed and I was able to successfully compile/build a working APK.

Love your content and the way you present your content.

I'm digging deeper inside your blog to find even more awesome stuff like this.
ignas, 2019/06/06 08:04
Hi Mohamed,
Nice to know that this information assisted you. I'm now doing some experiments with compiling android apps using gradle so in near future I hope to blog follow up
Enter your comment. Wiki syntax is allowed:
If you can't read the letters on the image, download this .wav file to get them read to you.
 
  • blog/190226_compile_apk_online.txt
  • Last modified: 2021/07/15 09:55
  • by Ignas