Android Tutorial Conditional Compilation & Build Configurations

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).

SS-2014-05-08_09.58.38.png


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:

SS-2014-05-08_10.19.51.png


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
Longtime User
Great job!
Thank you very much!

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

MaFu

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

corwin42

Expert
Licensed User
Longtime 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.
 

sorex

Expert
Licensed User
Longtime User
Thanks Erel, for implementing my feature request.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Can you use "or"? Eg:

#If A or B

#End If

Thanks -Colin.
 

DonManfred

Expert
Licensed User
Longtime 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
Longtime 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
Longtime 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)...
 
Top