1

I try to understand how a .apk file is built from source code. I know that with Android Studio or with the command gradlew the build can be done easily. The Android Sdk provides multiple tools that are used to build a .apk, which most of them I know what they do:

d8: java byte code to dalvik bytecode

aapt2: Compile resources and link them. This tool creates a .apk file of compiled resources. (It also creates R.java to reference the resources in the code.)

apksigner: Takes the .apk and signs it. Otherwise, it cannot be installed on an Android device.

zipalign: aligns uncompressed data of the .apk

The last two tools are used on the apk, which already contains all files. What I do not understand is how compiled code and compiled resources are combined? Before aapt2 was a thing the packaging was done with aapt, but it is not even listed on Androids Documentation. However, aapt is still in the Android Sdk build-tools. The new aapt2 has no option for packaging.

So, the question is which command-line tools and in which order are used to create a complete .apk from source code? Thank you!

EDIT: What I am looking for would be similar to this, where a .apk is created with the command-line step by step. Only the answer uses the old aapt, which I think is not used anymore.

EDIT 2: Thanks to Allen Shaw for giving me the missing information. I was now able to compile an app with aapt2. The answer to my question was simply to zip the aapt2 output and the classes.dex.

JANO
  • 2,995
  • 2
  • 14
  • 29

1 Answers1

2

You may also use aapt2 command

aapt2 link -o output.apk

reference the google document here

Add .dex file into apk:

zip -ur app-debug.apk classes.dex
Allen Shaw
  • 1,164
  • 7
  • 23
  • Thank you for your answer! From the document, I read that `aapt2` only makes a `.apk` with the resource files and not including `.dex` files. The question is how I can create a `.apk` with both dex and resources? – JANO Oct 14 '21 at 08:48
  • 1
    As apk file is simply a zip file, you can use `zip -ur out.apk classes.dex` to add dex file into apk. – Allen Shaw Oct 14 '21 at 08:56