instant app入门与开发指南

instant app入门与开发指南概述 instantapp 是谷歌推出的类似于微信小程序 或者说小程序类似于 instantapp 的一项技术 用户无须安装应用 用完就走 安全无残留 其实也有残留 后面讲到 同时兼备 h5 的便捷和原生应用的优质体验 工作方式和应用场景工作方式 当用户点击链接时 通过 applink 去打开相应的 instantapp 如果之前没有打开过 则会从 playstore 去下载并打开

本文由玉刚说写作平台提供写作赞助,版权归玉刚说微信公众号所有
原作者: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的入口。

  • 模块间的关系图
    这里写图片描述
    这里写图片描述
    这里写图片描述
    • 模块间的关系总结
      1. 模块app 和instantapp 一般作为入口不负责具体的代码实现
      2. base模块和feature模块都可以做具体逻辑实现,base侧重公用部分的代码实现和公共资源的存放,feature则侧重于独立模块功能的实现
      3. base模块有且只有一个
      4. feature可以没有或有多个
      5. 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

这里写图片描述

关于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的规则了

[{ "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

(0)
上一篇 2026年3月19日 下午2:40
下一篇 2026年3月19日 下午2:41


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号