Android Question automatic change of apk version

Alberto Michelis

Well-Known Member
Licensed User
1) Is there any way that the IDE change the apk version each time we compile it in release mode?
2) Can I read the apk version from within my code o do I have to mantain a pararle variable?

Last edited:


Active Member
Licensed User
I join this question and wish that exists in all modern IDEs. It will be very convenient to follow the apk version when it needs to be updated. Hope to get response from.


Active Member
Licensed User
On your Q2.)
  Dim apkVersion as String
Dim pm As PackageManager
  apkVersion = pm.GetVersionName(sPackageName)
Although you have to set "sPackageName" before.


Well-Known Member
Licensed User
I have come up with a sort-of solution that doesn't address the problem of changing the APK Version Number (I agree it would be a nice addition to the IDE), but does automatically increment a timestamp to identify a particular build.

I use a combination of three things:
  • In the B4A file I have a Custom Build Action that calls a batch file located within the same directory as the B4A file.
  • The batch file modifies a text file in the Files directory.
  • The text file is included in the Assets directory of the APK.
When my program is running I can read the contents of this text file to display or know the build timestamp.

Custom Build Action:

#CustomBuildAction: 1, C:\Full Path to Directory where ver.bat file is (should be same as .b4a directory)\ver.bat,
The contents of the ver.bat file:

@echo off

set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~12,2%

set hh=%time:~0,2%
set min=%time:~3,2%
set ss=%time:~6,2%

set /a ts=hh * 3600 + min * 60 + ss

call:toHex ts tsh

set bld=%yy%%mm%%dd%.%tsh%

echo %bld%>"%~dp0\Files\ver.txt"


:toHex dec hex -- convert a decimal number to hexadecimal, i.e. -20 to FFFFFFEC or 26 to 0000001A
::             -- dec [in]      - decimal number to convert
::             -- hex [out,opt] - variable to store the converted hexadecimal number in
::Thanks to 'dbenham' dostips forum users who inspired to improve this function
:$created 20091203 :$changed 20110330 :$categories Arithmetic,Encoding
set /a dec=%~1
set "hex="
set "map=0123456789ABCDEF"
for /L %%N in (1,1,8) do (
    set /a "d=dec&15,dec>>=4"
    for %%D in (!d!) do set "hex=!map:~%%D,1!!hex!"
rem !!!! REMOVE LEADING ZEROS by activating the next line, e.g. will return 1A instead of 0000001A
for /f "tokens=* delims=0" %%A in ("%hex%") do set "hex=%%A"&if not defined hex set "hex=0"
    IF "%~2" NEQ "" (SET %~2=%hex%) ELSE ECHO.%hex%
Example of the output ver.txt file (located in the project Files directory):

From within my B4A program I First copy the ver.txt file from Assets to the DefaultExternal directory then use the following code to read the ver.txt file:

File.Copy(File.DirAssets, "ver.txt", File.DirDefaultExternal, "ver.txt")
File.ReadString(File.DirDefaultExternal, "ver.txt")
The copy may not be necessary, but it makes ver.txt easy to find looking in the DefaultExternal directory.