什么是App Links?
谷歌的App Link让用户在点击一个普通web链接的时候可以打开指定app的指定页面,前提是这个app已经安装并且经过了验证,否则会显示一个打开选项的弹出框。自此,在安卓中打开一个链接的用户体验大大提高,用户可以在链接与app之间快速切换。
先看效果图:

总的来说,实现App Links其实非常简单,但是首先你需要保证满足一些列需求:
- 有一个注册了的域名
- 域名的SSL通道
- 具有上传JSON文件到域名的能力
- Android Studio 1.3 Preview及以上
- Gradle 版本 — com.android.tools.build:gradle:1.3.0-beta3及以上
- 设置 compileSdkVersion 为 23及以上
- buildToolsVersion — 23.0.0 rc2及以上
1.在app中激活App Links
<activity
android:name=".activity.ParseDeepLinkActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="${theme_splash}">
<intent-filter>
...
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="${scheme_str}" />
<data android:scheme="http" android:host="m.xxxx.com" android:pathPattern="/mall/.*" />
<data android:scheme="https" android:host="m.xxxx.com" android:pathPattern="/mall/.*" />
</intent-filter>
</activity>
这个配置告诉安卓去验证一个文件,这个文件地址是https://m.xxxx.com/.well-known/xxx.json (json文件名字自起).如果存在这个文件,同时验证成功,那么用户点击该域名之下的链接时,就可以直接到app,弹出框就可以避免。否则app就没有成为默认选项,弹出框就会呈现给用户。下一步,我们将学会如何构建这个文件。
2. 上传web-app关联文件(xxx.json)
基于安全的原因,这个文件必须通过SSL的GET请求获得。你可以代开一个文本编辑器,然后写入如下形式的JSON:
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "cn.xxx.xxx",
"sha256_cert_fingerprints": ["7E:85:4C:44:4D:52:14:9A:A7:1D:3A:98:92:94:01:09:16:FA:C8:7E:68:11:40:73:D4:13:7C:14:A6:41:09:DD"]
}
},
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "cn.xxx.xxx.mm",
"sha256_cert_fingerprints": ["7E:85:4C:44:4D:52:14:9A:A7:1D:3A:98:92:94:01:09:16:FA:C8:7E:68:11:40:73:D4:13:7C:14:A6:41:09:DD"]
}
}
]
可以在app下的gradle中找到app的package name。你还需要通过在终端中执行ava keytool 产生一个sha256指纹:
keytool -list -v -keystore /path/to/app/release-key.keystore
3. 在app中处理深度链接
public class ParseDeepLinkActivity extends Activity {
public static final String PRODUCTS_DEEP_LINK = "/products";
public static final String XMAS_DEEP_LINK = "/campaigns/xmas";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Extrapolates the deeplink data
Intent intent = getIntent();
Uri deeplink = intent.getData();
// Parse the deeplink and take the adequate action
if (deeplink != null) {
parseDeepLink(deeplink);
}
}
private void parseDeepLink(Uri deeplink) {
// The path of the deep link, e.g. '/products/123?coupon=save90'
String path = deeplink.getPath();
if (path.startsWith(PRODUCTS_DEEP_LINK)) {
// Handles a product deep link
Intent intent = new Intent(this, ProductActivity.class);
intent.putExtra("id", deeplink.getLastPathSegment()); // 123
intent.putExtra("coupon", deeplink.getQueryParameter("coupon")); // save90
startActivity(intent);
} else if (XMAS_DEEP_LINK.equals(path)) {
// Handles a special xmas deep link
startActivity(new Intent(this, XmasCampaign.class));
} else {
// Fall back to the main activity
startActivity(new Intent(context, MainActivity.class));
}
}
}
总结:
缺陷:
- app links只能工作在Android 6.0以上,但是不排除某些产商在自改的5.x系统中就加入了这一功能。
- 得维护一个专门的M站。
- 目前测试只在Chrome上可以完美运行,其他浏览器(UC、QQ浏览器等)都不支持。
个人心得:
配置manifest时原生系统上可以不用配置pathPattern,或者配置匹配所有,依然可以完美运行,如下:
<data android:scheme="http" android:host="m.xxxx.com" />
<data android:scheme="https" android:host="m.xxxx.com" android:pathPattern="/.*" />
但是国内的自改系统(华为、小米、魅族等)上会没有效果,自改系统必须有一文件夹匹配,配置如下:
<data android:scheme="http" android:host="m.xxxx.com" android:pathPattern="/mall/.*" />
<data android:scheme="http" android:host="m.xxxx.com" android:pathPattern="/xxx/.*" />
网友评论