Android Question Problem with update APK with FileProvider

Matteo Giorgini

Member
Licensed User
Hi Guys!
For update my APP, I decided to use an FTP.
Now... before old Android version I launched an intent and all worked.
Today I update my code and I added "FileProvider" taken from [https://www.b4x.com/android/forum/threads/version-safe-apk-installation.87667/#content]

Naturally, I have updated my project:
- Javac.exe: 11.0.1
- android.jar: 28
- Android's version on device: 10

I modified Manifest (like sample InstallAPK) and now is:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_MEDIA_STORAGE)
AddPermission(android.permission.READ_MEDIA_STORAGE)
'End of default text.

AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
)
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
'CreateResource(xml, provider_paths,
'   <external-files-path name="name" path="shared" />
')
'AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)

CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)
AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)

I created Starter service, for create FileProvider.
First time that launch update, Android ask me to set unknown source for update: I setted it.

When update starts, I receive a starnge message: "Error while parsing the package."
APK not was installed.
If I open manually "shared" folder and tap on APK, setup starts and end without problems.


"My" SendInstallaIntent procedure is:
B4X:
Private Sub SendInstallIntent
    Dim ApkName As String = "StoreMobile.apk"
    'File APK was copied in this path from download's flow => File.copy(File.DirAssets, ApkName, Starter.Provider.SharedFolder, ApkName)
    Dim i As Intent
    Dim uri As String = ""
    If phone.SdkVersion >= 24 Then
        uri = Starter.Provider.GetFileUri(ApkName)
        Log(uri)
        i.Initialize("android.intent.action.INSTALL_PACKAGE", uri )
        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

Intent was initialized with this uri:
B4X:
 content://net.commercod.storemobile.provider/name/StoreMobile.apk


If I exec InstallAPK sample on same device works all.

Do you have any idea? Can you help me?

Thanks in advance,
Matteo
 

JohnC

Expert
Licensed User
Longtime User
I had the same problem, but I forgot what ended up fixing the issue.

I tried to do a quick compare between your code and mine, and one thing that stands out is that after I call the "SendInstallintent", I close the activity:
B4X:
Sub Ftp_OK
    
    SendInstallIntent(ApkDirectory, APKFileName)

    Activity.Finish

End Sub

So see if closing the activity will fix this.
 
Upvote 0

Matteo Giorgini

Member
Licensed User
I had the same problem, but I forgot what ended up fixing the issue.

I tried to do a quick compare between your code and mine, and one thing that stands out is that after I call the "SendInstallintent", I close the activity:
B4X:
Sub Ftp_OK
   
    SendInstallIntent(ApkDirectory, APKFileName)

    Activity.Finish

End Sub

So see if closing the activity will fix this.
I try now event without download from ftp...
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try adding a prompt, after the FTP completes, but before calling the StartInstallIntent:

B4X:
   MsgboxAsync("Click OK to start the install","Download Complete")
   Wait For MsgBox_Result (Result As Int)
  
   SendInstallIntent(ApkDirectory, APKFileName)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
FILE Uris are no longer allowed.
android.jar / targetSdkVersion / minSdkVersion

See this Tutorial
 
Upvote 0

Matteo Giorgini

Member
Licensed User
FILE Uris are no longer allowed.
android.jar / targetSdkVersion / minSdkVersion

See this Tutorial
Thanks DonManfred for your time, but I don't understand why InstallAPK sample works with same version on B4A, same device and... same Manifest.
I need to solve this problem, because I can't update apps. I will read another time all specifications but I think than I need an help...
 
Upvote 0

Matteo Giorgini

Member
Licensed User
A little update...
I checked all suggest link and I try this.
I compile in release mode my APP and add it into InstallAPK project.
Install InstallAPK on device with Android 10.
I tap on "install": permission are request, I check "unknown source" and my APP was installed correctly.

targetSdkVersion of InstallAPK and my APP are the same, like android SDK (28).

Why from my APP I continue to have problems?

Where are I wrong?

Thanks in advance...
Matteo
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You might need to slowly start adding in your code to the example (that works), and test after each update to make sure it still runs.

At some point it should fail, and then figure out why the latest update caused the failure.
 
Upvote 0

Matteo Giorgini

Member
Licensed User
You might need to slowly start adding in your code to the example (that works), and test after each update to make sure it still runs.

At some point it should fail, and then figure out why the latest update caused the failure.

Solved! I rewrited code and found error.
Now I've another problem with PERMISSION READ_PHONE_STATE, but I will open a new thread... Thanks!
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Well, what was the fix?

(so it might help others that run into this problem)
 
Upvote 0
Top