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:

Bel

Member
Licensed User
Hello
Can i get current choosen BuildConfiguration in b4a?
I have #AdditionalRes: ../client1 in my project
now i need to replace client1 with current BuildConfiguration
 

luke2012

Well-Known Member
Licensed User
Longtime User
So if I have to handle different versions of my app I can compile the Apk in this way:

Pkg name = "mybrand.android.appname"

1) Trial
- Configuration name = "Trial Version"
- Conditional Symbols = "Trial", "30Days"

2) Full
- Configuration name = "Full Version"
- Conditional Symbols = "Full", "WithOnlineSupport"

So in this way I can exclude code or customize my app code based on this configuration. It is correct?
 

luke2012

Well-Known Member
Licensed User
Longtime User
If I have to implement 2 interfaces (ex. "Customer App" and "Manager App") within the same project and than I wish to create the related APKs in order to publish they on the Play Store.
I can't use the same package name with different configuration (conditional compilation) but I must compile with two different package name (ex. myapp.android.cust and myapp.android.manager) ?

Otherwise the PlayStore handle the app as one single app and not as two different apps (also if I have implemented the conditional compilation). It's Right?
 

Humberto

Active Member
Licensed User
Longtime User
You can set in "Project-Build Configuration - Create New" a new configuration with different package name
 
Top