Android Question automatic change of apk version

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Hi,
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?

Thanks
 
Last edited:

EvgenyB4A

Active Member
Licensed User
Longtime 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.
 
Upvote 0

opus

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

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
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:

B4X:
#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:

B4X:
@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"

exit


: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
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
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"
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%hex%) ELSE ECHO.%hex%
)

Example of the output ver.txt file (located in the project Files directory):

B4X:
151106.8DD1

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:

B4X:
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.

Barry.
 
Upvote 0
Top