Tool [B4X] JsonLayouts - synchronize json and binary layouts

The purpose of this tool is to automatically generate json layout files, and synchronize them as they are updated. This can be useful when working with a source control and especially when multiple developers work on the same project.

Usage:
1. Download jar and put in additional libraries folder: https://www.b4x.com/b4j/files/JsonLayouts.jar
2. Add this code to Main module:
B4X:
#Macro: After Save, Sync Layouts, ide://run?File=%ADDITIONAL%\JsonLayouts.jar&Args=%PROJECT%&Args=%PROJECT_NAME%
#Macro: Title, JsonLayouts folder, ide://run?File=%WINDIR%\explorer.exe&Args=%PROJECT%\JsonLayouts

The tool will run whenever the project is saved, and that includes running the project if "auto save" is enabled.
A folder named JsonLayouts will be created with the json layouts (Ctrl + 1 will open it). The files will be synchronized when the layouts are updated.

Notes
- The synchronization is at the file level, which means that if both the binary layout and the json layout are updated at the same time, then the tool will show a message about a conflict and will not make any change.
- There is a notion of the layout owner which is the source of the last conversion. If the owner is deleted then the converted layout is also deleted (moved to recycle bin).
- As this tool is compatible with B4A, B4J and B4i, it is recommended to put the jar in the B4X additional folder (https://www.b4x.com/android/forum/threads/b4x-additional-libraries-folder.103165/#content) and change the "After Save" macro to (thanks @aeric ):
B4X:
#Macro: After Save, Sync Layouts, ide://run?File=%ADDITIONAL%\..\B4X\JsonLayouts.jar&Args=%PROJECT%&Args=%PROJECT_NAME%

The B4J code is attached.

v1.01 - Fixed issue with old layouts failing to convert (BalConverter v2.31).
 

Attachments

  • JsonLayouts.zip
    6.2 KB · Views: 25
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I don't think that the explanation is correct. The zero comes from somewhere else.
[I wasn't fully awake]
That zero is written by your tool in the JSON, Erel, not by the clsLayoutInspector class, which takes the key parts from the JSON created by the tool.
I did some testing.

1 (absurd) I tried writing some text in a view tag, saved it, deleted the text, saved it, and the zero wasn't "generated" (that's what the AI assumed had happened).
2 I verified that the label tag that was showing as "0" was actually empty, and it was. I thought there might be an invisible character (well, now that I think about it, I only tried it in the Designer; I should have logged it at runtime). I followed the same steps as in step 1, and now the "0" isn't "invented" anymore.

Final outcome: mystery.
 

LucaMs

Expert
Licensed User
Longtime User
1783845292436.png


I created an empty B4J UI project, added the macros to create jsonlayouts:

#Macro: After Save, Sync Layouts, ide://run?File=%ADDITIONAL%\..\B4X\JsonLayouts.jar&Args=%PROJECT%&Args=%PROJECT_NAME%
#Macro: Title, JsonLayouts folder, ide://run?File=%WINDIR%\explorer.exe&Args=%PROJECT%\JsonLayouts

and... surprise: a SQLite database (layouts.db) was generated.
I opened it and it contains something familiar, related to some test projects I created recently.

Again, the project is completely new and empty, saved on the desktop (because I'm going to throw it away; it's a test). My databases aren't on the desktop. There's a link to my B4J projects, one of which contains that database (in a rather long path).

I also discovered that the layouts.db is replicated in 11 project folders (all in the same path: C:\Users\sg\Desktop\Appoggio\
(Appoggio = Support).

1783846836105.png
 

LucaMs

Expert
Licensed User
Longtime User
The db file is created in the project folder, under JsonLayouts. I don't think that it is related to the other layout files that you are seeing. Each project has its own file.
I don't understand, Erel. Does JsonLayouts.jar create SQLite databases?

Maybe I explained it poorly.

New empty B4J UI project. Macro:

#Macro: After Save, Sync Layouts, ide://run?File=%ADDITIONAL%\..\B4X\JsonLayouts.jar&Args=%PROJECT%&Args=%PROJECT_NAME%
#Macro: Title, JsonLayouts folder, ide://run?File=%WINDIR%\explorer.exe&Args=%PROJECT%\JsonLayouts

They create the "layouts.db" file, which contains an old table of mine, from an old (well, recent) project of mine that obviously has nothing to do with the EMPTY test project.


My "little" mistake 😁, sorry :confused:, while looking at the contents of layouts.db with an external tool: I left the tab selected, which showed something completely different!
Now:
1783859878927.png
 
Last edited:
Top