通過 Link這個單詞我們可以看出這個是一種鏈接,使用此鏈接可以直接跳轉到 APP,常用于應用拉活,跨應用啟動,推送通知啟動等場景。
在AS 上其實已經有詳細的使用步驟解析了,這里給大家普及下
快速點擊 shift 兩次,輸入 APPLink 即可找到 AS 提供的集成教程。
在 AS 中已經有詳細的使用步驟了,總共分為 4 步
創(chuàng)建一個 URL
或者也可以點擊 “How it works” 按鈕
選擇通過 applink 啟動的入口 activity。
點擊完成后,AS 會自動在兩個地方進行修改,一個是 AndroidManifest
<activity android:name=".TestActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" android:host="geyan.getui.com" /> </intent-filter> </activity>
此處多了一個 data,看到這個 data 標簽,我們可以大膽的猜測,也許這個 applink 的是一個隱式啟動。
另外一個改動點是
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); // ATTENTION: This was auto-generated to handle app links. Intent appLinkIntent = getIntent(); String appLinkAction = appLinkIntent.getAction(); Uri appLinkData = appLinkIntent.getData(); }
applink 的值即為之前配置的 url 鏈接,此處是為了接收數(shù)據(jù)用的,不再多說了。
這一步最關鍵了,需要根據(jù) APP 的證書生成一個 json 文件, APP 安裝的時候會去聯(lián)網進行校驗。選擇你的線上證書,然后點擊生成會得到一個 assetlinks.json 的文件,需要把這個文件放到服務器指定的目錄下
基于安全原因,這個文件必須通過 SSL 的 GET 請求獲取,JSON 格式如下:
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.lenny.myapplication", "sha256_cert_fingerprints": ["E7:E8:47:2A:E1:BF:63:F7:A3:F8:D1:A5:E1:A3:4A:47:88:0F:B5:F3:EA:68:3F:5C:D8:BC:0B:BA:3E:C2:D2:61"] }}]
sha256_cert_fingerprints 這個參數(shù)可以通過 keytool 命令獲取,這里不再多說了。
最后把這個文件上傳到 你配置的地址/.well-know/statements/json,為了避免今后每個 app 鏈接請求都訪問網絡,安卓只會在 app 安裝的時候檢查這個文件。,如果你能在請求 https://yourdomain.com/.well-... 的時候看到這個文件(替換成自己的域名),那么說明服務端的配置是成功的。目前可以通過 http 獲得這個文件,但是在M最終版里則只能通過 HTTPS 驗證。確保你的 web 站點支持 HTTPS 請求。
若一個host需要配置多個app,assetlinks.json添加多個app的信息。
若一個 app 需要配置多個 host,每個 host 的 .well-known 下都要配置assetlinks.json
有沒有想過 url 的后綴是不是一定要寫成 /.well-know/statements/json 的?
后續(xù)講原理的時候會涉及到,這里先不細說。
最后我們本質僅是拿到一個 URL,大多數(shù)的情況下,我們會在 url 中拼接一些參數(shù),比如
https://yourdomain.com/products/123?coupon=save90
其中 ./products/123?coupon=save90 是我們之前在第二步填寫的 path。
那測試方法多種多樣,可以使用通知,也可以使用短信,或者使用 adb 直接模擬,我這邊圖省事就直接用 adb 模擬了
adb shell am start-W -a android.intent.action.VIEW-d "https://yourdomain.com/products/123?coupon=save90"[包名]
使用這個命令就會自動打開 APP。前提是 yourdomain.com 網站上存在了 web-app 關聯(lián)文件。
上述這些都簡單的啦,依葫蘆畫瓢就行,下面講些深層次的東西,不僅要知道會用,還得知道為什么可以這么用,不然和咸魚有啥區(qū)別。
上訴也說了,我們配置的域名是在 activity 的 data 標簽的,那是否是可以認為 applink 是一種隱式啟動,應用安裝的時候根據(jù) data 的內容到這個網頁下面去獲取 assetlinks.json 進行校驗,如果符合條件則把 這個 url 保存在本地,當點擊 webview 或者短信里面的 url的時候,系統(tǒng)會自動與本地庫中的域名相匹配, 如果匹配失敗則會被自動認為是 deeplink 的連接。確認過眼神對吧~~~
也就說在第一次安裝 APP 的時候是會去請求 data 標簽下面的域名的,并且去請求所獲得的域名,那 安裝->初次啟動 的體驗自然會想到是在源碼中 PackageManagerService 實現(xiàn)。
一個 APk 的安裝過程是極其復雜的,涉及到非常多的底層知識,這里不細說,直接找到校驗 APPLink 的入口 PackageManagerService 的 installPackageLI 方法。
PackageMmanagerService.class
private void installPackageLI(InstallArgs args, PackageInstalledInfo res) { final int installFlags = args.installFlags; <!--開始驗證applink--> startIntentFilterVerifications(args.user.getIdentifier(), replace, pkg); ... } private void startIntentFilterVerifications(int userId, boolean replacing, PackageParser.Package pkg) { ... mHandler.removeMessages(START_INTENT_FILTER_VERIFICATIONS); final Message msg = mHandler.obtainMessage(START_INTENT_FILTER_VERIFICATIONS); msg.obj = new IFVerificationParams(pkg, replacing, userId, verifierUid); mHandler.sendMessage(msg);}
可以看到這邊發(fā)送了一個 message 為 START_INTENT_FILTER_VERIFICATIONS 的 handler 消息,在 handle 的 run 方法里又會接著調用 verifyIntentFiltersIfNeeded。
private void verifyIntentFiltersIfNeeded(int userId, int verifierUid, boolean replacing, PackageParser.Package pkg) { ... <!--檢查是否有Activity設置了AppLink--> final boolean hasDomainURLs = hasDomainURLs(pkg); if (!hasDomainURLs) { if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "No domain URLs, so no need to verify any IntentFilter!"); return; } <!--是否autoverigy--> boolean needToVerify = false; for (PackageParser.Activity a : pkg.activities) { for (ActivityIntentInfo filter : a.intents) { <!--needsVerification是否設置autoverify --> if (filter.needsVerification() && needsNetworkVerificationLPr(filter)) { needToVerify = true; break; } } } <!--如果有搜集需要驗證的Activity信息及scheme信息--> if (needToVerify) { final int verificationId = mIntentFilterVerificationToken++; for (PackageParser.Activity a : pkg.activities) { for (ActivityIntentInfo filter : a.intents) { if (filter.handlesWebUris(true) && needsNetworkVerificationLPr(filter)) { if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Verification needed for IntentFilter:" + filter.toString()); mIntentFilterVerifier.addOneIntentFilterVerification( verifierUid, userId, verificationId, filter, packageName); count++; } } } } } <!--開始驗證--> if (count > 0) { mIntentFilterVerifier.startVerifications(userId); } }
對 APPLink 進行了檢查,搜集,驗證,主要是對 scheme 的校驗是否是 http/https,以及是否有 flag 為 Intent.ACTION_DEFAULT與Intent.ACTION_VIEW 的參數(shù),接著是開啟驗證
PMS#IntentVerifierProxy.class
public void startVerifications(int userId) { ... sendVerificationRequest(userId, verificationId, ivs); } mCurrentIntentFilterVerifications.clear(); } private void sendVerificationRequest(int userId, int verificationId, IntentFilterVerificationState ivs) { Intent verificationIntent = new Intent(Intent.ACTION_INTENT_FILTER_NEEDS_VERIFICATION); verificationIntent.putExtra( PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_ID, verificationId); verificationIntent.putExtra( PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_URI_SCHEME, getDefaultScheme()); verificationIntent.putExtra( PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_HOSTS, ivs.getHostsString()); verificationIntent.putExtra( PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_PACKAGE_NAME, ivs.getPackageName()); verificationIntent.setComponent(mIntentFilterVerifierComponent); verificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); UserHandle user = new UserHandle(userId); mContext.sendBroadcastAsUser(verificationIntent, user); }
目前 Android 的實現(xiàn)是通過發(fā)送一個廣播來進行驗證的,也就是說,這是個異步的過程,驗證是需要耗時的(網絡請求),發(fā)出去的廣播會被 IntentFilterVerificationReceiver 接收到。這個類又會再次 start DirectStatementService,在這個 service 里面又會去調用 DirectStatementRetriever 類。在此類的 retrieveStatementFromUrl 方法中才是真正請求網絡的地方
DirectStatementRetriever.class
@Override public Result retrieveStatements(AbstractAsset source) throws AssociationServiceException { if (source instanceof AndroidAppAsset) { return retrieveFromAndroid((AndroidAppAsset) source); } else if (source instanceof WebAsset) { return retrieveFromWeb((WebAsset) source); } else { throw new AssociationServiceException("Namespace is not supported."); } } private Result retrieveFromWeb(WebAsset asset) throws AssociationServiceException { return retrieveStatementFromUrl(computeAssociationJsonUrl(asset), MAX_INCLUDE_LEVEL, asset); } private String computeAssociationJsonUrl(WebAsset asset) { try { return new URL(asset.getScheme(), asset.getDomain(), asset.getPort(), WELL_KNOWN_STATEMENT_PATH) .toExternalForm(); } catch (MalformedURLException e) { throw new AssertionError("Invalid domain name in database."); } }private Result retrieveStatementFromUrl(String urlString, int maxIncludeLevel, AbstractAsset source) throws AssociationServiceException { List<Statement> statements = new ArrayList<Statement>(); if (maxIncludeLevel < 0) { return Result.create(statements, DO_NOT_CACHE_RESULT); } WebContent webContent; try { URL url = new URL(urlString); if (!source.followInsecureInclude() && !url.getProtocol().toLowerCase().equals("https")) { return Result.create(statements, DO_NOT_CACHE_RESULT); } <!--通過網絡請求獲取配置--> webContent = mUrlFetcher.getWebContentFromUrlWithRetry(url, HTTP_CONTENT_SIZE_LIMIT_IN_BYTES, HTTP_CONNECTION_TIMEOUT_MILLIS, HTTP_CONNECTION_BACKOFF_MILLIS, HTTP_CONNECTION_RETRY); } catch (IOException | InterruptedException e) { return Result.create(statements, DO_NOT_CACHE_RESULT); } try { ParsedStatement result = StatementParser .parseStatementList(webContent.getContent(), source); statements.addAll(result.getStatements()); <!--如果有一對多的情況,或者說設置了“代理”,則循環(huán)獲取配置--> for (String delegate : result.getDelegates()) { statements.addAll( retrieveStatementFromUrl(delegate, maxIncludeLevel - 1, source) .getStatements()); } <!--發(fā)送結果--> return Result.create(statements, webContent.getExpireTimeMillis()); } catch (JSONException | IOException e) { return Result.create(statements, DO_NOT_CACHE_RESULT); }}
到了這里差不多就全部講完了,本質就是通過 HTTPURLConnection 去發(fā)起來一個請求。之前還留了個問題,是不是一定要要 /.well-known/assetlinks.json,到這里是不是可以完全明白了,就是 WELL_KNOWN_STATEMENT_PATH 參數(shù)
private static final String WELL_KNOWN_STATEMENT_PATH = "/.well-known/assetlinks.json";
在配置好了app對App Links的支持之后,只有運行Android M的用戶才能正常工作。之前安卓版本的用戶無法直接點擊鏈接進入app,而是回到瀏覽器的web頁面。
對于小的開發(fā)者來說這個有點困難,因為他們沒有能力為app維護一個網站,但是它們仍然希望通過web鏈接獲得流量。
機型版本是否識別ink是否識別com小米MI6 Android 8.0 MIUI 9.5否是小米MI5 Android 7.0 MIUI 9.5否是魅族PRO 7 Android 7.0 Flyme 6.1.3.1A否是三星S8 Android 7.0是,彈框是華為HonorV10 Android 8.0 EMUI 8.0是是oppo R11s Android 7.1.1 ColorOS 3.2是是oppoA59s Android 5.1 ColorOS 3.0是,不能跳轉到app是,不能跳轉到appvivoX6Plus A Android 5.0.2 Funtouch OS_2.5否是vivo767 Android 6.0 Funtouch OS_2.6是,不能跳轉到app是,不能跳轉到appvivoX9 Android 7.1.1 Funtouch OS_3.1是,不能跳轉到app是,不能跳轉到app
最后對于程序員來說,要學習的知識內容、技術有太多太多,要想不被環(huán)境淘汰就只有不斷提升自己,從來都是我們去適應環(huán)境,而不是環(huán)境來適應我們!
這里附上上述的技術體系圖相關的幾十套騰訊、頭條、阿里、美團等公司19年的面試題,把技術點整理成了視頻和PDF(實際上比預期多花了不少精力),包含知識脈絡 + 諸多細節(jié),由于篇幅有限,這里以圖片的形式給大家展示一部分。
相信它會給大家?guī)砗芏嗍斋@:
【Android進階學習視頻】、【全套Android面試秘籍PDF】、【Android開發(fā)核心知識點筆記】可以 私信我【面試】免費獲?。?/strong>
當程序員容易,當一個優(yōu)秀的程序員是需要不斷學習的,從初級程序員到高級程序員,從初級架構師到資深架構師,或者走向管理,從技術經理到技術總監(jiān),每個階段都需要掌握不同的能力。早早確定自己的職業(yè)方向,才能在工作和能力提升中甩開同齡人。
意見反饋
×