对于Android版本的坑,已经无力吐槽了,由于高版本增加了权限,所以再用原始的安装apk文件的代码,只会碰坑。。。
简要概括:有可能有的小伙伴,按照新版本的规则写完代码,build的时候,会报一个错误:Error:Execution failed for task ':app:processDebugManifest'.> Manifest merger failed with multiple errors, see logs,这是由于与现有的jar包中的重名了。解决办法:
步骤1:
建一个类继承FileProvider :
public class SLFileProvider extends FileProvider {
}
然后在AndroidManifest.xml里声明:
<provider
android:name=".common.utils.SLFileProvider"
android:authorities="com.android.sl.restaurant.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
步骤2:
在res目录下新增file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="files_root"
path="" />
</paths>
</resources>
步骤3:
//安装apk文件
private void installApk(String filename) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
/* Android N 写法*/
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", new File(filename));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
/* Android N之前的老版本写法*/
intent.setDataAndType(Uri.fromFile(new File(filename)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
// File file = new File(filename);
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setAction(Intent.ACTION_VIEW); //浏览网页的Action(动作)
// String type = "application/vnd.android.package-archive"; //apk文件类型
// intent.setDataAndType(Uri.fromFile(file), type); //设置数据类型
// startActivity(intent);
}