Restore Split Apks Efficiently : pm Session Install

[Update – 2019-11-05]
Migrate is adding this script in the latest Android 10 compatible version launching today 🙂
https://t.me/migrateApp/27530
https://t.me/migrateApp/24706

Problem

I regularly flash custom ROMs on my android devices like any Android Enthusiast and that requires a good backup tool for my apps and app-data so that I can avoid downloading them again and again from Play Store/F-Droid and sometimes keep previous state of running to avoid setting up afresh.

Migrate Backup is a great tool which can backup everything in zip and can be flashed from recovery.

I was doing this one day and was stuck on restoration of PayTM app, which made me think that the restore process has either failed or somehow hung. But I gave it some time and it completed in around 15 minutes. This was ridiculously high as my other 50+ apps took less time altogether than that one app.

I asked this question on Telegram Group and many people had issues with Apps which use split apks, some with Facebook, others with PayTM. I also got to know that a similar Backup/Restore tool Swift Backup is also taking ridiculous 40 minutes for restoring PayTM, and there is very less possibility of fixing this

This baffled me… why would Play Store take a minute to install the app and similar installation by any other method would always take 15 times more?


Solution

After experimenting for some time with pm (package manager), I found that almost all backup/restore solutions were handling the split apks the traditional way, that is

pm install -p <package-name> <apk-path>

-p option is passed to feed base package name to install split apks on.

I decided to check what other options we have in pm and could see that we can create an install session with install-create and push data into that with install-write and finish installing the pushed apps with install-commit or discard the session with install-abandon.

I made two small scripts capturing date-time at every step to compare the time taken by the commands in both scripts, one with traditional install …

### install-normal.sh ###

echo "Cleaning"
pm uninstall net.one97.paytm

echo "[$(date)]\tInstalling base"
pm install net.one97.paytm/base.apk &>/dev/null

for split in net.one97.paytm/split_*.apk
do
    echo "[$(date)]\t$split"
    pm install -p net.one97.paytm $split &>/dev/null
done

echo "[$(date)]\tCompleted"

and the other with session install …

### install-session.sh ###

echo "Cleaning"
pm uninstall net.one97.paytm

echo "[$(date)]\tInstalling base"
pm install net.one97.paytm/base.apk &>/dev/null

echo "[$(date)]\tCreating Session"
session=$(pm install-create -p net.one97.paytm | cut -d'[' -f2 | cut -d']' -f1)

for split in net.one97.paytm/split_*.apk
do
    echo "[$(date)]\tadding $split"
    pm install-write $session $(basename $split) $split &>/dev/null
done

echo "[$(date)]\tInstalling Session"
pm install-commit $session

echo "[$(date)]\tCompleted"

For experiment, I installed fresh PayTM Beta on my phone and checked that it has 33 apks in its app directory including base.apk. I copied them as a backup in my working directory. (A few days ago I checked, older version had 44 apks).

beryllium:/sdcard/pm # mkdir net.one97.paytm
beryllium:/sdcard/pm # cp /data/app/net.one97.paytm-*/*.apk net.one97.paytm/
beryllium:/sdcard/pm # ls -1 net.one97.paytm | wc -l
33
beryllium:/sdcard/pm # ls net.one97.paytm/
base.apk                               split_passbook.apk
split_bus_dynamic.apk                  split_passbook.config.xxhdpi.apk
split_bus_dynamic.config.xxhdpi.apk    split_paytmGamepind.apk
split_config.armeabi.apk               split_paytmGamepind.config.xxhdpi.apk
split_config.xxhdpi.apk                split_paytm_bank.apk
split_cst.apk                          split_paytm_bank.config.xxhdpi.apk
split_cst.config.xxhdpi.apk            split_paytm_cashback.apk
split_feed.apk                         split_paytm_cashback.config.xxhdpi.apk
split_feed.config.xxhdpi.apk           split_paytm_creditcard.apk
split_flight_dynamic.apk               split_paytm_creditcard.config.xxhdpi.apk
split_flight_dynamic.config.xxhdpi.apk split_paytm_finance.apk
split_h5sdk_dynamic.apk                split_paytm_finance.config.xxhdpi.apk
split_h5sdk_dynamic.config.xxhdpi.apk  split_paytm_wifi.apk
split_mall.apk                         split_paytm_wifi.config.xxhdpi.apk
split_mall.config.xxhdpi.apk           split_train_dynamic.apk
split_movie.apk                        split_train_dynamic.config.xxhdpi.apk
split_movie.config.xxhdpi.apk

After this I ran the scripts one by one and got the following outputs –

beryllium:/sdcard/pm # sh install-normal.sh
Cleaning
Success
[Fri Jul 12 03:28:36 IST 2019]  Installing base
[Fri Jul 12 03:28:41 IST 2019]  net.one97.paytm/split_bus_dynamic.apk
[Fri Jul 12 03:28:43 IST 2019]  net.one97.paytm/split_bus_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:28:46 IST 2019]  net.one97.paytm/split_config.armeabi.apk
[Fri Jul 12 03:28:53 IST 2019]  net.one97.paytm/split_config.xxhdpi.apk
[Fri Jul 12 03:28:56 IST 2019]  net.one97.paytm/split_cst.apk
[Fri Jul 12 03:29:00 IST 2019]  net.one97.paytm/split_cst.config.xxhdpi.apk
[Fri Jul 12 03:29:05 IST 2019]  net.one97.paytm/split_feed.apk
[Fri Jul 12 03:29:12 IST 2019]  net.one97.paytm/split_feed.config.xxhdpi.apk
[Fri Jul 12 03:29:20 IST 2019]  net.one97.paytm/split_flight_dynamic.apk
[Fri Jul 12 03:29:29 IST 2019]  net.one97.paytm/split_flight_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:29:40 IST 2019]  net.one97.paytm/split_h5sdk_dynamic.apk
[Fri Jul 12 03:29:52 IST 2019]  net.one97.paytm/split_h5sdk_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:30:06 IST 2019]  net.one97.paytm/split_mall.apk
[Fri Jul 12 03:30:21 IST 2019]  net.one97.paytm/split_mall.config.xxhdpi.apk
[Fri Jul 12 03:30:38 IST 2019]  net.one97.paytm/split_movie.apk
[Fri Jul 12 03:30:57 IST 2019]  net.one97.paytm/split_movie.config.xxhdpi.apk
[Fri Jul 12 03:31:17 IST 2019]  net.one97.paytm/split_passbook.apk
[Fri Jul 12 03:31:40 IST 2019]  net.one97.paytm/split_passbook.config.xxhdpi.apk
[Fri Jul 12 03:32:03 IST 2019]  net.one97.paytm/split_paytmGamepind.apk
[Fri Jul 12 03:32:30 IST 2019]  net.one97.paytm/split_paytmGamepind.config.xxhdpi.apk
[Fri Jul 12 03:32:58 IST 2019]  net.one97.paytm/split_paytm_bank.apk
[Fri Jul 12 03:33:27 IST 2019]  net.one97.paytm/split_paytm_bank.config.xxhdpi.apk
[Fri Jul 12 03:33:58 IST 2019]  net.one97.paytm/split_paytm_cashback.apk
[Fri Jul 12 03:34:33 IST 2019]  net.one97.paytm/split_paytm_cashback.config.xxhdpi.apk
[Fri Jul 12 03:35:08 IST 2019]  net.one97.paytm/split_paytm_creditcard.apk
[Fri Jul 12 03:35:45 IST 2019]  net.one97.paytm/split_paytm_creditcard.config.xxhdpi.apk
[Fri Jul 12 03:36:25 IST 2019]  net.one97.paytm/split_paytm_finance.apk
[Fri Jul 12 03:37:06 IST 2019]  net.one97.paytm/split_paytm_finance.config.xxhdpi.apk
[Fri Jul 12 03:37:49 IST 2019]  net.one97.paytm/split_paytm_wifi.apk
[Fri Jul 12 03:38:34 IST 2019]  net.one97.paytm/split_paytm_wifi.config.xxhdpi.apk
[Fri Jul 12 03:39:21 IST 2019]  net.one97.paytm/split_train_dynamic.apk
[Fri Jul 12 03:40:09 IST 2019]  net.one97.paytm/split_train_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:41:00 IST 2019]  Completed

You can see the above output matches the performance of backup solutions taking around 13 minutes to complete installing the 33 apks.

Now, running the one with session install method..

beryllium:/sdcard/pm # sh install-session.sh
Cleaning
Success
[Fri Jul 12 03:26:32 IST 2019]  Installing base
[Fri Jul 12 03:26:37 IST 2019]  Creating Session
[Fri Jul 12 03:26:37 IST 2019]  adding net.one97.paytm/split_bus_dynamic.apk
[Fri Jul 12 03:26:37 IST 2019]  adding net.one97.paytm/split_bus_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_config.armeabi.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_config.xxhdpi.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_cst.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_cst.config.xxhdpi.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_feed.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_feed.config.xxhdpi.apk
[Fri Jul 12 03:26:38 IST 2019]  adding net.one97.paytm/split_flight_dynamic.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_flight_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_h5sdk_dynamic.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_h5sdk_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_mall.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_mall.config.xxhdpi.apk
[Fri Jul 12 03:26:39 IST 2019]  adding net.one97.paytm/split_movie.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_movie.config.xxhdpi.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_passbook.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_passbook.config.xxhdpi.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_paytmGamepind.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_paytmGamepind.config.xxhdpi.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_paytm_bank.apk
[Fri Jul 12 03:26:40 IST 2019]  adding net.one97.paytm/split_paytm_bank.config.xxhdpi.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_cashback.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_cashback.config.xxhdpi.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_creditcard.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_creditcard.config.xxhdpi.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_finance.apk
[Fri Jul 12 03:26:41 IST 2019]  adding net.one97.paytm/split_paytm_finance.config.xxhdpi.apk
[Fri Jul 12 03:26:42 IST 2019]  adding net.one97.paytm/split_paytm_wifi.apk
[Fri Jul 12 03:26:42 IST 2019]  adding net.one97.paytm/split_paytm_wifi.config.xxhdpi.apk
[Fri Jul 12 03:26:42 IST 2019]  adding net.one97.paytm/split_train_dynamic.apk
[Fri Jul 12 03:26:42 IST 2019]  adding net.one97.paytm/split_train_dynamic.config.xxhdpi.apk
[Fri Jul 12 03:26:42 IST 2019]  Installing Session
Success
[Fri Jul 12 03:27:11 IST 2019]  Completed

Wow, so fast! The session install method took less than 1 minute to complete. This is in line with what Play Store takes to install this app.

I hope this gets picked by people making everyone’s life easier by putting their valuable time and I could be a part of the same. Please let me know your views and your findings if you experiment with other apps having split apks. Thank you for reading 🙂

One thought on “Restore Split Apks Efficiently : pm Session Install

  1. Averoth Shigue

    Super helpful! But on Android 12(idk what the minimum version is) you need to use
    ——————————–
    $ENFORCE && setenforce 0
    #your code here
    $ENFORCE && setenforce 1
    ——————————–

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.