本文由玉刚说写作平台提供写作赞助,版权归玉刚说微信公众号所有
原作者:AndroFarmer
版权声明:未经玉刚说许可,不得以任何形式转载
概述
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个模块
至此一个模板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
- 模块间的关系总结
写一个例子
多个feature间的数据交互
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation project(':base') testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }producdesc:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':base') testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
<activity android:name=".ProductDesc" android:label="商品详情"> <intent-filter > <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="/productdesc" android:scheme="https" />
intent-filter>
activity>
那么根据我们的deep links 去写intent如下
Intent it=new Intent(); it.setAction(Intent.ACTION_VIEW); it.addCategory("android.intent.category.BROWSABLE"); it.addCategory("android.intent.category.DEFAULT"); it.setData(Uri.parse("https://androfarmer.com/productdesc")); it.putExtra("data",datas.get(position)); ActivityCompat.startActivity(ProductList.this,it,null);
it.setPackage(getPackageName());
adb shell am start -W -a android.intent.action.VIEW -d https://androfarmer.com/productlist

App Links
关于deep links 这里就不做详细介绍了,大家可以搜索下资料还是挺多的,简单点来说,app links也属于 deep links,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(数字资产链接验证)
[{ "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"] } }]
Tips & Suggestion
- 要使用模拟器测试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方案解决
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207001.html原文链接:https://javaforall.net
