APK 更新機制
適用於Android 11
Google Play自 COVID-19開始,上傳更新APK的審核時間,調整到一個星期的工作天才能審核完畢上架。這對有即時需修復bug 的apk無法在短時間內更新,讓所有的公司直跳腳。凡事靠 Google ,必有所損。
所以本人自行開發更新apk機制,只要將新版的apk置於自架的網頁伺服器中,使用者執行app,自動上網查詢並立即更新。
如今 Android 10, 及11的改版,對SDCard又有極大的變化。真的是讓開發者直抓狂。所以本篇相當複雜,請多點耐心。本人也花了好幾天才完成此專案。
AndroidManifest.xml
權限 uses-permission
Android 10及以前,需要 EXTERNAL_STORAGE的 WRITE及READ權限,但Android 11又不用了,超煩人的。為了大小通吃,全都寫上去比較省事。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
新增<provider>
Android 7之前 : 使用絕對路徑(file://sdcard/Download/test.apk)
Android 7~Android 9 : 使用fileProvider,將絕對路徑轉成uri (content://…..)
Android 10及以後 : 使用MediaStore,將絕對路徑轉成uri
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:ignore="LockedOrientationActivity">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<activity
provider_paths.xml
在res/xml裏增 provider_paths.xml檔案,內容如下
