android 7.0 apk version updates, automatically installed after download

Keywords: Android FileProvider xml network

android 7.0 apk version updates, automatically installed after download

This time is to record the project check update, download the new version of apk, install automatically, install automatically after 7.0 and before

Reference Articles
http://blog.csdn.net/czhpxl007/article/details/53781464
http://blog.csdn.net/yulianlin/article/details/52775160
http://blog.csdn.net/cjpx00008/article/details/54293571

Before Android 6.0

Automatic Installation Using Intent

        //apkFile is a downloaded apk file
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.fromFile(apkFile);
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);

After Android 7.0

The above code will report the following error on the 7.0 system:

Caused by: android.os.FileUriExposedException

that is because android7.0 The system improves the security of private files and restricts access to private file directories. This setting can prevent the metadata leakage of private files, such as their size or existence.
Passing a file://URI outside the package network may leave an inaccessible path for the receiver. Therefore, trying to pass a file://URI triggers FileUriExposedException. The recommended way to share private file content is to use FileProvider.

  • 1. Define a FileProvider
<mainfest>
    ...
    <application>
        ...
        <!--Be used for7.0Automatic installation of the above system apk-->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider"
                />
        </provider>
        ...
    </application>
    ...
 </mainfest>
  • 2. File directories to add permissions available
    Create the xml folder under the res folder, and then create the file_provider file
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--upgrade-->
    <external-cache-path
        name="mn_update_external_cache"
        path="" />

    <cache-path
        name="mn_update_cache"
        path="" />
</paths>

Note: filename (file_provider) here and provider in manifest

android:resource="@xml/file_provider"

Corresponding.

  • 3. Call in code, call up installation
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data= FileProvider.getUriForFile(mContext, "com.laihui.pinche.fileProvider", apkFile);
        // Apply a temporary authorization to the target
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);

So, in order to adapt, you need to add judgment when setting up apk. Here is the complete code, before and after adapting 7.0

private void installAPk(File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //If SDCard write permission is not set, or SDCard is not set, the APK file is saved in memory, and permissions need to be granted to install it.
        Uri data;
        // Judge that the version is greater than or equal to7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "net.csdn.blog.ruancoder.fileprovider"That is, it is configured in the manifest file. authorities
            data = FileProvider.getUriForFile(mContext, "com.laihui.pinche.fileProvider", apkFile);
            // Apply a temporary authorization to the target
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            data = Uri.fromFile(apkFile);
        }
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

Problems or exceptions encountered in use are recorded here:


This exception led to the compilation of the project. However, after solving it, it was assumed in English that provider s were also used elsewhere in the project (or by third parties).

Solve

1. Double-click on the last line of the figure above to see the information that is unusual.
2. Or click on the location below.

English is limited, guess what:
This is google's recommendation.

Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:170:13-174:19 to override.

It roughly means adding tools:replace="android:resource" to 170 lines of manifest.

Solve similar problems in turn

Posted by minifairy on Sun, 02 Jun 2019 12:39:38 -0700