import android.support.v4.content.FileProvider; // FileProvider引入
import android.os.Build; // Build引入
...
@ReactMethod
public void install(String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data;
File file =new File(path);
if(!file.exists()) {
return;
}
// 判断版本大于等于7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// "com.应用包名.fileprovider"即是在清单文件中配置的authorities,
data = FileProvider.getUriForFile(_context, "com.应用包名.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
data = Uri.fromFile(file);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
_context.startActivity(intent);
}
...
在AndroidManifest.xml 的 application添加
<manifest xmlns:tools="http://schemas.android.com/tools">
...
<provider
tools:replace="android:authorities"
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
tools:replace="android:resource"
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
/>
</provider>
...
</manifest>
android/src/main/res/xml添加file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root_path" path="." />
<external-cache-path name="cache_download" path="download"/>
</paths>
注意:android 应用更新安装时出现《 文件包与具有同一名称的现有文件包冲突》可用上文${applicationId}解决
调试环境无效可在build.gradle文件新增debug配置
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
// 新增
debug {
signingConfig signingConfigs.release
}
}