今天遇到一个错误就是在蒲公英发布新的版本,然后APP检测到有新的就提示更新,但是更新完弹不出安装,闪退
报错信息:
Process: com.kooun.dio.agent, PID: 25841
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.kooun.dio.agent.fileProvider
一开始以为是权限,但是权限给了呀,怎么琢磨都不会,还是会闪退,最终还是搞定了,哈哈
添加了下面这句话
```javascript
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:grantUriPermissions="true"
tools:replace="android:authorities"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="android:resource"
/>
</provider>
在project下的build.gradle文件中:
allprojects {
repositories {
jcenter()
maven { url "https://raw.githubusercontent.com/Pgyer/mvn_repo_pgyer/master" }
}
}
在module下的build.gradle文件中添加依赖
dependencies {
implementation files('libs/pgyer_sdk_3.0.10.jar')
}
清单文件:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:grantUriPermissions="true"
tools:replace="android:authorities"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="android:resource"
/>
</provider>
<provider
android:name="com.pgyersdk.PgyerProvider"
android:authorities="${applicationId}.com.pgyer.provider"
android:exported="false"/>
<meta-data
android:name="PGYER_APPID"
android:value="" />
更新方法:
//手动申请文件读取权限
public void verifyStoragePermissions() {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
} else {
checkUpdate();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
} else {
checkUpdate();
}
}
private void checkUpdate() {
new PgyUpdateManager.Builder()
.setForced(true) //设置是否强制提示更新,非自定义回调更新接口此方法有用
.setUserCanRetry(false) //失败后是否提示重新下载,非自定义下载 apk 回调此方法有用
.setDeleteHistroyApk(false) // 检查更新前是否删除本地历史 Apk, 默认为true
.setUpdateManagerListener(new UpdateManagerListener() {
@Override
public void onNoUpdateAvailable() {
//没有更新是回调此方法
}
@Override
public void onUpdateAvailable(final AppBean appBean) {
//有更新回调此方法
//调用以下方法,DownloadFileListener 才有效;
//如果完全使用自己的下载方法,不需要设置DownloadFileListener
UpdateDialog dialogUpdate = new UpdateDialog(AgentMainActivity.this, appBean);
dialogUpdate.setOnItemClickListener(new UpdateDialog.OnItemClickListener() {
@Override
public void onUpgrade() {
PgyUpdateManager.downLoadApk(appBean.getDownloadURL());
}
@Override
public void onRemind() {
}
});
dialogUpdate.show();
}
@Override
public void checkUpdateFailed(Exception e) {
//更新检测失败回调
//更新拒绝(应用被下架,过期,不在安装有效期,下载次数用尽)以及无网络情况会调用此接口
}
})
//注意 :
//下载方法调用 PgyUpdateManager.downLoadApk(appBean.getDownloadURL()); 此回调才有效
//此方法是方便用户自己实现下载进度和状态的 UI 提供的回调
//想要使用蒲公英的默认下载进度的UI则不设置此方法
.setDownloadFileListener(new DownloadFileListener() {
@Override
public void downloadFailed() {
//下载失败
}
@Override
public void downloadSuccessful(File file) {
// 使用蒲公英提供的安装方法提示用户 安装apk
PgyUpdateManager.installApk(file);
}
@Override
public void onProgressUpdate(Integer... integers) {
}})
.register();
}
public class UpdateDialog extends Dialog implements View.OnClickListener {
private boolean forceUpgrade = false;
private OnItemClickListener mOnItemClickListener;
private Context context;
private AppBean appBean;
@Override
public void onClick(View view) {
this.dismiss();
switch (view.getId()) {
case R.id.tv_must_upgrade:
case R.id.tv_upgrade:
if (mOnItemClickListener != null) {
mOnItemClickListener.onUpgrade();
}
break;
case R.id.tv_remind:
if (mOnItemClickListener != null) {
mOnItemClickListener.onRemind();
}
break;
}
}
public interface OnItemClickListener {
void onUpgrade();
void onRemind();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
public UpdateDialog(Context context, AppBean appBean) {
super(context);
this.context = context;
this.appBean = appBean;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_update);
initView();
}
private void initView() {
setCancelable(false);
getWindow().setBackgroundDrawable(new BitmapDrawable());//清除四角框边
TextView mTextViewVersion = findViewById(R.id.tv_version);
TextView mTextViewContent = findViewById(R.id.tv_content);
TextView mTextViewUpgrade = findViewById(R.id.tv_upgrade);
TextView mTextViewMustUpgrade = findViewById(R.id.tv_must_upgrade);
TextView mTextViewRemind = findViewById(R.id.tv_remind);
LinearLayout mLayoutChooseItem = findViewById(R.id.layout_choose_item);
if (forceUpgrade) {
mTextViewMustUpgrade.setVisibility(View.VISIBLE);
mLayoutChooseItem.setVisibility(View.GONE);
} else {
mTextViewMustUpgrade.setVisibility(View.GONE);
mLayoutChooseItem.setVisibility(View.VISIBLE);
}
mTextViewVersion.setText("最新版本:V" + appBean.getVersionName());
mTextViewContent.setText(appBean.getReleaseNote());
mTextViewUpgrade.setOnClickListener(this);
mTextViewMustUpgrade.setOnClickListener(this);
mTextViewRemind.setOnClickListener(this);
}
public void setUpgrade(boolean forceUpgrade) {
this.forceUpgrade = forceUpgrade;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:orientation="vertical"
android:paddingBottom="32dp"
android:paddingTop="29dp">
<ImageView
android:layout_width="153dp"
android:layout_height="124dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ico_update" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dp"
android:layout_marginTop="20dp"
android:text="升级提醒"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="10dp"
android:textColor="@color/black"
android:textSize="15sp"
tools:text="最新版本:" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="5dp"
android:textColor="@color/black"
android:textSize="15sp"
tools:text="更新时间:2016/12/8"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="5dp"
android:text="更新内容:"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:textColor="@color/black"
android:textSize="15sp"
tools:text="" />
<TextView
android:id="@+id/tv_must_upgrade"
android:layout_width="110dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/dialog_background"
android:gravity="center"
android:text="立即更新"
android:textColor="@color/white"
android:textSize="15sp"
android:visibility="gone" />
<LinearLayout
android:id="@+id/layout_choose_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/tv_remind"
android:layout_width="110dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:background="@drawable/shape_gray_round_rectangle"
android:gravity="center"
android:text="暂不更新"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_upgrade"
android:layout_width="110dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:background="@drawable/shape_theme_rounded_rectangle"
android:gravity="center"
android:text="立即更新"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>