Forum Xamarin.Android

"Problem while parsing the package" error trying run apk installation from code.

Hi ,
I have the same problem as many another ppl: "There was a problem while parsing the package." when I try to install package from app. Package installs from Download folder without problem, reinstall from this folder works as well. But start installation from app gives error. Googling for three days without result.

Code is:

    Intent intent = new Intent(Intent.ActionView);
    string fileApk = "com.packagename.apk";

    Java.IO.File file = new Java.IO.File(Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath.ToString(), fileApk));
`//file = {/storage/emulated/0/Download/com.packagename.apk}`

      Android.Net.Uri uri = FileProvider.GetUriForFile(Android.App.Application.Context, "com.packagename.provider", file);
`//uri = {content://com.packagename.provider/ex_storage/Download/com.packagename.apk} `

    intent.AddFlags(ActivityFlags.GrantReadUriPermission);

    intent.AddFlags(ActivityFlags.NewTask);

    intent.SetDataAndType(uri, "application/vnd.android.package-archive");

    Android.App.Application.Context.StartActivity(intent);

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="2.0" package="com.packagename" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="24" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application android:label="Application" android:icon="@drawable/icon48">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.packagename.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_path" />
        </provider>
    </application>
</manifest>

provider_path.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
             <external-path path="." name="ex_storage" />
</paths>
Tagged:

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @JustAlex

    Please try to use following code to install the apk:

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.SetVmPolicy(builder.Build());
    
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(Uri.FromFile(new File(Environment.ExternalStorageDirectory + "/download/" + "test.apk")), "application/vnd.android.package-archive");
            intent.SetFlags(ActivityFlags.NewTask);
            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
            StartActivity(intent);
    

    You could refer to the following links for more detail:
    https://stackoverflow.com/questions/4967669/android-install-apk-programmatically
    https://stackoverflow.com/a/40674771/7063418

  • JustAlexJustAlex Member ✭✭

    @BillyLiu Thank you trying to help. Your proposed code will not work for for SDKversion >22 (method Uri.FromFile). Should use FileProvider.
    I resolved my problem after phone factory reset only. I do not know why, but one of my app's version did not uninstall correctly. Even with adb I could not uninstall it. So, the way only was to reset my phone. Now all is OK. Tnx.

  • edcsuscribedcsuscrib Member

    I have the same problem and can't get it to work.

    The file that is downloaded by system and that would be the update of the app, if I execute it manually it works, if I execute it from the same app it does not work.

    Any ideas?

Sign In or Register to comment.