Conditional Compile?

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
Is there a facility for conditional compilation in B4A?

I have Free and Plus versions of an application. The two versions are identical – the only difference being a Boolean variable that I set to true or false.

I would also like to use a different application icon (version code/string, name, package name) for each version, but I believe this is set at compile time.

In C or Delphi this could be handled with conditional compilation. Does B4A have anything similar?

To change the icon conditionally it looks like the statement from the manifest, android:icon="@drawable/icon", would ultimately need to be modified depending on the conditional statement.

Thanks,
Barry.
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
Currently no.

Note that one possible solution is to use "compile to library" to build your app as a library and then include it from two different projects.
Could this potentially be done with CustomBuildActions?
You could run a sed/find-and-replace over the B4A file to change the value of a variable.
However I dont think Java code is very optimised, so a single variable change would be easy to circumvent.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Could this potentially be done with CustomBuildActions?
You could run a sed/find-and-replace over the B4A file to change the value of a variable.

Thanks,
I will have to look more closely at CustomBuildActions.

However I dont think Java code is very optimised, so a single variable change would be easy to circumvent.

This is a very good point. I test my Boolean value and execute/don't execute code, but the actual code becomes part of the compiled module – just not executed.

It would be better to include or completely not include the code based on the whether it was compiled for the Free or Plus version.

Barry.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It would be better to include or completely not include the code based on the whether it was compiled for the Free or Plus version.
Agreed. Maybe if you code the extra functionality in a code module (or use the code module just to indirectly do a callsub), and then remove that module from the .B4A at compile time you could exclude the code.
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Hello,
Is there a facility for conditional compilation in B4A?

I have Free and Plus versions of an application. The two versions are identical – the only difference being a Boolean variable that I set to true or false.

I would also like to use a different application icon (version code/string, name, package name) for each version, but I believe this is set at compile time.

In C or Delphi this could be handled with conditional compilation. Does B4A have anything similar?

To change the icon conditionally it looks like the statement from the manifest, android:icon="@drawable/icon", would ultimately need to be modified depending on the conditional statement.

Thanks,
Barry.

Hi,

do you mean similar so? http://www.b4x.com/forum/bugs-wishlist/28881-wish-compiler-instructions.html#post167517
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User

Yes.

And as thedesolatesoul pointed out earlier having the code be physically included or not included is important.

Delphi (a development environment for Windows applications) has a feature called "compiler directives"

B4X:
{$DEFINE DEBUG}

{$IFDEF DEBUG}
  Writeln('Debug is on.');  // this code is included
{$ELSE}
  Writeln('Debug is off.');  // this code is not included
{$ENDIF}

{$UNDEF DEBUG}

you can define a symbol and then include or not include sections of code based on the symbol. The key is that it's done at compile time, not runtime, so that the excluded code does not even appear in the output (exe or apk).

I think this would be useful in B4A if one wants to produce multiple versions based on nearly the same code – I'm sure there are other reasons also.

B4A now includes the "Project Attributes" and "Activity Attributes" sections. I think these areas should also be selectable within the context of the compiler directives.

I would add the PackageName, icon file, and being able to specify the base apk output file name to these Attributes.

Barry.
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Hi, here may be a little solution for you:
in your code place things like this:
B4X:
'***DEBUG***
'Log("something
'***ENDDEBUG
And then write a little external tool or script (for notepad++ for example) that removes or places the comment "'" in the lines between. I made on in visual basic that scanns all the files in one directory. So I simply close B4A, run the tool and reload the programm in B4A with or without comments.
You can add as many different identifiers as you like:
'***PROVERSION***
'***LITEVERSION***
And in the tool set or unset the comments.
 
Upvote 0
Top