Android Tutorial Conditional Compilation & Build Configurations

Discussion in 'Tutorials & Examples' started by Erel, May 8, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    B4A v3.80 adds support for conditional compilation.

    From Wikipedia:
    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:


    #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:
    #If FULL and Not(WITH_ADS)
    'Do something
    #End If
    Last edited: Jun 7, 2015
  2. alexandercolbasic

    alexandercolbasic Member Licensed User

    Excelente ... Muchas gracias :)

    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!
  4. jmon

    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?
  5. Erel

    Erel Administrator Staff Member Licensed User

    Yes, see the bold sentence on the first post ;)
    jmon likes this.
  6. jmon

    jmon Well-Known Member Licensed User

    :) Sorry, I missed it, I was too excited!
  7. shashkiranr

    shashkiranr Active Member Licensed User

    Brilliant !! :)
  8. MaFu

    MaFu Well-Known Member Licensed User

    Brilliant :D

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

    and this?
    #If FULL
    #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: May 8, 2014
    cimperia likes this.
  9. JordiCP

    JordiCP Well-Known Member Licensed User

    Really useful, thanks!
  10. Erel

    Erel Administrator Staff Member Licensed User

    Currently NOT is not implemented. It might be added in the future.
  11. corwin42

    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.
  12. ilan

    ilan Expert Licensed User

    excelent news :) good job !
  13. MaxApps

    MaxApps Active Member Licensed User

    This is one of the best features, I have seen.
    THANKS :)
  14. corradotisi

    corradotisi Member Licensed User

  15. sorex

    sorex Expert Licensed User

    Thanks Erel, for implementing my feature request.
  16. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    Hi Erel,

    Can you use "or"? Eg:

    #If A or B

    #End If

    Thanks -Colin.
  17. DonManfred

    DonManfred Expert Licensed User

    Maybe i misunderstood the explanation but i think you can create a build-config with A and B as conditional symbols...

  18. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    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.
  19. Computersmith64

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

    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 ;))
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice