在發(fā)布應(yīng)用到 Google Play 之前有一個(gè)細(xì)節(jié)可能很多人會(huì)忽略邀桑,這就是 <uses-featrue>
的配置祟蚀。
什么是 <uses-featrue>
工窍?
所謂 <uses-featrue>
就是 Android 官方為發(fā)布到 Google Play 的程序建立的一套篩選機(jī)制。每一個(gè) <uses-featrue>
都聲明了對(duì)某種系統(tǒng)資源的硬性要求前酿,在編譯階段患雏,會(huì)自動(dòng)根據(jù)我們程序中調(diào)用到的資源來(lái)添加。 Google Play 會(huì)讀取 APK 文件信息中的 <uses-featrue>
罢维,當(dāng)用戶檢索應(yīng)用的時(shí)候淹仑,會(huì)自動(dòng)過(guò)濾掉那些他們可能無(wú)法適用的應(yīng)用程序。
比如肺孵,我們的應(yīng)用中某個(gè)地方使用到了藍(lán)牙操作匀借,編譯時(shí),Android 編譯系統(tǒng)會(huì)自動(dòng)在我們的 APK 文件信息中增加對(duì)藍(lán)牙的 <uses-featrue>
聲明平窘,大概長(zhǎng)這個(gè)樣子:
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />
如此吓肋,不具有藍(lán)牙模塊的設(shè)備在 Google Play 中檢索應(yīng)用時(shí),將無(wú)法看到我們的應(yīng)用瑰艘。
為什么我們需要去配置 <uses-featrue>
是鬼?
前文說(shuō)明了,默認(rèn)狀態(tài)下 <uses-featrue>
是自動(dòng)生成了磅叛,所以看起來(lái)我們不需要去手動(dòng)配置屑咳,但問(wèn)題是編譯系統(tǒng)其實(shí)沒(méi)有那么智能,可以直接篩選出必要的設(shè)備需求弊琴,現(xiàn)實(shí)情況是只要我們程序中有引用到某種資源(可能這種資源并不是必要的),最終的 APK 信息中就會(huì)包含這種資源的 <uses-featrue>
聲明杖爽。
當(dāng)然敲董,我們希望我們的應(yīng)用能有盡可能多的受眾紫皇,所以發(fā)布到 Google Play 之前進(jìn)行 <uses-featrue>
的配置,避免因?yàn)槭褂昧四撤N不必要的資源被 Google Play 過(guò)濾掉腋寨,就顯得有必要了聪铺。
如何去配置 <uses-featrue>
?
網(wǎng)上有很多介紹 <uses-featrue>
的文檔萄窜,這里就不做搬運(yùn)工了铃剔,本文只介紹如何查看 APK 文件的 <uses-featrue>
和配置方法。
步驟一:查看 APK 文件的 <uses-featrue>
>
環(huán)境需要 : Android SDK
我們需要使用 Android SDK 工具中的 aapt 工具查刻,其路徑一般是在:
你的 SDK 路徑\build-tools\對(duì)應(yīng)編譯工具版本目錄\
這個(gè)路徑下有很多編譯工具键兜,我們用的 aapt 工具就在這里。
找到后穗泵,切換到上述目錄下普气,運(yùn)行命令:
aapt dump badging demo.apk
將上面的 demo.apk
換成你的 APK 名字就可以了〉柩樱回車(chē)執(zhí)行命令后现诀,會(huì)輸入整個(gè) APK 的信息,如下是微信的 APK 信息:
package: name='com.tencent.mm'
versionCode='861'
versionName='6.3.25'
platformBuildVersionName='6.0-2704002'
install-location:'auto'sdkVersion:'15'
targetSdkVersion:'23'
uses-permission: name='com.tencent.mm.plugin.permission.READ'
uses-permission: name='com.tencent.mm.plugin.permission.WRITE'
uses-permission: name='com.tencent.mm.permission.MM_MESSAGE'
uses-permission: name='com.huawei.authentication.HW_ACCESS_AUTH_SERVICE'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.ACCESS_COARSE_LOCATION'
uses-permission: name='android.permission.ACCESS_FINE_LOCATION'
uses-permission: name='android.permission.CAMERA'
uses-permission: name='android.permission.GET_TASKS'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.MODIFY_AUDIO_SETTINGS'
uses-permission: name='android.permission.RECEIVE_BOOT_COMPLETED'
uses-permission: name='android.permission.RECORD_AUDIO'
uses-permission: name='android.permission.READ_CONTACTS'
uses-permission: name='android.permission.READ_SMS'
uses-permission: name='android.permission.VIBRATE'
uses-permission: name='android.permission.WAKE_LOCK'
uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.WRITE_CONTACTS'
uses-permission: name='android.permission.WRITE_SETTINGS'
uses-permission: name='com.android.launcher.permission.INSTALL_SHORTCUT'
uses-permission: name='com.android.launcher.permission.UNINSTALL_SHORTCUT'
uses-permission: name='com.android.launcher.permission.READ_SETTINGS'
uses-permission: name='com.tencent.mm.location.permission.SEND_VIEW'
uses-permission: name='android.permission.BLUETOOTH'
uses-permission: name='android.permission.BLUETOOTH_ADMIN'
uses-permission: name='android.permission.BROADCAST_STICKY'
uses-permission: name='android.permission.SYSTEM_ALERT_WINDOW'
uses-permission: name='android.permission.CHANGE_WIFI_STATE'
uses-permission: name='android.permission.GET_PACKAGE_SIZE'
uses-permission: name='android.permission.DOWNLOAD_WITHOUT_NOTIFICATION'
uses-permission: name='android.permission.NFC'
uses-permission: name='com.huawei.android.launcher.permission.CHANGE_BADGE'
uses-permission: name='android.permission.WRITE_APP_BADGE'
uses-permission: name='android.permission.ACCESS_WIFI_STATE'
uses-permission: name='android.permission.READ_PHONE_STATE'
uses-permission: name='com.tencent.mm.ext.permission.READ'
uses-permission: name='com.tencent.mm.ext.permission.WRITE'
uses-permission: name='com.android.vending.BILLING'
uses-permission: name='android.permission.READ_EXTERNAL_STORAGE'
uses-permission: name='com.android.alarm.permission.SET_ALARM'
uses-permission: name='com.tencent.mm.wear.message'
uses-permission: name='android.permission.BODY_SENSORS'
uses-permission: name='android.permission.USE_CREDENTIALS'
uses-permission: name='com.google.android.c2dm.permission.RECEIVE'
uses-permission: name='android.permission.GET_ACCOUNTS'
uses-permission: name='com.tencent.mm.permission.C2D_MESSAGE'
uses-permission: name='android.permission.USE_FINGERPRINT'
uses-permission: name='android.permission.MANAGE_ACCOUNTS'
uses-permission: name='android.permission.AUTHENTICATE_ACCOUNTS'
uses-permission: name='android.permission.READ_SYNC_SETTINGS'
uses-permission: name='android.permission.WRITE_SYNC_SETTINGS'
uses-permission: name='android.permission.READ_PROFILE
'application-label:'WeChat'
application-label-az-AZ:'WeChat'
application-label-bn-BD:'WeChat'
application-label-en:'WeChat'
application-label-en-AU:'WeChat'
application-label-en-GB:'WeChat'
application-label-en-IN:'WeChat'
application-label-es-US:'WeChat'
application-label-et-EE:'WeChat'
application-label-eu-ES:'WeChat'
application-label-fa:'WeChat'
application-label-fr-CA:'WeChat'
application-label-gl-ES:'WeChat'
application-label-gu-IN:'WeChat'
application-label-hy-AM:'WeChat'
application-label-is-IS:'WeChat'
application-label-ka-GE:'WeChat'
application-label-kk-KZ:'WeChat'
application-label-km-KH:'WeChat'
application-label-kn-IN:'WeChat'
application-label-ky-KG:'WeChat'
application-label-lo-LA:'WeChat'
application-label-mk-MK:'WeChat'
application-label-ml-IN:'WeChat'
application-label-mn-MN:'WeChat'
application-label-mr-IN:'WeChat'
application-label-ms-MY:'WeChat'
application-label-my-MM:'WeChat'
application-label-ne-NP:'WeChat'
application-label-pa-IN:'WeChat'
application-label-pt-BR:'WeChat'
application-label-pt-PT:'WeChat'
application-label-si-LK:'WeChat'
application-label-sq-AL:'WeChat'
application-label-sr:'WeChat'
application-label-sv:'WeChat'
application-label-sw:'WeChat'
application-label-ta-IN:'WeChat'
application-label-te-IN:'WeChat'
application-label-tl:'WeChat'
application-label-uk:'WeChat'
application-label-ur-PK:'WeChat'
application-label-uz-UZ:'WeChat'
application-label-zh-CN:'寰俊'
application-label-zh-TW:'WeChat'
application-label-zu:'WeChat'
application-icon-120:'r/j/icon.png'
application-icon-160:'r/j/icon.png'
application-icon-213:'r/y/icon.png'
application-icon-240:'r/y/icon.png'
application-icon-320:'r/a0/icon.png'
application-icon-480:'r/a2/icon.png'
application-icon-640:'r/a2/icon.png'
application: label='WeChat'
icon='r/j/icon.png'
uses-library-not-required:'com.google.android.maps'
uses-library-not-required:'com.sec.android.app.multiwindow'
launchable-activity: name='com.tencent.mm.ui.LauncherUI'
label='WeChat'
icon=''
uses-library-not-required:'com.here.android'
feature-group:
label=''
uses-feature-not-required: name='android.hardware.bluetooth'
uses-feature-not-required: name='android.hardware.bluetooth_le'
uses-feature-not-required: name='android.hardware.camera'
uses-feature-not-required: name='android.hardware.camera.autofocus'
uses-feature-not-required: name='android.hardware.location'
uses-feature-not-required: name='android.hardware.location.gps'
uses-feature-not-required: name='android.hardware.location.network'
uses-feature-not-required: name='android.hardware.microphone'
uses-feature-not-required: name='android.hardware.telephony'
uses-feature-not-required: name='android.hardware.touchscreen'
uses-feature-not-required: name='android.hardware.wifi'
uses-feature: name='android.hardware.screen.portrait'
uses-implied-feature: name='android.hardware.screen.portrait'
reason='one or more activities have specified a portrait orientation'
mainother-activitiesother-receiversother-services
supports-screens: 'small' 'normal' 'large' 'xlarge'
supports-any-density: 'true'locales: '--_--' 'az-AZ' 'bn-BD' 'en' 'en-AU' 'en-GB' 'en-IN' 'es-US' 'et-EE' 'eu-ES' 'fa' 'fr-CA' 'gl-ES' 'gu-IN' 'hy-AM' 'is-IS' 'ka-GE' 'kk-KZ' 'km-KH' 'kn-IN' 'ky-KG' 'lo-LA' 'mk-MK' 'ml-IN' 'mn-MN' 'mr-IN' 'ms-MY' 'my-MM' 'ne-NP' 'pa-IN' 'pt-BR' 'pt-PT' 'si-LK' 'sq-AL' 'sr' 'sv' 'sw' 'ta-IN' 'te-IN' 'tl' 'uk' 'ur-PK' 'uz-UZ' 'zh-CN' 'zh-TW' 'zu'
densities: '120' '160' '213' '240' '320' '480' '640'
native-code: 'armeabi' 'lib/'
可以看到履肃,下面的以 uses-feature
打頭的信息就是了仔沿,發(fā)現(xiàn)沒(méi)有,微信使用了很多uses-feature-not-required
的聲明尺棋,這就是我們接下來(lái)需要做的事情了于未。
步驟二:聲明不必要的資源
如上面微信的那樣,我們聲明那些不必要的資源陡鹃,告訴 Google Play 在不具備這些資源的設(shè)備上顯示我們烘浦。
經(jīng)過(guò)步驟一,我們已經(jīng)可以拿到自己應(yīng)用的 APK 信息了萍鲸,跟聲明應(yīng)用權(quán)限很類(lèi)似闷叉,我們只需要在 Androidmainfest.xml 文件中聲明對(duì)應(yīng)不需要的信息為 false 就可以了,比如聲明上面的藍(lán)牙信息為不必要:
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
這樣再使用步驟一的命令查看應(yīng)用信息脊阴,就會(huì)看到uses-feature-not-required: name='android.hardware.bluetooth'
了握侧,如此沒(méi)有藍(lán)牙設(shè)備的用戶就會(huì)在 Google Play 中看到我們了。