什么是免安裝應用
Android 免安裝應用使原生 Android 應用能夠在啟動網址時運行,無需安裝應用。 當 Google Play 商店收到與 免安裝應用匹配的網址請求時,它將必需的代碼文件發送到發送該請求的 Android 設備。 然后,該設備將運行此應用。
注:Android 免安裝應用僅在運行 Android 5.0(API 級別 21)或更高版本的 Android 設備上正常工作。(但更多的是告訴我要在 api 23)

為什么要使用免安裝應用
好處
1.快速(了解與體驗)
2.輕(因為免安裝應用至少要有一個開放的功能,可能是核心app,用戶可以直接刪繁就簡觸碰核心功能)
3.熱點(gp會推薦一波)
壞處
1.活躍統計(看將來gp如何做)
2.流量
注:權限(還是需要的)
怎么加入免安裝應用
原理
免安裝應用中的每項功能應至少有一個可充當該功能入口點的 Activity 類。 入口點 Activity 可托管此功能的 UI,并定義整個用戶流。
要提供按需功能下載,您需要將應用分解成較小的模塊,并將它們打包成功能模塊。
在構建免安裝應用項目時,構建輸出是一個包含一個或多個功能 APK 的免安裝應用 APK。 每項功能 APK 都是從項目中的功能模塊構建的,可以由用戶按需下載,并且可以作為免安裝應用進行啟動。
每個免安裝應用都必須有且僅有一個基礎功能 APK。 如果您的免安裝應用只有一項功能,那么,您只需要基礎功能 APK;其他功能 APK 是可選的。如果您的免安裝應用具有多項功能,則基礎功能 APK 通常包含其他功能所依賴的共享資源和代碼文件。 像地圖免安裝應用一樣,基礎 APK 可能包含此地圖應用的基礎 styles.xml 文件或數據結構類,以用于位置和景點建模。 無論用戶請求哪項功能,均會下載基礎功能 APK。
除基礎功能 APK 外,您還可以有其他功能 APK。 其他功能 APK 可以包含與某項功能對應的應用片段。 功能 APK 包含針對該功能的入口點 Activity,以及該功能所需的任何獨特資源。
當用戶從免安裝應用請求一項功能時,他們將獲得兩個功能 APK:所請求的功能 APK 和基礎功能 APK。 如果同一個用戶從該免安裝應用請求另一項功能,他們可能只會收到該功能 APK,因為他們已下載基礎功能 APK。 當然,如果免安裝應用只有一項功能,因而只有一個基礎功能 APK,則用戶只會收到基礎功能 APK。

需要了解的方面
關于地址鏈接的應用要求
1.所有聲明的意圖過濾器必須同時支持HTTPS和HTTP協議
2.使用的APK簽名方案V2相同的證書
受限功能
1.前臺服務(而通過servic和content providers和broadcast receivers將無法啟動您的應用程序)
2.推送通知
3.免安裝應用可以暫時使用內部存儲專用的應用程序(gp的文件夾)
4.目前不支持在桌面創建圖標
5.更改設備的設置
6.但可能訪問廣告
7.持續的長聯接(Persist longer than the instant app)
8.在運行免安裝應用時,不可能通過代碼下載未認證的app...(Run unverified software, run arbitrary native code, or load code dynamically other than the code provided by the Instant Apps runtime.)
注:一種即時應用程序無法使用替代手段繞過上述限制。不要試圖使用由設備制造商提供定制的API或功能。谷歌將刪除違反這些限制的應用程序。
不支持的功能
1.長時間運行的后臺服務
2.broadcast receivers
3外部訪問的content providers
4.content providers
5.通知
硬件加速
Android的即時應用程序支持OpenGL ES 2.0和語境共享組除外。下面的擴展支持:
GL_OES_texture_external
EGL_ANDROID_image_native_buffer
EGL_ANDROID_recordable
EGL_KHR_fence_sync
代碼層面
base
1.權限
<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>