Android Question Unable to parse package

nibbo

Active Member
Licensed User
Longtime User
OK, first off I know this is not a B4A error and is android related; that said I do need to understand what is going wrong before I hand out any new Android 10 devices.

I have an app that checks for newer versions of itself by comparing the package version with the latest version via a web server, it then downloads the new apk and starts the install intent.
This works on all devices I have tried except this new one which is probably the only one I have running Android 10.
The app has permissions to install and the download seems to work OK but the install itself always comes up with the dreaded 'There was a problem while parsing the package.' message.
The existing app was compiled and installed from the same machine as the new apk version so has the same signature and package name.
Below is the install intent code (sdkVersion is > 24) which works on all other devices I have tried.

B4X:
                ProgressDialogShow("Installing...")
                Dim i As Intent
                If phone.SdkVersion >= 24 Then
                    i.Initialize("android.intent.action.INSTALL_PACKAGE", "content://" & File.Combine(File.DirInternalCache, "MyApp.apk"))
                    i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
                Else
                    i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternalCache, "MyApp.apk"))
                    i.SetType("application/vnd.android.package-archive")
                End If
                StartActivity(i)

Has anyone got any idea what else could be causing the error?

Thanks
Nibbo
 

DonManfred

Expert
Licensed User
Longtime User
Follow the code in this Tutorial
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Follow the code in this Tutorial

That is the tutorial I followed.
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
If I download the apk manually and install it everything works just fine so I can only conclude that the apk file is not downloading properly but again, why just on this device?
Code including the download bit:

B4X:
ProgressDialogShow("Downloading...")
            j.Initialize("JobApkDownload", Me)
            j.Download("https://webservice.woodlodge.co.uk/androidApp/Ordering.apk")
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                ProgressDialogShow("Saving...")
                Dim out As OutputStream
                out = File.OpenOutput(File.DirInternal,"Ordering.apk",False)
                File.Copy2(j.GetInputStream, out)
                out.Close
                Sleep(1000)
                j.Release
                ProgressDialogShow("Installing...")
                Dim i As Intent
                If phone.SdkVersion >= 24 Then
                    i.Initialize("android.intent.action.INSTALL_PACKAGE", "content://" & File.Combine(File.DirInternal, "Ordering.apk"))
                    i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
                Else
                    i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternal, "Ordering.apk"))
                    i.SetType("application/vnd.android.package-archive")
                End If
                StartActivity(i)
            Else
                ProgressDialogHide
                j.Release
                Msgbox("Update failed", "Error")
                Return False
            End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
What Android Version is running on this Device?
It the Android Version on all Devices the same?

PS: It is NOT the same code you are using. Only parts of them

for ex. i am missing this code (and probaly MORE)
As you can see the Example from the Tutorial is using FileProvide while your code does not use FileProvider?! Yours will fail on Android SDK 24+ or 26+

B4X:
Sub Button1_Click
    Wait For (CheckInstallationRequirements) Complete (Result As Boolean)
    If Result Then
        SendInstallIntent
    End If
End Sub

Private Sub CheckInstallationRequirements As ResumableSub
    If File.ExternalWritable = False Then
        MsgboxAsync("Storage card not available. Make sure that your device is not connected in USB storage mode.", "")
        Return False
    Else If phone.SdkVersion >= 26 And CanRequestPackageInstalls = False Then
        MsgboxAsync("Please allow me to install applications.", "")
        Wait For Msgbox_Result(Result As Int)
        Dim in As Intent
        in.Initialize("android.settings.MANAGE_UNKNOWN_APP_SOURCES", "package:" & Application.PackageName)
        StartActivity(in)
        Wait For Activity_Resume '<-- wait for Activity_Resume
        Return CanRequestPackageInstalls
    Else If CheckNonMarketAppsEnabled = False Then
        MsgboxAsync("Please enable installation of non-market applications." & CRLF & "Under Settings - Security - Unknown sources" _
             & CRLF & "Or Settings - Applications - Unknown sources", "")
        Return False
    Else
        Return True
    End If
End Sub

Private Sub CanRequestPackageInstalls As Boolean
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
    Return PackageManager.RunMethod("canRequestPackageInstalls", Null)
End Sub

Private Sub CheckNonMarketAppsEnabled As Boolean
    If phone.SdkVersion >= 26 Then Return True
    If phone.SdkVersion < 17 Or phone.SdkVersion >= 21 Then
        Return phone.GetSettings("install_non_market_apps") = "1"
    Else
        Dim context As JavaObject
        context.InitializeContext
        Dim resolver As JavaObject = context.RunMethod("getContentResolver", Null)
        Dim global As JavaObject
        global.InitializeStatic("android.provider.Settings.Global")
        Return global.RunMethod("getString", Array(resolver, "install_non_market_apps")) = "1"
    End If
End Sub

Private Sub SendInstallIntent
    Dim ApkName As String = "2nd_app.apk"
    File.copy(File.DirAssets, ApkName, Starter.Provider.SharedFolder, ApkName)
    Dim i As Intent
    If phone.SdkVersion >= 24 Then
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Starter.Provider.GetFileUri(ApkName))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.Provider.SharedFolder, ApkName))
        i.SetType("application/vnd.android.package-archive")
    End If
    StartActivity(i)
End Sub
 
Last edited:
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
I do all of the permissions checking and that is all working. The problem is presumably the download.

The version of this device is 29, I just tried a version 27 and that does the same so I guess it is the FileProvider bit...

Out of curiosity; why do I suddenly need to use a FileProvider?
Does DirInternal not work anymore? My apps store tons of data in there and I can access it Ok for my database and my images etc...
 
Last edited:
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
OK, using the FileProvider works but it does introduce other problems.
After the install the data from the previous version is lost as if I had installed the app for the first time.
This includes over 7000 images and a database which will now take 40 minutes to download again, my users will have a cow if they have to do this every time there is a new version.

I changed the app to use DirDefaultExternal (with the permission etc...) only to find that it is still within the app directory and we still get the parse error.

How can I do an automated update that does not destroy the data from previous installs?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The fact that you are using File.DirDefaultExternal means that you haven't watched the tutorial.
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Thanks Erel but still no further forward.
My code did the correct permissions checks and admittedly I was still using DirDefaultExternal but the GetSafeDirDefaultExternal is the same...

B4X:
'    need to be able to access external storage
        If phone.SdkVersion >= 24 Then
            rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
            Wait For Activity_PermissionResult (Permission As String, rpResult As Boolean)
            If Not(rpResult) Then
                'no permission
                Return False
            End If
        End If

        Log(File.DirDefaultExternal)
        Log(rp.GetSafeDirDefaultExternal(""))

When I run the above both directories resolve to the same 'external' directory.

B4X:
/storage/emulated/0/Android/data/project.name/files

And obviously both lead to the apk parsing error.

Just to add I do have the manifest entries in there also.

PS... just checked and the apk file is in the expected folder so it looks like it is just the install that is failing.
 
Last edited:
Upvote 0
Top