# Full tutorial to patch APKs on Android using apk-mitm and APKLab. Support for bundle APKs.

> Source: <https://gist.github.com/rigwild/02729a128b878186ffe8b1982d31b4f4>
> Published: 2022-07-19 04:18:32+00:00

## Patch APK

Full tutorial to patch APKs on Android using [apk-mitm](https://github.com/shroudedcode/apk-mitm) and [APKLab](https://github.com/APKLab/APKLab). Support for bundle APKs.

### Pull APK

```sh
adb shell pm list packages
adb shell pm path <package_name>
```

If the app contains a single APK:

```sh
adb pull /data/app/~~SzA2evEyrPV2ucUpwPzYYQ==/some_path==/base.apk base.apk
```

If it's a bundle APK (multiple APK files):

- Install [Split APKs Installer (SAI)](https://play.google.com/store/apps/details?id=com.aefyr.sai)
- Backup the app to a `.apks` file
- Load the file to your computer
- Extract the `.apks` file (it's an archive!)
- Create a copy of the `.apks` file, we will need it later when rebuilding the app

### Patch APK

Open `base.apk` with [APKLab](https://github.com/APKLab/APKLab) with options:

- Only main classes
- Decompile Java

Inspect the Java code in the `java_src` directory, find the function you want to edit.

When found, go to its `.smali` file equivalent in the `smali` directory, do your edit and save.

Right-click on the `apktool.yml` file: `APKLab: Rebuild the APK`.

The APK file is generated at `dist/base.apk`.

If you are working on a single APK, you can install it to your phone:

```sh
adb install dist/base.apk
```

### Build bundle APK

- Open your copy of the `.apks` file as an archive
- Delete the `base.apk` file from the archive
- Add your `dist/base.apk` patch APK file to the archive

Using [apk-mitm](https://github.com/shroudedcode/apk-mitm)

```sh
apk-mitm my_app-edited.apks
```

```

  ╭ apk-mitm v1.2.1
  ├ apktool v2.6.1
  ╰ uber-apk-signer v1.2.1

  Using temporary directory:
  /tmp/apk-mitm-ab640ef99e9eacd6b96f033651301328

  ✔ Extracting APKs
  ✔ Patching base APK
  ✔ Signing APKs
  ✔ Compressing APKs

   Done!  Patched file: ./my_app-edited-patched.apks
```

### Quick SSL/HTTPS MITM patch

Use the `--certificate` parameter your proxy SSL certificate

```sh
apk-mitm --certificate charles_proxy_certificate.pem my_app-edited.apks
```

### Install bundle APK

- Load the `my_app-edited-patched.apks` file to your phone
- Install [Split APKs Installer (SAI)](https://play.google.com/store/apps/details?id=com.aefyr.sai)
- Install it

Enjoy! 💕

