java - 权限拒绝 : opening provider that is not exported from UID 1000

标签 java android android-fileprovider securityexception android-securityexception

我已经使用共享用户 ID android.uid.system 构建了一个签名的应用程序(系统应用程序)。它包含一个 FileProvider,我需要它来实现包安装 Intent

当我尝试使用包安装 Intent 安装应用程序时,会发生以下错误。

2020-01-16 23:44:48.506 5305-16771/com.google.android.packageinstaller W/InstallStaging: Error staging apk from content URI
    java.lang.SecurityException: Permission Denial: opening provider com.example.example.CustomFileProvider from ProcessRecord{5bd7399 5305:com.google.android.packageinstaller/u0a13} (pid=5305, uid=10013) that is not exported from UID 1000
        at android.os.Parcel.readException(Parcel.java:2004)
        at android.os.Parcel.readException(Parcel.java:1950)
        at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4758)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:5836)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2526)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1394)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:967)
        at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:167)
        at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:161)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

但是,如果我删除共享用户 ID(将其转换为用户应用程序而不是系统),则软件包安装可以正常工作。

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.example"
    android:sharedUserId="android.uid.system">
    ...

<provider
   android:name="com.example.example.CustomFileProvider"
   android:authorities="${applicationId}.provider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths" />
</provider>

提供商路径:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
    <files-path name="files" path="." />
</paths>

软件包安装:

private static void OpenNewVersion(String location) {
       Intent intent = new Intent("android.intent.action.VIEW");
       intent.setDataAndType(getUriFromFile(location), "application/vnd.android.package-archive");
       intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
       intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
       activity.startActivity(intent);
       activity.finish();
   }

   private static Uri getUriFromFile(String location) {
       return CustomFileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", new File(location + fileName));
   }

自定义文件提供程序:

package com.example.example;

import android.support.v4.content.FileProvider;

public class CustomFileProvider extends FileProvider {} 

我不明白为什么我不能使用 FileProvider 作为系统应用程序。有解决办法吗?

最佳答案

如果您在 AndroidManifest.xml 中将导出更改为 true,FileProvider 就会起作用。

像这样:

<provider
   android:name="com.example.example.CustomFileProvider"
   android:authorities="${applicationId}.provider"
   android:exported="true"
   android:grantUriPermissions="true">
   <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths" />
</provider>

但是,这会带来巨大的安全风险。如果您将其更改为 true,则所有其他应用程序将能够在未经许可的情况下使用您的 FileProvider

所以我决定创建自己的安装程序。这样我就不必使用 FileProvider

详细信息: How to Update Android App Silently Without User Interaction

我使用了 ProgressDialog 向用户显示安装进度。

关于java - 权限拒绝 : opening provider that is not exported from UID 1000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779290/

相关文章:

java - 直接分配 volatile 变量线程安全吗?和 AtomicReference 一样安全?

android - 使 FirebaseRecyclerAdapter 在条件下获取数据而不是全部

Android - 如何获取每个应用程序的使用/屏幕时间?

java - 来自 JavascriptInterface 的 android startActivity

Android 使用 FileProvider 拍照

android - FileProvider 和二级外部存储

java - jfieldID 和 jmethodID 是否绑定(bind)到特定类,或者我可以在子类对象上使用它们吗?

java - 我无法在我的代码中启动播放器

android - 从搜索图像的内容 URI 获取绝对文件路径

java - SherlockFragment 内的 SQLite 连接打开错误