Android Build Experiments
Bare minimum android application should consist of a AndroidManifest.xml, may be an Activity or other android component, and may be a launcher for app icon in res.
See directory bare_minimum_android_app for example.
Creating apk using command line tools from Android Sdk consists of the below steps. For reference see directory build_with_sdk_tools
- Generate
R.java - Compile all java source files (including R.java)
- Package
- Sign
- ZipAlign
- Pre-requisites
- Following command line instructions has been tested on Windows environment (Windows 10)
- Assuming you have
%ANDROID_HOME%environment variable has been set to Android sdk path which was in my case "C:\Users\m.qasim\AppData\Local\Android\Sdk" - Assuming you have buildTools/28.0.x/ is at your
%PATH%for easily runningappt/dx/jarsigner/zilalignetc. - Also obviously,
jdkat your%PATH%(i've added the studio embedded jdk to%PATH%)
To verify environment variables on windows, useecho %PATH%orecho %ANDROID_HOME%
- Generate
R.java
R.javacontains all the id's generated from resources to be referenced in code. It can be generated usingaaptcommand line tool available inandroid_sdk/build-tools/28.0.3/appt.
aapt package -f -M ./src/main/AndroidManifest.xml -I "%ANDROID_HOME%/platforms/android-28/android.jar" -S src/main/res -J build/gen -m
- Compile all Java source files (including
R.java)
Since android 8, jack toolchain has been deprecated, we will usejavacfor compiling source files to classes and then will usedxfor dexing.
Since our example has only two source files, i-e MainActivity.java and R.java, we'll compile them both one by one.
javac -d build/classes -bootclasspath %ANDROID_HOME%/platforms/android-28/android.jar src/main/java/io/github/kosmologist/jackandjill/*.java
javac -d build/classes -bootclasspath %ANDROID_HOME%/platforms/android-28/android.jar build/gen/io/github/kosmologist/jackandjill/*.java
Now dexing these class files as Android do not understand java byte directly, instead it uses special bytecode written for android runtime (ART) or previously delvik.
dx --dex --output=classes.dex build/classes
- Package
Since we have transformed our code into dex format, now it's time to package it with our resources as android application package or apk.
aapt package -f -M src/main/AndroidManifest.xml -I "%ANDROID_HOME%/platforms/android-28/android.jar" -S src/main/res -F build/handlbuilt.apk
Now adding classes.dex file to above created package
aapt add build/handbuilt.apk classes.dex
- Sign
Since every android app must be signed before running on android (both debug and release) variants, we'll use default debug keystore packaged with android sdk to sign our debug test apk.
jarsigner -verbose -keystore C:/Users/m.qasim/.android/debug.keystore -storepass android -keypass android build/handbuilt.apk androiddebugkey
- ZipAlign
To compress all resources, we should use toolzipalignas our last step:
zipalign -f 4 build/handbuilt.apk handbuilt-aligned.apk
- Install
To install the above created apk, we can useadbas following or can be manually transfered to sdcard and install using phone packagemanager.
adb install handbuilt-aligned.apk
References
- https://czak.pl/2016/05/31/handbuilt-android-project.html
- https://czak.pl/2016/01/13/minimal-android-project.html
- http://tools.android.com/tech-docs/jackandjill#TOC-Compilation-support
- https://github.com/czak/minimal-android-project
- https://medium.com/authmane512/how-to-build-an-apk-from-command-line-without-ide-7260e1e22676
Without wrapper it's very easy to build apk, we just have to include a build.gradle in the root of project with contents as shown in build_with_gradle_without_wrapper directory.
To Build, just use gradle assembleDebug assuming, gradle is installed and is at %PATH%.
To install gradle, just download it as binary and add the bin directory to %PATH%, or use scoop.sh. (gradle -v)
Note: example above is built using gradle v5.4.1 and android plugin for gradle v3.4.0.
References