Android Question Install and run apk with in apk

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Hello,

I have a question is it possible to install downloaded apks within another apk

Like i create a button that refers to a apk in the download dir.

That apk will then be installed.

Is this possibble in b4a ?
 

susu

Well-Known Member
Licensed User
Longtime User
You can download an apk file then call install screen but the user need to confirm to install it first.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Ricardo,

most of the functionality you need is part of my AppUpdating library. Look at its source code for hints.

udg
 
Upvote 0

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Hi Udg i tried your Lib with the 1.26 example script but now i get this strange error

java.lang.NoClassDefFoundError: anywheresoftware.b4a.http.HttpClientWrapper
at anywheresoftware.b4a.samples.httputils2.httputils2service._process_globals(httputils2service.java:137)
at b4a.example.appupdate.main.initializeProcessGlobals(main.java:317)
at b4a.example.appupdate.main.afterFirstLayout(main.java:96)
at b4a.example.appupdate.main.access$100(main.java:17)
at b4a.example.appupdate.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)

This is the code from your zip with my apk and info file just to test out

B4X:
#Region  Project Attributes 
    #ApplicationLabel: AppUpdating Example
    #VersionCode: 1
    #VersionName: 1.01
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim apkupdt As cl_appupdate
    Private EditText1 As EditText
    Private btnCurVer As Button
    Private btnPackage As Button
    Private btnWebVer As Button
    Private btnCompare As Button
    Private btnDwnld As Button
    Private btnInstall As Button
    Private ButtonUpdate As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    EditText1.Color=Colors.White
    EditText1.TextColor=Colors.Black
    EditText1.Text="Step by Step library usage"&CRLF
    If FirstTime Then
    apkupdt.Initialize(Me,"update")  
        apkupdt.Verbose = True  'this one affects the verbosity of the logs
    End If   
  'ALWAYS NEEDED - this is yor app's package name (see "Project/Package name")
  apkupdt.PackageName = "b4a.example.appupdate"
  'ALWAYS NEEDED - this is the complete path to the text file holding the newer version number  
  apkupdt.NewVerTxt = "https://archive.org/download/kraakie_gmail_Test/version.info"  
  'ALWAYS NEEDED - this is the complete path to your newer apk
  apkupdt.NewVerApk = "https://archive.org/download/kraakie_gmail_Test/C64.apk" 
    'OPTIONAL - Set credentials to access a protected folder. Not needed for this example
    apkupdt.setCredentials("test","test")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

'STEP 0: which is apk's package name?
'Can be called at any time not necessarily as step 0 as shown here
Sub btnPackage_Click
    'Show package name
    EditText1.Text=EditText1.Text&"Package name: " & apkupdt.PackageName&CRLF
End Sub

'STEP 1: which is apk's current version number? 
'Can be called at any time not necessarily as step 1 as shown here
Sub btnCurVer_Click
    'Read version number of currently executing apk
    apkupdt.ReadCurVN  'send out command; async result in update_UpdateComplete
End Sub
   
'STEP 2: Just curious about eventual availability of a newer apk version, so we simply check for it
'Can be called at any time not necessarily as step 2 as shown here
Sub btnWebVer_Click
    'Read version number as showed in txt file on webserver
    apkupdt.ReadWebVN  'send out command; async result in update_UpdateComplete
End Sub

'STEP 3: Must follows steps 1 and 2. Assuming no errors in steps 1 and 2
Sub btnCompare_Click
    If apkupdt.CurVN < apkupdt.WebVN Then
        EditText1.Text=EditText1.Text&"Newer version available"&CRLF
  Else
     EditText1.Text=EditText1.Text&"No newer version available"&CRLF
  End If
End Sub

'STEP 4: let's download the apk published on the webserver. No check on its version, just download it
'Can be called at any time not necessarily as step 4 as shown here
Sub btnDwnld_Click
  'download apk from webserver raising appropriate errors when problems arise
    apkupdt.DownloadApk  'send out command; async result in update_UpdateComplete
End Sub

'STEP 5: Must follow step 4. Let's ask the user to install the just downloaded apk's newer version
Sub btnInstall_Click
    apkupdt.InstallApk  'send out command; async result in update_UpdateComplete
End Sub

'Do it all (steps 1 to 5) with a single call
Sub ButtonUpdate_Click
    EditText1.Text="A single sub call does it all"&CRLF
  'OPTIONAL - if you like to show a splash screen while checking for a newer apk goes on
    apkupdt.SetAndStartSplashScreen(Activity,LoadBitmap(File.DirAssets, "updating.jpg")) 
  'NEEDED - this is the one you need if you want to perform "automatic" updating of your apk
    apkupdt.UpdateApk 'checks for newer apk, downloads it and asks the user to install it                                                    
End Sub

'This subs gets called after each command is executed
Sub update_UpdateComplete
  apkupdt.StopSplashScreen
    'too lazy to manage error conditions..check apkupdt.ERR_xxx codes if you like
    Select apkupdt.Status
     Case apkupdt.OK_CURVER
         EditText1.Text=EditText1.Text&"Running apk version: " & apkupdt.CurVN&CRLF
     Case apkupdt.OK_WEBVER
         EditText1.Text=EditText1.Text&"Webserver apk version: " & apkupdt.WebVN&CRLF
         EditText1.Text=EditText1.Text&"Optional Change Log data: " & apkupdt.WebChangeLog&CRLF
     Case apkupdt.OK_NEWERAPK
         EditText1.Text=EditText1.Text&"Newer version available"&CRLF
    Case apkupdt.NO_NEWERAPK
         EditText1.Text=EditText1.Text&"No newer version available"&CRLF
     Case apkupdt.OK_DOWNLOAD
         EditText1.Text=EditText1.Text&"Newer version downloaded"&CRLF
        Case apkupdt.OK_INSTALL
         EditText1.Text=EditText1.Text&"User asked to install newer version"&CRLF
     Case Else
         EditText1.Text=EditText1.Text&"Status: "&apkupdt.Status&CRLF
    End Select       
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Ricardo,
sorry for what appears as a misunderstanding.
My lib is used to update the same apk it's part of when your app is not published in the Play Store. So, as is, it's not what you need to accomplish your goal.
My suggestion from post #4 was simply to look at my lib's code in order to extract those functions you may need to code your own solution.
BTW, accessing your info txt file (version.info) I can see it's not in the form expected by my lib.

udg
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
Hi,

Check for an updated version of your app and download the apk from your site, then install it with this code :

B4X:
Sub InstallAPK       
    If File.Exists(File.DirRootExternal , "NEWVERSION.apk") Then
        Dim ii As Intent
        ii.Initialize(ii.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal, "NEWVERSION.apk"))       
        ii.SetType("application/vnd.android.package-archive")
        StartActivity(ii)                               
    End If
End Sub
 
Upvote 0

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Hi Ricardo,
sorry for what appears as a misunderstanding.
My lib is used to update the same apk it's part of when your app is not published in the Play Store. So, as is, it's not what you need to accomplish your goal.
My suggestion from post #4 was simply to look at my lib's code in order to extract those functions you may need to code your own solution.
BTW, accessing your info txt file (version.info) I can see it's not in the form expected by my lib.

udg
Thnx problem was your example code the weblinks are dead so could not see how the info file was build :)
 
Upvote 0

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
Hi,

Check for an updated version of your app and download the apk from your site, then install it with this code :

B4X:
Sub InstallAPK      
    If File.Exists(File.DirRootExternal , "NEWVERSION.apk") Then
        Dim ii As Intent
        ii.Initialize(ii.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal, "NEWVERSION.apk"))      
        ii.SetType("application/vnd.android.package-archive")
        StartActivity(ii)                              
    End If
End Sub

Thnx M8 will take a look at this
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Ricardo,
thanks for your feedback; I wasn't aware the demo is no more functional..
Anyway, the info file I use is defined in post #2 (old scheme), #62 (a better one) and #84 (most recent).
You can devise your own scheme. As Ohanian showed, from my lib's code you need to copy just those few functions that let you download a file (presumibly an updated apk), save it locally and finally calling the installation intent which is responsible to ask the user about he/she intention to update.
I never tried to update program B while executing program A, so you've to experiment a bit, I guess.

udg
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
That leads me to a question:
Is there such a thing in android as a "silent install"?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Is there such a thing in android as a "silent install"?

AFAIK, a silent installation is available only on rooted devices.

udg
 
Upvote 0
Top