所以我在尝试安装 apk 时遇到了 uri 暴露的问题。我编写了一个自动更新 android 应用程序的库,然后使用以下代码安装新的 apk。
Uri uri = VersionManagerUtil.getFileDestinationUri(apkDir, mParameters.getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, RC_REQUEST_INSTALL);
但是,由于 FileUriExposedException,这不适用于 api 24+,所以我切换到 FileProvider 来获取 Uri
Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
这很好用...但是,FileProvider 需要以下依赖项
compile 'com.android.support:support-v4:26.0.0-alpha1'
这是有问题的,因为我想使用这个库的所有项目都需要有相同版本的支持库。那么是否有任何非 hacky 方法来解决 FileUriExposedException 的问题?或者是否有不同的方式来安装 apk 来一起避免这个问题?
最佳答案
However, FileProvider requires the following dependency compile 'com.android.support:support-v4:26.0.0-alpha1'
首先,版本由您决定,当前版本(截至我发布此答案时)是 26.1.0
。
其次,FileProvider
的实际 Artifact 是 support-core-utils
。要求 support-v4
将通过传递依赖项引入 support-core-utils
。
which is problematic because all of the projects I would like to use this library on would need to have the same version of the support library
或者一个兼容的。 FileProvider
已经存在几年了。
So are there any non-hacky ways to resolve the issue of the FileUriExposedException?
您可以使用 FileProvider
。或者,您可以编写自己的 ContentProvider
来提供文件。
Or is there a different way to install an apk that avoids this issue all together?
据我所知不是。
关于android - 除了使用 FileProvider 获取用于安装 Android apk 的 uri 之外,还有其他方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46695990/