Android 9 中的自动更新应用在解析时抛出错误
AutoUpdate app in Android 9 throws error while parsing
我正在尝试从外部更新我的应用 Google Play,但出现错误,我查看了所有内容,但找不到解决方案。
我希望通过以下3种方法进行自动更新:
private final String FILE_NAME = "app-release.apk";
public void checkStoragePermission(){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
} else {
updateApp();}
}
private void updateApp() {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + FILE_NAME));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 123: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
updateApp();
} else {
checkStoragePermission();
}
return;
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.jose.updateinapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
和 .xml 用于 FileProvider
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/${applicationId}" />
<external-path
name="external_files"
path="." />
</paths>
我尝试安装的应用是相同的,但版本代码 2 和版本名称 2.0 而不是版本代码 1 和版本名称 2.0
执行时会在设备上引发错误:解析包时出错
运行 控制台没有任何结果。并且在 logcat 中出现以下错误:
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri
content://com.jose.updateinapp.provider/external_files/Android/data/com.jose.updateinapp/files/Download/app-release.apk
from pid=16603, uid=10035 requires the provider be exported, or
grantUriPermission()
Error staging apk from content URI
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri
content://com.jose.updateinapp.provider/external_files/Android/data/com.jose.updateinapp/files/Download/app-release.apk
from pid=16603, uid=10035 requires the provider be exported, or
grantUriPermission()
有什么解决办法吗?谢谢!
抱歉,我脑子里漏掉了什么。
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="" />
<external-files-path
name="Download"
path="" />
</paths>
uri = FileProvider.getUriForFile(context,
"packgeName.provider",
new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxx.apk"));
intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
试试吧。它在我的项目中有效..
private void updateApp() {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + FILE_NAME));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//setFlags change addFlags
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
我正在尝试从外部更新我的应用 Google Play,但出现错误,我查看了所有内容,但找不到解决方案。
我希望通过以下3种方法进行自动更新:
private final String FILE_NAME = "app-release.apk";
public void checkStoragePermission(){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
} else {
updateApp();}
}
private void updateApp() {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + FILE_NAME));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 123: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
updateApp();
} else {
checkStoragePermission();
}
return;
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.jose.updateinapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
和 .xml 用于 FileProvider
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/${applicationId}" />
<external-path
name="external_files"
path="." />
</paths>
我尝试安装的应用是相同的,但版本代码 2 和版本名称 2.0 而不是版本代码 1 和版本名称 2.0
执行时会在设备上引发错误:解析包时出错
运行 控制台没有任何结果。并且在 logcat 中出现以下错误:
E/DatabaseUtils: Writing exception to parcel java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri
content://com.jose.updateinapp.provider/external_files/Android/data/com.jose.updateinapp/files/Download/app-release.apk from pid=16603, uid=10035 requires the provider be exported, or grantUriPermission()
Error staging apk from content URI java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri
content://com.jose.updateinapp.provider/external_files/Android/data/com.jose.updateinapp/files/Download/app-release.apk from pid=16603, uid=10035 requires the provider be exported, or grantUriPermission()
有什么解决办法吗?谢谢!
抱歉,我脑子里漏掉了什么。
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="" />
<external-files-path
name="Download"
path="" />
</paths>
uri = FileProvider.getUriForFile(context,
"packgeName.provider",
new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxx.apk"));
intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
试试吧。它在我的项目中有效..
private void updateApp() {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + FILE_NAME));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//setFlags change addFlags
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}