Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

c# - Xamarin Forms Android Ad-Hoc apk update There was a problem parsing the package

Trying to create an android autoupdater for an ahoc app in Xamarin forms. here is my code and manifest and paths xml.

The app launches fine and i can get the updater dialog. it then seems to download the apk but when it comes to install it i get the 'There was a problem parsing the package' error.

I have tried loads of different ways to do this so some pointers would be cool.

i also tried to look in logcat in release and its saying file permission error but im not sure if this is a red herring or not.

Anyone got any ideas?

```
public async void LaunchActivityInAndroid()
        {


            string documentsPath = $"{Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath}";

            string m_uri = $"{GlobalVariables.ApkPrefix}{GlobalVariables.ApkDomain}{GlobalVariables.ApkEndpoint}";
            string fileName = m_uri.Substring(m_uri.LastIndexOf("/") + 1);

            HttpResponseMessage httpResponse = await new HttpClient().GetAsync(m_uri);
            byte[] data = await httpResponse.Content.ReadAsByteArrayAsync();

            string directory = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath.ToString();
            string path = Path.Combine(directory, fileName);
            foreach (string file in Directory.GetFiles(directory))
            {
                if (Path.GetExtension(file) == ".apk")
                {
                    File.Delete(file);
                    File.Delete("app.db3");
                }

            }

            File.WriteAllBytes(path, data);

            Android.Net.Uri fileUri;
            if ((int)Build.VERSION.SdkInt < 23) {
                fileUri = Android.Net.Uri.FromFile(new Java.IO.File(path));
            }
            else {
                fileUri = FileProvider.GetUriForFile(Xamarin.Forms.Forms.Context as Context, Xamarin.Forms.Forms.Context.ApplicationContext.PackageName + ".fileprovider", new Java.IO.File(documentsPath + "/app.apk")); 
            }

            Intent promptInstall = new Intent(Intent.ActionView).SetFlags(ActivityFlags.ClearTop).SetFlags(ActivityFlags.GrantReadUriPermission).SetFlags(ActivityFlags.NewTask).PutExtra(Intent.ExtraNotUnknownSource, true).SetDataAndType(fileUri, "application/vnd.android.package-archive");
            Android.App.Application.Context.StartActivity(promptInstall);


            //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);

        }
''' 

Manifest

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.99" package="app" android:installLocation="auto" android:versionCode="1">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="29" />
    <application android:label="app.Android" android:icon="@mipmap/ic_launcher" android:networkSecurityConfig="@xml/network_security_config" android:allowBackup="false" android:requestLegacyExternalStorage="true">
        <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"></meta-data>
        </provider>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
    <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
    <uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
    <uses-permission android:name="android.permission.WRITE_PROFILE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
</manifest>

```

provider paths xml

```
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
      name="external"
      path="." />
    <external-files-path
      name="external_files"
      path="." />
    <cache-path
      name="cache"
      path="." />
    <external-cache-path
      name="external_cache"
      path="." />
    <files-path
        name="files"
        path="." />
</paths>
```

Thanks in advance guys!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...