Skip to content

Instantly share code, notes, and snippets.

@ppoffice
Last active April 2, 2024 08:58
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ppoffice/9ce9790708eeabbec1281467e25139e4 to your computer and use it in GitHub Desktop.
Save ppoffice/9ce9790708eeabbec1281467e25139e4 to your computer and use it in GitHub Desktop.
A step by step guide to upgrade webview on an old Android Emulator

Tools

  1. emulator (30.3.5) (via Android SDK)
  2. adb (30.0.5) (via Android SDK)
  3. apktool (2.5.0) and Java runtime
  4. zip (3.0)
  5. unzip (6.0)

Make sure you have JAVA_HOME and ANDROID_HOME environment variables correctly set.

Steps

  1. Create a new Android Virtual Device (AVD) using Android Studio or avdmanager. Here, I created an Android 5.0 device with the following system image:

    Path                                        | Version | Description                                | Location
    system-images;android-21;google_apis;x86_64 | 32      | Google APIs Intel x86 Atom_64 System Image | system-images/android-21/google_apis/x86_64/

    The AVD name is

    $ emulator -list-avds
    Pixel_2_API_21
  2. Boot the AVD and make its system partition writable:

    $ $ANDROID_HOME/tools/emulator @Pixel_2_API_21 -writable-system

    And the AVD starts, remount its system partition using adb:

    $ $ANDROID_HOME/platform-tools/adb remount
  3. Download the Android System Webview of your desired version from the internet. For example, you may use Android System WebView 88.0.4324.93 (x86 + x86_64) (Android 5.0+) from apkmirror.com. The package ID is com.google.android.webview, which is different from the default system webview (com.android.webview). Also, make sure your package has the same ABI as your AVD.

  4. Extract the webview package using unzip and push it and the native libraries to the system partition of your AVD:

    $ unzip <path/to/your/webview.apk>
    $ $ANDROID_HOME/platform-tools/adb shell mkdir /system/app/ChromiumWebview
    $ $ANDROID_HOME/platform-tools/adb push <path/to/your/webview.apk> /system/app/ChromiumWebview/webview.apk
    $ $ANDROID_HOME/platform-tools/adb push <path/to/your/extracted/webview.apk>/lib /system/app/ChromiumWebview/

    After this step is done, you should have the following new files in your AVD:

    $ANDROID_HOME/platform-tools/adb shell ls -lR /system/app/ChromiumWebview/lib                                                            ✔ 
    
    /system/app/ChromiumWebview/lib:
    drwxr-xr-x root     root              2021-01-27 00:36 x86
    drwxr-xr-x root     root              2021-01-27 00:36 x86_64
    
    /system/app/ChromiumWebview/lib/x86:
    -rw-r--r-- root     root        43360 1979-12-31 00:00 libchromium_android_linker.so
    -rw-r--r-- root     root         3680 1979-12-31 00:00 libcrashpad_handler_trampoline.so
    -rw-r--r-- root     root     78167008 1979-12-31 00:00 libwebviewchromium.so
    
    /system/app/ChromiumWebview/lib/x86_64:
    -rw-r--r-- root     root        44896 1979-12-31 00:00 libchromium_android_linker.so
    -rw-r--r-- root     root         5104 1979-12-31 00:00 libcrashpad_handler_trampoline.so
    -rw-r--r-- root     root     78545528 1979-12-31 00:00 libwebviewchromium.so
  5. Restart Android services to let the Package Manager pick up our new webview package:

    $ $ANDROID_HOME/platform-tools/adb shell stop
    $ $ANDROID_HOME/platform-tools/adb shell start
    $ $ANDROID_HOME/platform-tools/adb shell pm list packages | grep webview
    package:com.android.webview           --> the built-in and outdated webview
    package:com.google.android.webview    --> our new webview
  6. Check if the package is registered with the Package Manager. Make sure codePath, nativeLibraryPath, and primaryCpuAbi are correct:

    $ $ANDROID_HOME/platform-tools/adb shell cat /data/system/packages.xml | grep com.google.android.webview
    <package name="com.google.android.webview" codePath="/system/app/ChromiumWebview" nativeLibraryPath="/system/app/ChromiumWebview/lib" primaryCpuAbi="x86_64" secondaryCpuAbi="x86" flags="...
  7. As the default webview package name used by the Android OS is hard-coded in older Android OSes (https://android.stackexchange.com/a/139415), you need to modify the Android framework to use the new package name. First, pull framework-res.apk from your AVD and decompile it using apktool:

    $ $ANDROID_HOME/platform-tools/adb pull /system/framework/framework-res.apk
    $ apktool d framework-res.apk
  8. Update the <path/to/extracted/framework-res.apk>/res/values/strings.xml file to use your new system webview package name:

    --- res/values/strings.xml	2021-01-27 00:53:05.000000000 -0500
    +++ res/values/strings.xml	2021-01-27 00:54:38.000000000 -0500
    @@ -70,7 +70,7 @@
         <string name="config_appsAuthorizedForSharedAccounts">;com.android.settings;</string>
         <string name="config_defaultNetworkScorerPackageName" />
         <string name="config_persistentDataPackageName" />
    -    <string name="config_webViewPackageName">com.android.webview</string>
    +    <string name="config_webViewPackageName">com.google.android.webview</string>
         <item type="string" name="timepicker_circle_radius_multiplier">0.82</item>
         <item type="string" name="timepicker_circle_radius_multiplier_24HourMode">0.85</item>
         <item type="string" name="timepicker_selection_radius_multiplier">0.16</item>
  9. Repack framework-res.apk using apktool.

    $ apktool b <path/to/extracted/framework-res.apk>

    There might be a few errors you need to fix before the package can be repacked. For example, if you encountered the following error:

    Error: String types not allowed (at 'APKTOOL_DUMMY_34e' with value '').
    

    you should replace <drawable name="APKTOOL_DUMMY_..." /> with <item type="drawable" name="APKTOOL_DUMMY_..." /> in the <path/to/extracted/framework-res.apk>/res/values-nodpi-v4/drawables.xml file and retry.

  10. The repacked framework-res.apk is placed under <path/to/extracted/framework-res.apk>/dist. Extract the resources.arsc file from it and merge it to the original framework-res.apk:

    $ unzip <path/to/extracted/framework-res.apk>/dist/framework-res.apk resources.arsc -d .
    $ zip -0 <path/to/original/framework-res.apk> resources.arsc

    The original framework-res.apk you pulled from your AVD is now updated.

  11. Next, replace the framework-res.apk in your AVD with our updated (merged) framework-res.apk from last step and reboot device:

    $ $ANDROID_HOME/platform-tools/adb push <path/to/original/framework-res.apk> /system/framework/framework-res.apk
    $ $ANDROID_HOME/platform-tools/adb reboot
  12. Finally, check if the webview is switched to the new one:

    Screen Shot 2021-01-27 at 1 22 05 AM
@iMrDJAi
Copy link

iMrDJAi commented Mar 12, 2022

HelIo! I opened /res/values/strings.xml and I found no default value for config_webViewPackageName (<string name="config_webViewPackageName" />). Will this still work?

If the Webview package name is not stored there, then where is it supposed to be? (just wondering).

Edit:

Never mind, that was ApkEditor fault. I used Apktool and I was able to see it there.

Edit2:

It worked! thank you so much!

@BrixSat
Copy link

BrixSat commented Feb 14, 2023

Android 7 = > framework-res/res/xml/config_webview_packages.xml

@BrixSat
Copy link

BrixSat commented Feb 14, 2023

For android 7 I executed this and file was perfectly generated.

#!/bin/bash
set -x
apktool d framework-res.apk
sed -i 's/com.android.webview/com.google.android.webview/g' framework-res/res/xml/config_webview_packages.xml
# colocar em build o android manifest e a pasta META-INF
unzip framework-res.apk "META-INF/*" -d .
unzip framework-res.apk "AndroidManifest.xml" -d .
mkdir framework-res/build
cp -r META-INF framework-res/build
cp AndroidManifest.xml framework-res/build
apktool b framework-res
cp framework-res/dist/framework-res.apk moded-framework-res.apk

@wingerlion
Copy link

wingerlion commented Oct 23, 2023

Hi guys. After tested in Android 5 and checked it worked perfectly. I switched to android 7.1.1. In step 10, I merged resources.arsc file to the original framework-res.apk. I decompiled it again with apktool but the file framework-res/res/xml/config_webview_packages.xml wasn't modified :(

Maybe should I merge other files instead of merging resources.arsc? thx in advance.

tested with apktool 2.5 and 2.9

@wingerlion
Copy link

nvm guys. I fixed only doing something like this

`$ unzip <path/to/extracted/framework-res.apk>/dist/framework-res.apk res/xml/config_webview_packages.xml -d .

Now you only have to merge that file with the original one.

$ $ANDROID_HOME/platform-tools/adb push <path/to/original/framework-res.apk> /system/framework/framework-res.apk
$ $ANDROID_HOME/platform-tools/adb reboot

idk why merge resources.arsc file didn't work in android 7.1.1. I tried it but I got an amazing loop

@manlu07
Copy link

manlu07 commented Dec 5, 2023

hello guys, I am getting the error below, please someone help

C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools>apktool b "C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res" -o framework-res_modified.apk
I: Using Apktool 2.9.0
W: Could not find sources
I: Checking whether resources has changed...
I: Building resources...
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2164: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2165: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2166: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2167: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2168: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2169: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2170: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2171: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2172: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2173: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2174: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2175: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2176: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2177: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2178: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2179: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2180: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2181: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2182: error: invalid value for type 'attr'. Expected a reference.
W: C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res\values\attrs.xml:2183: error: invalid value for type 'attr'. Expected a reference.
brut.androlib.exceptions.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\lsibanda\AppData\Local\Temp\brut_util_Jar_129218820829660162353275046165676398454.tmp, compile, --dir, C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\res, --legacy, -o, C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools\framework-res\build\resources.zip]

C:\Users\lsibanda\AppData\Local\Android\Sdk\platform-tools>

this is how the mentioned lines look like :

<item type="attr" name="private_resource_pad50">padding</item>
<item type="attr" name="private_resource_pad49">padding</item>
<item type="attr" name="private_resource_pad48">padding</item>
<item type="attr" name="private_resource_pad47">padding</item>
<item type="attr" name="private_resource_pad46">padding</item>
<item type="attr" name="private_resource_pad45">padding</item>
<item type="attr" name="private_resource_pad44">padding</item>
<item type="attr" name="private_resource_pad43">padding</item>
<item type="attr" name="private_resource_pad42">padding</item>
<item type="attr" name="private_resource_pad41">padding</item>
<item type="attr" name="private_resource_pad40">padding</item>
<item type="attr" name="private_resource_pad39">padding</item>
<item type="attr" name="private_resource_pad38">padding</item>
<item type="attr" name="private_resource_pad37">padding</item>
<item type="attr" name="private_resource_pad36">padding</item>
<item type="attr" name="private_resource_pad35">padding</item>
<item type="attr" name="private_resource_pad34">padding</item>
<item type="attr" name="private_resource_pad33">padding</item>
<item type="attr" name="private_resource_pad32">padding</item>
<item type="attr" name="private_resource_pad31">padding</item>

@JonaNorman
Copy link

JonaNorman commented Feb 23, 2024

I find a way to upgrade WebView without install, may be useful. https://github.com/JonaNorman/WebViewUpgrade

@janstadt
Copy link

I find a way to upgrade WebView without install, may be useful. https://github.com/JonaNorman/WebViewUpgrade

How do you use this? Do you have to build the apk completely? Is there a how to?

@JonaNorman
Copy link

How do you use this? Do you have to build the apk completely? Is there a how to?

implementation 'io.github.jonanorman.android.webviewup:core:0.1.0'
implementation 'io.github.jonanorman.android.webviewup:download-source:0.1.0'
UpgradeDownloadSource  upgradeSource = new UpgradeDownloadSource(
            context,
            "https://raw.githubusercontent.com/JonaNorman/ShareFile/main/com.google.android.webview_122.0.6261.64_armeabi-v7a.zip",
            saveFile
            );
WebViewUpgrade.upgrade(upgradeSource);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment