Android Tutorial Conditional Compilation & Build Configurations

Erel

Administrator
Staff member
Licensed User
B4A v3.80 adds support for conditional compilation.

From Wikipedia:
In computer programming, conditional compilation is compilation implementing methods which allow the compiler to produce differences in the executable produced controlled by parameters that are provided during compilation.
Conditional compilation allows you to exclude parts of the code based on the selected build configuration. You can use it to create several applications from the same project. For example you can create one application with ads and another application without ads.

Build Configurations

The build configurations dialog is available under Project -> Build Configurations (Ctrl + B).



This dialog allows you to edit or add new configurations and to choose the current active configuration.
A build configuration is made of a package name and a set of conditional symbols.
The package name replaces the previously global package field. This means that you can produce APKs with different package names from the same project. Note that multiple configurations can share the same package name.

The conditional symbols define the active compiler symbols. This allows you to exclude parts of the code based on the chosen build configuration.
You can set multiple symbols separated with commas.

There are several built-in symbols:
- B4A: B4A, DEBUG and RELEASE. Either DEBUG or RELEASE will be active based on the deployment mode.
- B4J: B4J, DEBUG, RELEASE, UI or NON_UI (based on the app type)
- B4i: B4I, DEBUG, RELEASE

Code Exclusion

With the conditional compilation feature you can exclude any code you like from the code editor, manifest editor and designer script (any text can be excluded, including complete subs and attributes).
Excluded code will not be parsed and will be effectively removed before it reaches the compiler.
Note that the manifest editor and designer script will not visualize the excluded code. However it still works properly.

The code exclusion syntax:



B4X:
#If [symbol]
...
#End If
Each build configuration holds a set of symbols. Multiple configurations can share all or some of the symbols. This makes it possible to include or exclude code in several different configurations.

Starting from B4A v5, B4J v3 and B4i v2 more complex expressions are supported.
For example:
B4X:
#If FULL and Not(WITH_ADS)
'Do something
#Else If WITH_ADS OR (TRIAL And IN_APP)
'...
#Else
'...
#End If
 
Last edited:

CARTHO

Member
Licensed User
Great job!
Thank you very much!

B4A is the easiest and best way to make apps fast and to make money!
 

jmon

Well-Known Member
Licensed User
That's an excellent new feature! Thanks a lot!

Does the conditional build works with the manifest editor and the designer?
 

MaFu

Well-Known Member
Licensed User
Brilliant :D

Works the reverse check also?
#If Not FULL
#End If

and this?
#If FULL
#Else
#End If

ok, i see, no #Else. But in this case the NOT condition should be implemented. It reduces the amount of needed symbols.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
But in this case the NOT condition should be implemented. It reduces the amount of needed symbols.
Currently NOT is not implemented. It might be added in the future.
 

corwin42

Expert
Licensed User
This feature arrives just in time. It was getting a mess handling all differences between my free and payed app. Now this should make everything much better.
 

DonManfred

Expert
Licensed User
Can you use "or"? Eg:
#If A or B
#End If
Maybe i misunderstood the explanation but i think you can create a build-config with A and B as conditional symbols...

The conditional symbols define the active compiler symbols. This allows you to exclude parts of the code based on the chosen build configuration.
You can set multiple symbols separated with commas.
 

Computersmith64

Well-Known Member
Licensed User
Maybe i misunderstood the explanation but i think you can create a build-config with A and B as conditional symbols...
Yeah - I guess I can do it that way. I was thinking of something different, but I think that will work. I'll give it a try.

Thanks -Colin.
 

Computersmith64

Well-Known Member
Licensed User
Actually, maybe my brain isn't working properly today - but that won't work for me. I have 3 different releases of one of my apps - a Free, a Paid & an Amazon. The Free & Amazon are very similar, except that the Amazon doesn't allow show the "Rate Me" option, because it's against Amazon's T&C's to allow an app distributed by them to take the user to Play Store.

So in most cases, I want my Free build configuration to be the same for the Free & Amazon apps, however in one case I want them to be different. So how do I do this? (It's just not coming to me)...
 

DonManfred

Expert
Licensed User
I think you should create a new thread for this question as this thread is the tutorial-thread (this include my off-topics as well ;))
 
Top