Benefits of using TWA
- Common code base for multiple platforms - reduces the cost of application development.
- Easy and quick updates - the application UI and data are fully downloaded from the web server, there is no need to go through the process of publishing a new version on Google Play each time. Every user gets the update immediately.
- Low crash rate - the risk of application failure is low as all content is downloaded from external source.
- Small application size - the average size is about 200 kB.
How to use TWA?
1. Ensure compliance with PWA
To be able to "wrap" a website in TWA technology it must first comply with PWA standard. The Lighthouse tool from Google and my previous article explaining what is PWA and how to adapt a website to PWA requirements can be helpful in this.
By following these guidelines, the website should at least:
- have a manifest.json file with basic information about the application,
- contain a service worker responsible for offline mode (e.g. displaying a page with information about the need to go online),
- encrypt all traffic via HTTPS protocol,
- have a good responsiveness to look good on small screens.
2. Generate APK/AAB files for publication on Google Play
The Bubblewrap tool developed by Google is very suitable for this. Prepare the environment as instructed in the README.md file, and then install Bubblewrap with the command:
npm i -g @bubblewrap/cli
Next, initiate a new Android project with a link to the manifest.json
file:
bubblewrap init --manifest https://example.com/manifest.json
The generator will ask you a few questions necessary to generate the application, most of which are confirmation of the values contained in the manifest.json
file. The part concerning the application signing key is very relevant. You can generate a new one or use an existing one. It is important not to lose it, as this will result in the loss of ability to update the application in the future and the need to create a new one (without previously gained ratings and comments).
After answering the questions, the Android project file structure will be generated in exactly the same way as using Android Studio.
Then you just need to build the application with the command:
bubblewrap build
This will generate AAB and APK files ready for publishing on Google Play.
3. Publish assetlinks.json
The content displayed by Trusted Web Activity, as the name says, is trusted. This means that the page should declare with a special JSON file that it is associated with a specific Android application. This is done with a file called assetlinks.json
placed in the .well-known
directory, which should be publicly available (https://example.com/.well-known/assetlinks.json
).
After Bubblewrap has build the application, the assetlinks.json
file is already generated in the project directory. If you are building the application in a different way, you can generate it using the Statement List Generator.
It is very important that the SHA-256 fingerprint contained in it should be the same as the one assigned to the Android application in Google Play Console (you can check it in the "Application signing" section). Otherwise the app will run with the address bar visible instead of in full screen mode!
Examples of TWA applications
- Google Maps Go - a lighter version of Google maps.
- Morele.net - a consumer electronics store.
- Smakowity.pl - my own application with recipes.