Android Question [SOLVED] Write #Attribute to Textfile

Air

Member
Licensed User
Hello,

i'm trying to write the Value of the Attribute #VersionName to a Textfile in Object-Folder

B4X:
#Region  Project Attributes
    #VersionName: 0.48
#End Region

#CustomBuildAction: 4, c:\windows\system32\cmd.exe,/c echo #VersionName >upd

In the Textfile upd only the AttributeName is written.

How can i write the Value (0.48) of #VersionName into the Textfile upd?
 
Last edited:

Erel

Administrator
Staff member
Licensed User
It will not work. The value is not stored in any compiler variable.

What do you need it for?

You can access it at runtime with:
B4X:
Application.VersionName
 

Air

Member
Licensed User
It will not work. The value is not stored in any compiler variable.
What do you need it for?
After Compiling my App, i copy the APK-File to my Server
also i want to copy the upd-File (with the new Version inside) to my Server

My App can read the UPD-File with a PHP-Link.

If the Result of the PHP-Link > then the installed APP i ask the User to Download the new APK-File.

My current Solution is to write the last compiled Version manually to the UPD-File and copy it to my Server.
 

Erel

Administrator
Staff member
Licensed User
1. Create a B4J non-ui program with code similar to:
B4X:
Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
   For Each f As String In File.ListFiles("..\")
       If f.EndsWith(".b4a") Then
           Dim attributes As List
           attributes.Initialize
           Dim rx As RegexBuilder
           rx.Initialize
           rx.Append("#").StartCapture.AppendAnyBut(Array(":")).AppendAtLeastOne.EndCapture
           rx.Append(":").Append(rx.CharWhitespace).AppendZeroOrMore
           rx.StartCapture.Append(rx.CharAny).AppendZeroOrMore.EndCapture
           For Each line As String In File.ReadList("..\", f)
               If line.ToLowerCase.Contains("process_global") Then
                   Exit
               End If
               Dim m As Matcher = Regex.Matcher(rx.Pattern, line)
               If m.Find Then
                   attributes.Add(m.Group(1) & ":" & m.Group(2))
               End If
           Next
           File.WriteList("c:\temp", "attributes.txt", attributes) '<-- change as needed
       End If
   Next
End Sub
Example is attached.

2. Add this line to your project:
B4X:
#CustomBuildAction: 2, C:\Program Files\Java\jdk1.8.0_51\bin\java.exe, -jar c:\temp\attributes.jar
Change the paths as needed.

It will create a text file with the project attributes. Note that it ignores conditional compilation symbols.
 

Attachments

Air

Member
Licensed User
Thx Erel,

first i have to Install B4J... (never used it)

I will try it

Again thank you for your Example
 

Air

Member
Licensed User
Here´s another Quick and Dirty Solution for Windows-Powershell

Put the Powershell-Script to your Project-Folder

B4X:
pushd $args[0]
$input_path = $args[1]
$output_file = 'Objects\upd'
$regex = '(?<=#VersionName:.*)[0-9.]+'

select-string -Path $input_path -Pattern $regex | % { $_.Matches } | % { $_.Value } >$output_file
and save it as getV.ps1


In Your Project put the Line

B4X:
#CustomBuildAction: 4, c:\windows\system32\cmd.exe,/c PowerShell.exe -ExecutionPolicy Bypass -File ..\getV.ps1 ..\ YourProject.b4a
The UPD-File that includes the Value of #VersionName will be saved in the SubFolder 'Objects' as upd
 

Air

Member
Licensed User
If you need the UPD-File in UTF-8 Format, then type
B4X:
pushd $args[0]
$input_path = $args[1]
$output_file = 'Objects\upd'
$regex = '(?<=#VersionName:.*)[0-9.]+'

$sum = select-string -Path $input_path -Pattern $regex | % { $_.Matches } | % { $_.Value }
write-output $sum | out-file $output_file -encoding utf8
and save it as getV.ps1
 

elpic76

Member
Licensed User
Good morning,
I tried the example AttributesExtractor inn B4J and compiled the file in Release mode.
It produces a jar file named result.jar.
In order to be compliant with what is suggested do I need to rename it from result.jar to attributes.jar?
Or can I use the following instruction?

B4X:
#CustomBuildAction: 2, C:\Program Files\Java\jdk1.8.0_51\bin\java.exe, -jar c:\temp\result.jar
Best Regards
 
Top