The problem
Unity game projects will automatically download APK when they make a major release update, and then call Install APK. Install apK on android 7+
android.os.FileUriExposedException: file:///storage/emulated/0/xxx.apk exposed beyond app through Intent.getData()
why
File paths are restricted on Android 7+ for security purposes, and the official documentation reads as follows:
Passing the file:// URI outside the package network can leave an inaccessible path for the receiver. Therefore, trying to pass the file:// URI triggers a FileUriExposedException. The recommended way to share private file content is to use FileProvider.
To solve
FileProvider way
Tried to solve the problem by using the FileProvider method, but found that apK still failed to be invoked successfully. Check the log:
java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)’ on a null object reference: com.xx.xxx
Verify that the configuration is correct, the XML file is placed in the AAR library, and the AUTHORITIES attribute matches directly.
Disable check Mode
There are other ways to disable the system by reflection. See:stackoverflow.com/a/42437379 In the library’s onCreate method, add the following code:
if(Build.VERSION.SDK_INT>=24) {try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){ e.printStackTrace(); }}Copy the code
After testing, android 7, Android 8, Android 9 success.