Android Tutorial Compilation time and auto-increment build version

This tutorial demonstrates how the custom build action feature can be used to add a file during compilation with the compilation time and an automatic version number that is incremented with every compilation.

Using it is simple:

1. Download the attached jar file and save it.
2. Add this line to your project:
B4X:
#CustomBuildAction: folders ready, %JAVABIN%\java.exe, -jar C:\Users\H\Downloads\compiletime.jar
Change the path to the downloaded jar.

During compilation a file named compiletime.txt will be created in the Files folder.
You can read its data with:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     Dim compiledata As Map = File.ReadMap(File.DirAssets, "compiletime.txt")
     Log($"autoversion: ${compiledata.Get("autoversion")}"$)
     Log($"compilation time: $DateTime{compiledata.Get("time")}"$)
   End If
End Sub

Run your app once and then go to the Files tab and click on Sync button (this will remove the file warning).

The small utility code (B4J) is:
B4X:
Sub AppStart (Args() As String)
   Dim m As Map
   Dim path As String = "../Files/compiletime.txt"
   If File.Exists(path, "") Then
     m = File.ReadMap(path, "")
   Else
     m.Initialize
   End If
   m.Put("autoversion", m.GetDefault("autoversion", 0) + 1)
   m.Put("time", DateTime.Now)
   File.WriteMap(path, "", m)
End Sub

If you only want to increment the version for Release builds:
B4X:
#If RELEASE
#CustomBuildAction: 2, ...
#End If

Edit: example of code that extracts the project attributes: https://www.b4x.com/android/forum/threads/write-attribute-to-textfile.88549/#post-560535
 

Attachments

  • compiletime.jar
    81.9 KB · Views: 1,059
Last edited:

swChef

Active Member
Licensed User
Longtime User
If you want to avoid using abs/rel paths you can use a %variable%, such as %ADDITIONAL%, but need to use a shortened form of the CustomBuildAction.
B4X:
    #CustomBuildAction: folders ready, %ADDITIONAL%\attributes.jar,
    #CustomBuildAction: folders ready, %ADDITIONAL%\compiletime.jar,
For B4A, the jars need to be placed in the B4A folder under your Additional Libraries (IDE Tools/) configured path.
Then these same lines can be copied directly into any project w/o changes.
 
Top