一款Android App升級庫

Loren1994發表於2017-09-20

AppUpdate

你可以通過它來升級你的App

簡介

  • 小巧便捷 , 使用方便
  • 自帶強制/非強制性升級提示框 , 可替換彈框的顏色
  • HttpURLConnection下載 , 不引用額外的庫
  • 解決三方庫之間FileProvider衝突問題
  • 支援Android8.0

引用

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
複製程式碼
dependencies {
     compile 'com.github.Loren1994:AndroidUpdate:1.2.0'
}
複製程式碼

用法

AppUpdateUtils.bindDownloadService(this, new AppUpdateUtils.CheckUpdateListener() {
            @Override
            public void checkUpdate() {
                //模擬請求介面延時
                SystemClock.sleep(3000);
                //在這裡請求你的檢測更新介面
                //在介面成功回撥裡判斷是否更新,更新則彈Dialog
                UpdateDialog.showUpdateDialog(MainActivity.this,
                        "update your app", new UpdateDialog.OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                //下載方法
                                AppUpdateUtils.update(MainActivity.this,"URL");
                            }
                        });
            }
});
複製程式碼

強制/非強制性升級提示框

//強制
UpdateDialog.showUpdateForceDialog(MainActivity.this, 
      "update your app", new UpdateDialog.OnConfirmListener() {
        @Override
        public void onConfirm() {
              Toast.makeText(MainActivity.this, 
              "confirm", Toast.LENGTH_SHORT).show();
        }
});
//非強制
UpdateDialog.showUpdateDialog(MainActivity.this, "update your app", 
                               new UpdateDialog.OnConfirmListener() {
        @Override
        public void onConfirm() {
            Toast.makeText(MainActivity.this, 
                         "confirm",Toast.LENGTH_SHORT).show();
        }
});
複製程式碼

替換提示框顏色

<color name="download_indicator_color">XXXX</color>
複製程式碼

關於FileProvider衝突的問題

以下用TakePhoto 4.0.3測試衝突問題

TakePhoto庫的Provider
<provider
   android:name="android.support.v4.content.FileProvider"
   android:authorities="${applicationId}.fileprovider"
   android:grantUriPermissions="true"
   android:exported="false">
   <meta-data
     android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/file_paths" />
</provider>
複製程式碼
測試過程
條件 結果
app模組: tools:replace="android:authorities" android:name="android.support.v4.content.FileProvider" update模組: android:name="android.support.v4.content.FileProvider" ✘編譯不通過
app模組: tools:replace="android:authorities" android:name="android.support.v4.content.FileProvider" update模組: android:name=".LorenProvider" ✔編譯通過✘TakePhoto呼叫崩潰
app模組: android:name="android.support.v4.content.FileProvider" update模組: android:name=".LorenProvider" ✘編譯不通過
app模組: android:name="pers.loren.appupdate.ResolveConflictProvider" update模組: android:name=".LorenProvider" ✔編譯通過✔TakePhoto呼叫正常✔update庫正常
app模組: tools:replace="android:authorities" android:name="android.support.v4.content.FileProvider" update模組: tools:replace="android:authorities" android:name="android.support.v4.content.FileProvider" ✔編譯通過✘TakePhoto呼叫崩潰
app模組: android:name="pers.loren.appupdate.ResolveConflictProvider" update模組: android:name=".ResolveConflictProvider" ✘編譯不通過
衝突報錯
Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute provider#android.support.v4.content.FileProvider@authorities value=(pers.loren.test.app.fileprovider) from AndroidManifest.xml:26:13-68
  	is also present at [com.jph.takephoto:takephoto_library:4.0.3] AndroidManifest.xml:19:13-64 value=(pers.loren.test.fileprovider).
  	Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:24:9-32:20 to override.
複製程式碼
最終Manifest
<!--app模組-->
<provider
    android:name="pers.loren.appupdate.ResolveConflictProvider"
    android:authorities="${applicationId}.app.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
       <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
           android:resource="@xml/file_paths" />
</provider>

<!--update模組-->
<provider
    android:name=".LorenProvider"
    android:authorities="${applicationId}.appupdate.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
      <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
           android:resource="@xml/file_paths" />
</provider>
複製程式碼
最終解決方案

update庫裡採用自定義的fileprovider , app模組裡也需要寫provider標籤, update模組提供ResolveConflictProvider以便app模組使用 , app模組裡也可以自行建立fileprovider類

一點總結

  • 同一App的多個庫的provider的name都是android.support.v4.content.FileProvider時會引起衝突
  • 上述情況可以通過寫自定義FileProvider解決
  • 同一App不同庫的provider的name用同一個自定義FileProvider也會引起衝突
  • 不同App之間provider的name可以相同 , 但authorities不可以重複,否則後者App不能安裝
  • 三方庫的Manifest裡引入tools , app裡也引入了tools , 可能導致merge manifest fail

一點題外總結

設定applicationId時 :

"pers.loren.test" : ✔ 成功

"pers.loren.test.1" : ✘ 編譯通過 , install時提示解析安裝包失敗 (Android6.0)

"pers.loren.test1" : ✔ 成功

Tips

  • 如果只引用這一個帶有provider的庫 , 則app裡不需要寫provider
  • 如果庫之間有衝突 , 則參考 最終Manifest
  • AppUpdateUtils.bindDownloadService( ) : bind時service不為null會先自動解綁
  • 不要忘記在onDestory( )裡呼叫AppUpdateUtils.unbindDownloadService( )解綁Service

專案地址

GitHub:https://github.com/Loren1994/AndroidUpdate 歡迎star~ issue~

<(▰˘◡˘▰)>

相關文章