Converting APK file to AAB

Converting APK file to AAB

The process of converting an APK file to AAB is fairly simple but time-consuming,

First the required stuff, you will need
1. apktool
2. bundletool
3. aapt2

You will also need Java installed to run those stuff

Anyway, the steps are

Step 1: Decompiling APK and Recompiling APK

Decompile the APK that we want to convert with apktool, we can use the command below

java -jar /path-to/apktool.jar d app.apk

After running the command, there should be a folder called app, which has the decompiled APK files

Then we recompile the decompiled APK with the command below

java -jar /path-to/apktool.jar b ./app

Step 2: Compiling the resources

After decompiling, you will need to compile the resource file from the decompiled APK, use the command below to do so

aapt2.exe compile --dir ./ap/res -o res.zip

After that, res.zip will be generated in your working directory

Step 3: Linking the resources

Then after compiling the resources, we need to link them, use the command bellow

aapt2.exe link --proto-format -o base.zip -I android.jar --manifest ./app/AndroidManifest.xml --min-sdk-version 7 --target-sdk-version 30 --version-code 1 --version-name 1.0 -R res.zip --auto-add-overlay

What is happening with those command arguments?

Well, I will explain some of the arguments.
-I this specifies the path to platform’s android.jar, You can get the jar from platform folder in Android SDK, for example, mine is at /AppData/Local/Android/Sdk/platforms/android-32/android.jar.
--manifest this specifies the path to the decompiled APK Manifest file.
--min-sdk-version this sets the default minimum SDK version to use.
--target-sdk-version this sets the default target SDK version to use.
--version-code this specifies the version code to use.
--version-name the same but for version name.

You can get the complete detail here aapt2 Link Options.

After running the command, you should see base.zip in your working directory, that is the linked resources APK.

Step 4: Unzipping the base.zip

Unzip the generated base.zip file to a separate folder, in this tutorial we will use base as the output directory.

Step 5: Structuring folder

After unzipping, go to the output directory then you should see these files and folders

res/
AndroidManifest.xml
resources.pb

Then create a folder called manifest and move the AndroidManifest.xml to the manifest folder, and you should see these in base directory

manifest/
AndroidManifest.xml
res/
resources.pb

After that, copy the whole assets and lib folder from the decompiled APK directory to the base directory, and you should see this

assets/
lib/
manifest/
res/
resources.pb

Then create another folder called root in base directory and move the files from unknown folder inside the decompiled APK directory to that folder.

After that, copy kotlin folder from the decompiled APK directory to root folder we just created, and you should see this inside base directory

assets/
lib/
manifest/
res/
root/
kotlin/
...
resources.pb

Then, create another folder called dex in base directory, and after creating that folder, move all of theclasses.dex file to that folder we just created.
You can find all of the classes.dex file in /build/app/ directory inside decompiled APK directory

After all of those tiring folder structuring stuff, you should see these files and folders in base folder

assets/
dex/
lib/
manifest/
res/
root/
kotlin/
...
resources.pb

Step 6: Compressing the structured folder

Run the command below inside base folder to compress the structured folder

jar cMf out.zip ./manifest ./dex ./res ./root ./lib ./assets ./resources.pb

then you should get out.zip file

Step 7: Compiling to get AAB file

Compile the out.zip file that we just compressed with the command below

java -jar bundletool.jar build-bundle --modules=out.zip --output=out.aab

And that is it, we have successfully converted APK to AAB.
You can now upload your AAB file to Google Play Store.

source: https://github.com/sensei-z/APK2AAB