Search code examples
androidgradlebuildapk

How can I build an Android apk without Gradle on the command line?


I wrote an Android app that uses no dependencies or modules, has a single activity, and has a single layout file.

How can I build an apk file of my app on the command line without using Gradle (or other "build systems" or "dependency management" software)?


Solution

  • Use the following steps to build your apk manually, if you don't want use ant/gralde to build. But you must have Android SDK installed at least.

    • create R.java from aapt

    • use javac to compile all java source to *.class

    • use d8 (or dx in older SDKs) to convert all *.class to dex file, e.g output is classes.dex

    • create initial version of APK from assets, resources and AndroidManfiest.mk, e.g output is MyApplication.apk.unaligned

    • use aapt to add classes.dex generated in step 3 to MyApplication.apk.unaligned

    • use jarsigner to sign MyApplication.apk.unaligned with debug or release key

    • use zipalign to align the final APK, e.g output is MyApplication-debug.apk or MyApplication-release.apk if signing with release key

    • Done

    I have created a sample script to do all the stuffs above, see here

    Actually, Some articles have discussed this topic, see the following links.

    https://www.apriorit.com/dev-blog/233-how-to-build-apk-file-from-command-line

    https://spin.atomicobject.com/2011/08/22/building-android-application-bundles-apks-by-hand/