3

I was having some problem when trying to perform an application upgrade in Android emulator. The flow of the scenario is from an Activity, I will execute AsyncTask A which open up fragment A, then inside AsyncTask A, I will check if version upgrade is available.

If available and user selected "Okay" from fragment A, I will proceed to AsyncTask B to open up fragment B which show a message to user saying that upgrading is in process. In AsyncTask B doInBackground(), I will execute the install() and in onPostExecute(), I will show successful message.

In my AsyncTask B where I execute the install:

@Override
protected Boolean doInBackground(Void... params) {
boolean ret= viewmodel.installApk(mActivity);
return ret;
}

In my view model class:

public boolean installApk(Activity mActivity){
    boolean success = false;
    String fullPath = scanDirectoryForApk();

    System.out.println("INSTALLING APK ......................... ");

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
    intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity.startActivity(intent);
    return true;
 }

However, when I execute the code above, no error message was shown and the version upgrade is not working as well. It basically just restart the intent and there is no upgrade at all.

It does not prompt me for the permission to install new version as well. Any ideas?

By the way, my android emulator is not rooted and therefore I could not use the "su" command approach.

Thanks!

EDIT

As suggestion by @Sagar, I changed my code above to:

Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));

    List<ResolveInfo> resInfoList = mActivity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        mActivity.grantUriPermission(packageName, apkURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }

    intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity.startActivity(intent);

And I am getting new error message from logcat:

Error staging apk from content URI
java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{3883c8 9647:com.google.android.packageinstaller/u0a20} (pid=9647, uid=10020) that is not exported from UID 10085

The intent error message is telling me "There was a problem parsing package".

  • Do you see any errors in logcat? Have you checked my answer in this [SO](https://stackoverflow.com/questions/50072638/fileuriexposedexception-in-android/50102119#50102119) for properly handling the file uri exposed exception? – Sagar Jun 11 '18 at 01:15
  • @Sagar yeap I tried the same way. The only errors are: Requesting uid 10085 needs to declare permission android.permission.REQUEST_INSTALL_PACKAGES and Unrecognised action provided: android.intent.action.PACKAGE_REPLACED –  Jun 11 '18 at 01:22
  • Declare `android.permission.REQUEST_INSTALL_PACKAGES` in your manifest, just like any other permission – Sagar Jun 11 '18 at 01:25
  • 1
    @Sagar I am getting new error from the UI saying there is an error parsing the package. When I check the logcat, I am getting this: Error staging apk from content URI java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{df5eecd 6335:com.google.android.packageinstaller/u0a20} (pid=6335, uid=10020) that is not exported from UID 10085 –  Jun 11 '18 at 01:38
  • Check you answer of Lim from this [SO](https://stackoverflow.com/questions/24467696/android-file-provider-permission-denial) – Sagar Jun 11 '18 at 01:41
  • @Sagar I see I see but where do I execute that chunk of code? Before I execute the install()? –  Jun 11 '18 at 01:44
  • Yeah. Do it before install – Sagar Jun 11 '18 at 01:47
  • @Sagar I pasted in the code but it is still throwing me the same error message :( And the error saying there was an error parsing the package persist –  Jun 11 '18 at 01:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172862/discussion-between-sagar-and-guest176969). – Sagar Jun 11 '18 at 01:58
  • @sagar I think I know the problem already. It was due to this line: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); I managed to get to the install intent but the progress always get stuck –  Jun 11 '18 at 06:36
  • Were u able to fix the problem? – rsc May 24 '19 at 03:16

1 Answers1

5

You can try to merge

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

into

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);

I met the same issue as you, and this solution rescued myself.

Huiyang Shan
  • 431
  • 6
  • 8