Android Browser Helper
repository·main·Indexed 21 days ago
https://github.com/googlechrome/android-browser-helperA library that simplifies the implementation of Custom Tabs and Trusted Web Activities (TWA) on Android. It provides components for launching browsers, managing site settings, implementing ephemeral browsing with fallbacks, adding custom headers via Digital Asset Links, and handling TWA features such as location and notification delegation, multi-domain support, offline-first experiences, and Google Play Billing integration.
What's inside Android Browser Helper
- The TWA Web Share Target Demo is an Android application designed to demonstrate how to launch a Trusted Web Activity (TWA) that implements the Web Share Target API. This allows the Android app to act as a destination when a user shares content from other Android applications to the TWA.
What is Android Browser Helper
mainAndroid Browser Helper is a library designed to help developers implement Custom Tabs and Trusted Web Activities (TWA) using the AndroidX browser support library. It provides pre-built implementations for common tasks, such as:
- Creating a Launcher Activity specifically for launching a Trusted Web Activity.
- Logic for selecting the most appropriate Custom Tabs provider on a device.
- Creating an Activity that launches the browser's site settings for a specific TWA.
Explore the Custom Tabs Navigation Callbacks Demo
mainThe Custom Tabs Navigation Callbacks Demo demonstrates how to launch a Chrome Custom Tab and intercept navigation events occurring within that tab.
Key features of this demo include:
- Launching a Custom Tab via the Android Browser Helper library.
- Receiving and processing navigation callbacks.
- Reporting navigation events to the Android log and displaying them on the demo's launch screen.
Note on Privacy: The navigation information reported by this demo does not include actual URLs; it is designed to facilitate the collection of performance statistics without compromising user privacy.
Understand the TWA WebView Fallback demo
mainThe TWA WebView Fallback demo provides an alternative to the default fallback behavior when a Trusted Web Activity (TWA) fails to start or when a TWA-compatible browser is unavailable.
Instead of falling back to a Custom Tab or the default browser (which breaks the full-screen experience), this demo opens the content in a full-screen
WebView. It is designed to handle TWA-specific requirements, including:- Navigation: Correctly handling users leaving the verified origin and returning via the back button or links.
- Customization: Applying TWA-specific visual customizations, such as status bar and toolbar colors, to the fallback
WebViewexperience.
Warning: This demo is experimental and under development. It does not yet cover all user journeys required for a production-ready WebView fallback.
Explore the TWA Basic demo
mainThe
twa-basicdemo is a minimal Android application designed to demonstrate how to launch a Trusted Web Activity (TWA) using only existing components fromandroidx.browserandandroid-browser-helper.Key characteristics:
- No custom Java/Kotlin code: It relies entirely on configuring and wiring together existing library components.
- Purpose: Primarily intended for developers of
android-browser-helperto test changes in a real-world app context. - Functionality: Launches
https://airhorner.comas a TWA. - Similarity: It is architecturally similar to projects generated by svgomg-twa.
Understand Trusted Web Activity (TWA) Notification Delegation
mainThe Notification Delegation demo demonstrates how to override the standard web-based notification prompt using native Android code. This allows developers to leverage native Android notification features that are not available through standard web APIs, such as using custom notification sounds.
The core logic for implementing this behavior is located in the
NotificationDelegationServiceclass.Use Play Billing from a website
mainTo access Google Play Billing capabilities (like querying purchase history, initializing payments, or querying product details) from your website within a TWA, use the
window.getDigitalGoodsServiceAPI.You must provide the payment method URL
https://play.google.com/billingand an array of product item IDs.const PAYMENT_METHOD = 'https://play.google.com/billing'; const ITEM_IDS = [ 'android.test.purchased', 'android.test.canceled', ] const service = await window.getDigitalGoodsService(PAYMENT_METHOD); const details = await service.getDetails(ITEM_IDS); console.log(details);Add custom headers to Custom Tabs requests
mainYou can add custom headers to a web request when opening a URL with Custom Tabs.
Security Requirement: Adding custom headers is restricted. It only works if the developer owns both the Android application using Custom Tabs and the web origin being opened. Ownership must be proven via Digital Asset Links.
Add Android Browser Helper to an Android project
mainTo use the library, add it as a dependency in your application's
build.gradlefile. The library is hosted on Google Maven.dependencies { //... implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.7.2' }Implement Custom Tabs with Ephemeral Browsing and Fallback
mainThis demo demonstrates how to launch a Custom Tab using ephemeral browsing mode. Ephemeral browsing ensures that the session is private and does not persist cookies, cache, or history.
Because ephemeral browsing support varies by device, the implementation includes a fallback mechanism: if ephemeral Custom Tabs are unavailable, the app falls back to opening a web view that manually clears cookies, cache, history, and stored file data to achieve a similar privacy effect.
Launch a Custom Tab with ephemeral browsing enabled
mainThecustom-tabs-ephemeraldemo demonstrates how to use the Android Browser Helper library to launch a Custom Tab in ephemeral mode. Ephemeral browsing ensures that the session is private and does not leave a history or cookies behind in the user's main browser.Use the Trusted Web Activity Screen Orientation Demo
mainThetwa-orientationdemo demonstrates how to control screen orientation for a Trusted Web Activity (TWA) using theweb_manifestJSON file. This allows developers to specify how the application should behave when the device is rotated.