Android Question Install new apk version from the apk itself.

vecino

Well-Known Member
Licensed User
Longtime User
Hello, from my apk I download a new version and I run the new apk to install it.
I always get the error: "Application not installed"
Is it possible to install an apk from an apk?
Taking into account that it is the same apk, although a different version.
They have the same name and are running.
I have tried the code of several similar threads, although I do not know if those examples are designed to run another different apk or, what I want to do, is the same apk.
 

vecino

Well-Known Member
Licensed User
Longtime User
Sorry, but I'm a bit confused now.
You have a device for which the code doesn't work, right?
And a second device that executes the updated code on program start instead of after pressing the "1" button, right?
Yes, yes.

In post #18, when you say that it doesn't download the apk file, are you talking about the first device? Are you 100% sure that it's the downloading step at failure rather than the writing after the download (httpjob vs. File.Copy2)? Did you insert a few log statements in the cl_appupdate class to clearly spot the problematic step?
I have checked the Shared directory and it is empty.
I have many doubts about the use of AppUpdate. Can I use the class instead of the library?

As for the retarded upgrading on device #2, what I can say is that the code just send an intent to the OS, which in turn asks the user to allow the installation of the new apk and (on positive) response broadcasts the "android.intent.action.PACKAGE_REPLACED" intent whose action is to wake up any app on the device which has it in the Manifest. Reading the content of the data in this latter intent, service newinst2 (in the library) knows that it's its turn to execute (we're now on the new apk) and launches Main.
Why all that doesn't happen a few seconds after pressing the "1" button but only closing the running app and starting it again, I don't know. Maybe you can try to give the OS more time after pressing the "1" button and see if it completes the loop described above.
Is it possible to execute the update only, only, when the user presses a button?
I would like to do it that way: The user presses a button (when he wants) and then it checks if there is a new version, it is downloaded and updated.
Just that. But at the moment I have not managed to do it with any device. I do not know the motive

ps: let me know if you'd like me to prepare a special version of the lib full of logs in order to better undesrtand the probolem about device #1 or whether you prefer to do it yourself.
I do not want to bother you, I'm stealing you a lot of time. Do it only if you can really dedicate a time that you do not mind dedicating to help me.
Many thanks.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I have checked the Shared directory and it is empty.
I have many doubts about the use of AppUpdate. Can I use the class instead of the library?

That Shared is empty is a bit strange. Worth investigating.
About the use of the class, absolutely yes. The library is there just to have a "container" for the class and the service. You are free to use all the code the way you mostly like (and, obviously, free to amend, modify and personalize it). if you choose to go with the class, please don't forget to add the newinst2 service too (yes, you can name it differently but then you have to modify the Manifes accordingly)

Is it possible to execute the update only, only, when the user presses a button?
Yes. There's no need to check at app start. All the methods of the class are designed to work "on request". Whether you call one after the other (read current version, read webversion, compare, download, install) or the "do it all" one, you can place your call in a button click. As said, I've used your exact code and it worked for me.

I do not want to bother you, I'm stealing you a lot of time. Do it only if you can really dedicate a time that you do not mind dedicating to help me.
It will be a 10 minutes operation. I plan to do it at lunch time. I am curious to read the resulting logs.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi, please find attached :
- source code of the lib with a few extra logs here and there
- source code for your viamos project, sligthly modified in order to acquire more info.
The spoiler below shows my log window running the modified versions of viamos+lib
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
SDK#: 19 - UseFP: false - SharedFolder: /storage/emulated/0/Android/data/viamos.com/files/shared
check 4
---- AppUpdating.UpdateApk
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Webserver's info file content:
ver=2
Web version number: 2
Current Version: 1
Newer version available. Now I try its downloading
-- TryApkUpdate
Donload ok? true
Copy2 (saving) ok? TRUE
new apk version downloaded and ready to install
user asked to install new apk
UpdateComplete - time: 12:03:24
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **

Please run the code as published in this post and return abck your log's window content. Thank you.
 

Attachments

  • AU202_src.zip
    15.1 KB · Views: 187
  • viamos.zip
    30 KB · Views: 190
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, friend, and many thanks for your help.
The program "viamos" edited by you, works well in one of the devices.
In the other, it always shows the error -8

However, I tried to compile it in debug mode and, it works fine !!!
But in release mode the error message -8 is always output as usual.

I copy the log:
Logger connected to: unknown miTab LIVE
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
check 4
---- AppUpdating.UpdateApk
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Webserver's info file content:
ver=2
Web version number: 2
Current Version: 1
Newer version available. Now I try its downloading
-- TryApkUpdate
failed download of new apk version
UpdateComplete - time: 21:38:50
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Did you compile it with version 2.02 of the lib? I can't see the extra logs I added there, specifically the one about SDK, FP and SharedFolder (line 69 in class Initialize).
Reading "check 4" after Main Resume tells me that any installation requirement is fullfilled, so the problem should be confined to the temp directory (as we suspect from the beginning).
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I do not know what I should type to replace this: eu.dgconsulting.appupdating.newinst2

B4X:
AddReceiverText(eu.dgconsulting.appupdating.newinst2,
  <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" />
  </intent-filter>)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

udg

Expert
Licensed User
Longtime User
It depends on how you should reach the service code.
If you build a new library named ApkVecino whose package name is com.viamos.apkvecino AND rename the service to be myinst123 then the snippet in the Manifest will be:
B4X:
AddReceiverText(com.vecino.apkvecino.myinst123,
  <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" />
  </intent-filter>)

Alternatively, if you directly add class and service code to your program (the app that gets updated) than the snippet will look like:
B4X:
AddReceiverText(com.vecino.myinst123,
  <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" />
  </intent-filter>)

My suggestion is to dedicate some more time to solve the problem with your first device before coding a personalized solution based on my code.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello again
I've done a test normally, in release mode.
Show the error -8

Then I made another test, in debug mode. It has worked.

Attached the complete log of the 2 tests.
I hope that information will serve you.
Regards.

Logger connected to: unknown miTab LIVE
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
SDK#: 19 - UseFP: false - SharedFolder: /mnt/sdcard/Android/data/viamos.com/files/shared
check 4
---- AppUpdating.UpdateApk
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Webserver's info file content:
ver=2
Web version number: 2
Current Version: 1
Newer version available. Now I try its downloading
-- TryApkUpdate
Donload ok? true
Copy2 (saving) ok? FALSE
failed download of new apk version
UpdateComplete - time: 10:58:15
** Activity (main) Pause, UserClosed = true **
Logger connected to: unknown miTab LIVE
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
SDK#: 19 - UseFP: false - SharedFolder: /mnt/sdcard/Android/data/viamos.com/files/shared
check 4
---- AppUpdating.UpdateApk
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Webserver's info file content:
ver=2
Web version number: 2
Current Version: 1
Newer version available. Now I try its downloading
-- TryApkUpdate
Donload ok? true
Copy2 (saving) ok? FALSE
failed download of new apk version
UpdateComplete - time: 10:58:15
** Activity (main) Pause, UserClosed = true **
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
SDK#: 19 - UseFP: false - SharedFolder: /mnt/sdcard/Android/data/viamos.com/files/shared
check 4
---- AppUpdating.UpdateApk
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Webserver's info file content:
ver=2
Web version number: 2
Current Version: 1
Newer version available. Now I try its downloading
-- TryApkUpdate
Donload ok? true
Copy2 (saving) ok? TRUE
new apk version downloaded and ready to install
user asked to install new apk
UpdateComplete - time: 11:00:34
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Very informative, indeed.
Test #1 fails at the OpenOutput or Copy2 step (opening or writing the buffer containing the correctly downloaded new apk). That was the "catch" playing its role.
Test #2 works although anything stay unchanged, the only difference being the former was conducted as Release the latter as Debug.

Can you place a log statement right below the OpenOutput line (261 in cl_appupdate) and soon above the Copy2 instruction? Then run again the Release mode test.
What I'd like to understand is whether OpenOutput fails (that's what I expect) or it passes ok and then Copy2 fails. If the latter, maybe a short delay between the two could cure the problem (based on the assumption that the debugger let the code run slower, so we introduce an "artificial" delay).

SharedFolder: /mnt/sdcard/Android/data/viamos.com/files/shared looks correct as a virtual secondary storage in a 4.4 device. It is what GetSafeDirDefaultExternal returns, anyway.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, the error occurs on the line:
out = File.OpenOutput (SharedFolder, "tmp.apk", False)
From there, jump to the catch.
I have removed the "try catch" and I attached a screen copy so you can see everything.



9d396a8fdb438b8f8b9b2a96875130bbo.png
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, finally it worked well :)
The change has been to replace File.DirInternal with File.DirDefaultExternal
Although the result is the same directory in both cases :confused:
B4X:
UseFileProvider = True
'' 'SharedFolder = File.Combine (File.DirInternal, "shared")
SharedFolder = File.Combine (File.DirDefaultExternal, "shared")

Log ("--- >>>>>> SharedFolder:" & SharedFolder)

File.MakeDir ("", SharedFolder)
I do not understand much about these matters, but it seems very strange to me.

THANK YOU VERY MUCH for your help.

5eec66ff07b971a9e5132c6832ab6f4bo.jpg


5eec66ff07b971a9e5132c6832ab6f4bo.png
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Oh, I just checked that the code does not pass through there, but through the "else".
Why does it work now? I have not made any more changes :eek::eek::eek:
However, it works well.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
If I do not see it, I do not believe it.
Finally, I have found the problem, and it is impossible to believe.
I have done many tests and the result is always the same.

With DirInternal it does not work.
With DirDefaultExternal it works.

It's incredible, because the code does not pass through there, but rather through the else:
Does anyone have a reasonable explanation?
B4X:
If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        UseFileProvider = True
        '''SharedFolder = File.Combine(File.DirInternal, "shared")
        SharedFolder = File.Combine(File.DirDefaultExternal, "shared")
        
        Log("(1)--->>>>>> SharedFolder: "&SharedFolder)
        
        File.MakeDir("", SharedFolder)
    Else
        UseFileProvider = False
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")

00ee2796375ca0c3d74c47023e366c89o.png
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you very much.
There's a Permission denied error on the path returned by GetSafeDirDefaultExternal. This shouldn't happen. And you have evidence that using the exact same code in debug mode (which return the same path) everything works.
The DirInternal/DirDefaultExternal case is even more incredible. Time to open a specific thread to drive Erel's attention on that point alone.
 
Upvote 0
Top