android上instant app介紹 類似於微信小程序
instant app 是谷歌推出的類似於微信小程序(或者說小程序類似於instant app)的一項技術,用戶無須安裝應用,用完就走,同時兼備h5的便捷和原生應用的優質體驗。
當用戶點擊鏈接時,通過applink去打開相應的instant app,如果之前沒有打開過,則會從play store去下載並打開,整個過程一氣呵成,跟瀏覽器打開網頁,如果有緩存先讀緩存,沒有就去服務器loading一樣
應用場景:
通過直接點擊鏈接進入(從社交網絡或短信中點擊鏈接)
通過瀏覽器搜索,如搜索X電商的y商品,通過點擊瀏覽器的搜索結果可直接進入instant app
通過google play 可以先試用部分功能,覺得不錯再安裝完整功能
在游戲中方面的應用,跟上面類似,更偏相向於試玩
如何創建模板Demo
創建一個project
當走到選擇form和sdk版本時,勾選 “include android instant app support“
如果沒有安裝相應support,去sdktools下安裝
填寫apps link 相關的url 參數,這里作為創建演示用默認值就好
項目創建完成后會生成4個模塊
app 類型:com.android.application
base 類型:com.android.feature
feature 類型:com.android.feature
instantapp 類型:com.android.instantapp
2個入口
app
instantapp
至此一個模板instant app創建過程就完成了
傳統方式創建一個項目,會生成一個app的模塊,創建instant app 也會創建一個app模塊,但功能跟傳統的不太一樣,傳統的app模塊基本上是整個項目的核心,所有的資源和代碼實現都在這里,但instant app中app模塊,充當的是傳統app入口,具體代碼實現交給base 和feature模塊去完成同樣的instantapp模塊也是作為入口,它是作為instant app的入口。
模塊間的關系總結
模塊app 和instantapp 一般作為入口不負責具體的代碼實現
base模塊和feature模塊都可以做具體邏輯實現,base側重公用部分的代碼實現和公共資源的存放,feature則側重於獨立模塊功能的實現
base模塊有且只有一個
feature可以沒有或有多個
feature與base的gradle文件差異
feature可以通過 聲明 “ baseFeature true” 變成basefeature
app links也屬於 deep links,app links做了更嚴格的限制條件,以保證鏈接是安全可靠的
instant app是可以通過鏈接直接打開app,沒有彈窗,但是我們從外部鏈接打開的話不可能知道我們的app的包名,所以一旦我們的intent無法從系統中所有的app找到唯一值的化,系統就會彈出框讓我們選擇哪一個app打開,要做到從外部無彈窗打開就需要用到app links。
用adb 命令模擬從外部打開應用:
adb shell am start -W -a android.intent.action.VIEW -d https://androfarmer.com/productlist
-----------------------
app links要求:
scheme只能是http 或https
action必須android.intent.action.VIEW
category必須包含 android.intent.category.BROWSABLE 和 android.intent.category.DEFAULT
系統版本有最低6.0的要求
需要數字資產鏈接文件完成鏈接的驗證
看下我們例子中配置的app links
<activity android:name=".ProductList" android:label="商品列表">
<intent-filter
android:autoVerify="true" >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="androfarmer.com"
android:path="/productlist"
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:autoVerify=”true” 這個標明,它是自動驗證的, 把這個去掉就符合deep link的規則了
Link Verfication(數字資產鏈接驗證)
要完成鏈接的驗證我們有個需要有個站點,並且要在站點根目錄配置一個”.well-known” 文件夾,文件夾中需要配置一個名為”assetlinks.json“數字資產鏈接文件,
文件內容的格式如下:
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target" : { "namespace": "android_app", "package_name": "com.androfarmer.instant.app",
"sha256_cert_fingerprints": ["0E:2E:C0:8B:99:AA:F3:51:4C:EF:A5:14:A6:B9:0E:EA:85:FD:A6:F6:AB:A2:40:DB:27:C9:45:2E:8F:4E:97:D6"] }
}]
最終要保證在瀏覽器上測試 https://domain.name/.well-known/assetlinks.json 這個數字資產鏈接文件可以正常訪問
domain.name替換成你的站點域名,並且要與我們app中配置的app links域名一致
assetlinks.json文件通過 https://developers.google.com/digital-asset-links/tools/generator 官方站點去生成和驗證,一定要通過驗證才能使用。
也可以直接在上面的基礎上修改 package_name 和 sha256_cert_fingerprints的值,這兩個值 也就是我們app的application id和簽名文件的sha256值
經過這個步驟以后,我們再通過鏈接去打開我們的app就不會出現選擇彈窗直接打開我們的app了。
如果要體驗完整的instant app流程的話 還需要將app 和instant app的包都上傳到google的play store才可以。
要使用模擬器測試instant app,最好使用 Android 8.1以上系統,並且必須硬件架構選擇x86 不能是x86_64
App Links Assistant 可以幫助我們生成app links 工具在as菜單欄tools下找到
assetlinks.json 可配置一個站點關聯一個或多個app,或者一個app關聯多個站點,具體詳見官方鏈接https://developer.android.com/training/app-links/verify-site-associations
instant app 可以使用的權限:
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ACCESS_NETWORK_STATE
BILLING
CAMERA
INSTANT_APP_FOREGROUND_SERVICE (API level 26 or higher)
INTERNET
READ_PHONE_NUMBERS (API level 26 or higher)
RECORD_AUDIO
VIBRATE
對於已經發布應用市場的instant app 可以通過調用 showInstallPrompt() 去引導用戶安裝完整版的app
可以調用 isInstantApp()查看是否是instant app 這對於權限判斷比較重要,比如你的app和instant app共用feature的情況
instant app 不能脫離完整版的app 必須先上傳app 才能上傳instant app
instant app 單個feature的大小限制是4MB,但沒有總大小的顯示,所以如果項目體積比較大可以通過多feature方案解決
google trips應用市場頁面上 install 右邊有個 try now 這個就是instant app 的入口
對發布到google play上的app導流來說應該有幫助