什么是免安裝應(yīng)用
Android 免安裝應(yīng)用使原生 Android 應(yīng)用能夠在啟動網(wǎng)址時運(yùn)行缚陷,無需安裝應(yīng)用酒奶。 當(dāng) Google Play 商店收到與 免安裝應(yīng)用匹配的網(wǎng)址請求時,它將必需的代碼文件發(fā)送到發(fā)送該請求的 Android 設(shè)備免绿。 然后郭脂,該設(shè)備將運(yùn)行此應(yīng)用疾呻。
注:Android 免安裝應(yīng)用僅在運(yùn)行 Android 5.0(API 級別 21)或更高版本的 Android 設(shè)備上正常工作。(但更多的是告訴我要在 api 23)

為什么要使用免安裝應(yīng)用
好處
1.快速(了解與體驗(yàn))
2.輕(因?yàn)槊獍惭b應(yīng)用至少要有一個開放的功能,可能是核心app绊袋,用戶可以直接刪繁就簡觸碰核心功能)
3.熱點(diǎn)(gp會推薦一波)
壞處
1.活躍統(tǒng)計(看將來gp如何做)
2.流量
注:權(quán)限(還是需要的)
怎么加入免安裝應(yīng)用
原理
免安裝應(yīng)用中的每項(xiàng)功能應(yīng)至少有一個可充當(dāng)該功能入口點(diǎn)的 Activity 類烦秩。 入口點(diǎn) Activity 可托管此功能的 UI电湘,并定義整個用戶流。
要提供按需功能下載,您需要將應(yīng)用分解成較小的模塊晶府,并將它們打包成功能模塊。
在構(gòu)建免安裝應(yīng)用項(xiàng)目時矾飞,構(gòu)建輸出是一個包含一個或多個功能 APK 的免安裝應(yīng)用 APK枚冗。 每項(xiàng)功能 APK 都是從項(xiàng)目中的功能模塊構(gòu)建的,可以由用戶按需下載诞仓,并且可以作為免安裝應(yīng)用進(jìn)行啟動缤苫。
每個免安裝應(yīng)用都必須有且僅有一個基礎(chǔ)功能 APK。 如果您的免安裝應(yīng)用只有一項(xiàng)功能墅拭,那么活玲,您只需要基礎(chǔ)功能 APK;其他功能 APK 是可選的。如果您的免安裝應(yīng)用具有多項(xiàng)功能舒憾,則基礎(chǔ)功能 APK 通常包含其他功能所依賴的共享資源和代碼文件镀钓。 像地圖免安裝應(yīng)用一樣,基礎(chǔ) APK 可能包含此地圖應(yīng)用的基礎(chǔ) styles.xml 文件或數(shù)據(jù)結(jié)構(gòu)類镀迂,以用于位置和景點(diǎn)建模丁溅。 無論用戶請求哪項(xiàng)功能,均會下載基礎(chǔ)功能 APK探遵。
除基礎(chǔ)功能 APK 外窟赏,您還可以有其他功能 APK。 其他功能 APK 可以包含與某項(xiàng)功能對應(yīng)的應(yīng)用片段箱季。 功能 APK 包含針對該功能的入口點(diǎn) Activity涯穷,以及該功能所需的任何獨(dú)特資源。
當(dāng)用戶從免安裝應(yīng)用請求一項(xiàng)功能時藏雏,他們將獲得兩個功能 APK:所請求的功能 APK 和基礎(chǔ)功能 APK拷况。 如果同一個用戶從該免安裝應(yīng)用請求另一項(xiàng)功能,他們可能只會收到該功能 APK掘殴,因?yàn)樗麄円严螺d基礎(chǔ)功能 APK赚瘦。 當(dāng)然,如果免安裝應(yīng)用只有一項(xiàng)功能杯巨,因而只有一個基礎(chǔ)功能 APK蚤告,則用戶只會收到基礎(chǔ)功能 APK。

需要了解的方面
關(guān)于地址鏈接的應(yīng)用要求
1.所有聲明的意圖過濾器必須同時支持HTTPS和HTTP協(xié)議
2.使用的APK簽名方案V2相同的證書
受限功能
1.前臺服務(wù)(而通過servic和content providers和broadcast receivers將無法啟動您的應(yīng)用程序)
2.推送通知
3.免安裝應(yīng)用可以暫時使用內(nèi)部存儲專用的應(yīng)用程序(gp的文件夾)
4.目前不支持在桌面創(chuàng)建圖標(biāo)
5.更改設(shè)備的設(shè)置
6.但可能訪問廣告
7.持續(xù)的長聯(lián)接(Persist longer than the instant app)
8.在運(yùn)行免安裝應(yīng)用時服爷,不可能通過代碼下載未認(rèn)證的app...(Run unverified software, run arbitrary native code, or load code dynamically other than the code provided by the Instant Apps runtime.)
注:一種即時應(yīng)用程序無法使用替代手段繞過上述限制杜恰。不要試圖使用由設(shè)備制造商提供定制的API或功能。谷歌將刪除違反這些限制的應(yīng)用程序仍源。
不支持的功能
1.長時間運(yùn)行的后臺服務(wù)
2.broadcast receivers
3外部訪問的content providers
4.content providers
5.通知
硬件加速
Android的即時應(yīng)用程序支持OpenGL ES 2.0和語境共享組除外心褐。下面的擴(kuò)展支持:
GL_OES_texture_external
EGL_ANDROID_image_native_buffer
EGL_ANDROID_recordable
EGL_KHR_fence_sync
代碼層面
base
1.權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
2.素材
3.文案
4.其他支持庫
android {
...
baseFeature true
}
dependencies {
application project(":installed")
feature project(':features:bye')
feature project(':features:hello')
api "com.android.support:appcompat-v7:${rootProject.supportLib}"
}
hello
1.build.gradle
dependencies {
implementation project(':features:base')
api ('com.google.android.instantapps:instantapps:1.0.0') {
// This is to exclude the transitive dependencies for the support library otherwise
// the project doesn't compile with the error below.
//
// > Android dependency 'com.android.support:support-v4' has different version for the compile (23.3.0) and runtime (25.3.1) classpath. You should manually set the same version via DependencyResolution
exclude group: 'com.android.support'
}
}
2.AndroidManifest.xml
<activity
android:name="com.instantappsamples.feature.hello.HelloActivity"
android:label="@string/title_activity_hello"
android:theme="@style/AppTheme">
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter
android:autoVerify="true"
android:order="2">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="hello-feature.instantappsample.com"
android:pathPrefix="/hello"
android:scheme="https" />
<data
android:host="hello-feature.instantappsample.com"
android:pathPrefix="/hello"
android:scheme="http" />
</intent-filter>
<meta-data
android:name="default-url"
android:value="https://hello-feature.instantappsample.com/hello" />
</activity>
3.HelloActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://hello-feature.instantappsample.com/goodbye"));
intent.setPackage(getPackageName());
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
}
});
}
bye
1.build.gradle
dependencies {
implementation project(':features:base')
}
2.AndroidManifest.xml
<activity
android:name=".GoodbyeActivity"
android:label="@string/title_activity_goodbye"
android:theme="@style/AppTheme">
<intent-filter
android:autoVerify="true"
android:order="1">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="hello-feature.instantappsample.com"
android:pathPrefix="/goodbye"
android:scheme="https" />
<data
android:host="hello-feature.instantappsample.com"
android:pathPrefix="/goodbye"
android:scheme="http" />
</intent-filter>
</activity>