15/12/2024

[Android] Recompile and sign APK

Each new Android version introduces changes that usually require app developers to put some effort into keeping their app compatible with the latest OS releases. As a result, sometimes, your favorite free apps have issues if the developer does not update it.

Depending on the required changes, the effort varies, however you can often adapt the app yourself by simply decompiling it, updating whatever needs to be updated, and repackage it with a new Android version as target. Here are the simple steps to achieve this.

Install the required tools

You have a couple choices, either installing the full Android studio IDE or just installing the command line tools you need. We will take a look at the command line tools here.

First, download and extract sdkmanager wherever you like.

Then, we need to install at least build-tools and platform-tools, you can verify the available versions with:

sdkmanager --list --channel=0

choose the version you want and install it with:

sdkmanager "platform-tools" "build-tools;VERSION"

You will also need a JDK or JRE to have access to keytool.


Obtain the APK

You will need to obtain the APK of the app you want to modify, there are many ways to achieve this, Google is your friend


Decompile the APK

We need to unpack and extract all the files from the APK in order to be able to modify it:

apktool d YOUR_APK.apk -o FOLDER_WHERE_TO_UNPACK


Make the required changes

Inside FOLDER_WHERE_TO_UNPACK you will have the content of the APK, usually it is enough to modify the apktool.yml file and bump the targetSdkVersion (you can find here the API level for each Android version, you will need to set the API version number there, NOT the Android release)

Another place you might need to touch is the AndroidManifest.xml where services and permissions are configured


Repackage the app

apktool B FOLDER_WHERE_TO_UNPACK

Will generate inside FOLDER_WHERE_TO_UNPACK a new folder dist, where the compiled APK will be placed


Optimize the app build

You can read more about zipalign here, long story short, let it optimize your build:

zipalign -v 4 RECOMPILED_APK.apk ALIGNED_APK.apk


(Optional) Generate a signing key

All app packages need to be signed (even self-signed), you can skip this step if you already have a valid keystore.

You can use keytool from Java to achieve this, we have covered it already in a past blog post, you can reference that, check the "generate a new key and place it in a keystore" step.


Sign the APK

We will use apksigner tool for this step:

apksigner  sign --ks YOUR_KEYSTORE --v1-signing-enabled true --v2-signing-enabled true ALIGNED_APK.apk

Finally you can install the APK on your phone, you can either copy it somwhere on the phone, then open it and install from there (you might need to confirm you want to proceed after all safety warnings), or you can install from command line after connecting the phone to a computer.


(Optional) Install APK from command line

You need to enable developer options on your phone (the guide is a bit off). In general, find the phone info section in your settings and tap on the build number until you get the notification you are a developer. Then find the developer options menu and enable USB debugging.

When you connect the phone via USB, you should get a notification on the phone to trust the connected computer, allow it, and finall using adb tool you can install the app:

adb install ALIGNED_APK.apk


Hopefully that is enough to give new life to your favourite app, if not, you still learned something or can use this as a basis for further changes to keep what you like running for longer.

No comments:

Post a Comment

With great power comes great responsibility