Add TWA Support Library
To add a library required to use TWA we need to modify two files. Both of them live in the same folder Gradle Scripts, both have the same name build.gradle, we can distinguish which one is which by looking at the description in the parenthesis.
- Add Jitpack configuration – build.gradle (Project: Codeyon) – this is the Project level build.gradle. This allows us to add a dependency in the second file.
- Add required dependency – this is the Module level build.gradle
- Enable Java 8 – TWA library uses Java 8 features we also need to enable Java 8. To do that we need to add compileOptions as below:
- Add manifestPlaceholders – variables that will be used in the next step when configuring our TWA
Add TWA Activity
When required dependency and variables are there, we can continue and set our TWA up. We do that by editing Android App Manifest.
When creating a project we selected Add No Activity option, because of that our manifest is empty and contains only the application tag.
First we replace existing package name with our application ID and label with value from our manifestPlaceholders
Then we add TWA activity by adding an activity tag inside the application tag.
Step 2: Verify relationship between the website and the app
TWA requires a connection between the Android application and our PWA. To do that we use Digital Asset Links.
The connection must be set on both ends, TWA (application) and PWA (website).
Establish application to website relationship
To establish that connection we need to modify our manifestPlaceholders where our variables live. We need to add an extra element called assetStatements that keeps the information about our PWA.
Now we need to add a new meta-data tag to our application tag. This will inform the Android Application that we want to establish the connection with the application specified in the manifestPlaceholders.
That’s it, we just established the application to website relationship, now let’s jump into the website to application direction.
Establish website to application relationship
To establish the connection in the opposite direction we need to create a *.json file that will be available in our app’s /.well-known/assetlinks.json path.
The file can be generated using generator build into Android Studio. We need three values to generate the file:
- hosting site domain – our PWA url (eg. https://yourdomain.com/)
- app package name – our TWA package name (eg. yourpack.name.here)
- app package fingerprint (SHA256) – unique SHA256 that is generated based on Google Play Store keystore
We already have first and second values. Below I will describe how we can get the last one using Android Studio.
First we need to generate signed APK:
- In the Android Studio go to: Build → Generate Signed Bundle or APK → APK
- Use existing keystore (if you already have one) or Create new…
- Fill the form (remember the credentials as those are the credentials that the application will be signed with, they confirm your ownership of the application)
This will create a keystore file that is required to generate the app package fingerprint (SHA256)
- Select type of bundle (release as we want a production bundle) and signature versions
This will generate our APK that will be used later to create a release in Google Play Store.
After creating our keystore we can use it to generate required app package fingerprint (SHA256). This file is extremely important as it works as a proof that you are the owner of the application.
If this file is lost, you will not be able to do any further updates to your application in the store.
Going back to Android Studio, we need to go to Tools → App Links Assistant, it will open a sidebar that shows steps required to create a relationship between application and website.
Go to Step 3 – Associate website, then we need to fill required data (Site domain and Application ID) and select the keystore file generated in the previous step.
After filling the form, press Generate Digital Asset Links file. This will generate our assetlinks.json file, the file should look similar to the code below.
This is the file we need to make available in our app’s /.well-known/assetlinks.json path. I will not describe how to make it available on that path as it is too project specific.
Comments are closed.