简体   繁体   中英

Download and install apk (without publishing in playstore) from the Instant App

I have uploaded .apk file to the dropbox. I want to download and launch app when user clicks an install button in the Instant App. But the following exception occurs after call to startActivity() :

Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{8593d11 2399:com.google.android.packageinstaller/u0a26} (pid=2399, uid=10026) that is not exported from uid 10298

Intent intent = new Intent(Intent.ACTION_VIEW);
if (intent.resolveActivity(getPackageManager()) == null) {
    Log.e("No Packate Manager", intent.getPackage() + "");
    return;
}

Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //BuildConfig.APPLICATION_ID
    // File file = new File(File folder, "app-debug.apk");
    //uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", file);
    uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "app-debug.apk"));
    List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
} else {
    // uri = Uri.fromFile(new File("/mnt/sdcard/Download/app-debug.apk"));
    File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File outputFile = new File(folder, "app-debug.apk");
    uri = Uri.fromFile(outputFile);
}

intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// install.setDataAndType(myuri, manager.getMimeTypeForDownloadedFile(downloadId));

startActivity(intent);

It's not possible to initiate an install of an APK file that is hosted outside of Play Store from an Instant App (nor is it recommended).

The error you are receiving is because Instant Apps cannot read (or write) to external disk storage. For more info on restricted features in Instant Apps, please see the docs . Instant Apps can read/write to their own internal storage though, however they cannot expose or share that data to other apps.

The recommended approach is to publish a regular APK to Google Play and then use the provided showInstallPrompt() helper method to initiate an install. This will direct the user to Google Play to install your app.

You should also review our UX guidelines for presenting such an install button (called an "explicit install prompt").

If you want to download and install .apk file then you don't need to host .apk file on Dropbox instead you can publish it on playstore and from instant app you can redirect your users to playstore to download the app by following this

First add this dependency in your feature module

api "com.google.android.instantapps:instantapps:1.0.0"

Then match you project level build.gradle with this`

buildscript {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
  }...
}

allprojects {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
 }
}

Finally on click of install button in instant app you can call this

InstantApps.showInstallPrompt(MainActivity.this,0,"Install app")

So on this will open your app on playstore that you have published before.

NOTE: You must have app published on playstore before you develop your instant version of app

If you are building an .apk from instant run, it won't run on any other device as it has a feature of hot swapping the changes.

Go to Preferences in Android Studio > Build, Execution, Deployment > Instant Run.

Then you should disable it and then make a build by running. That .apk will run fine on any device.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM