Android Tutorial Change Icon and Assets Files based on the Build Configuration

This tutorial explains how the new conditional compilation feature can be used to change the app icon and the assets files based on the chosen build configuration.
See this tutorial for more information about build configurations: http://www.b4x.com/android/forum/threads/40746/#content

We have an app with two build configurations: Trial and Full. We want to change the icon and the included files based on the chosen configuration.

The first step is to create two folders in the project folder, fullonly and trialonly:

SS-2014-05-11_11.13.10.png


We use Robocopy, a windows built-in utility (replaces xcopy), to copy the correct files to a subfolder named extra. Robocopy mirrors the source folder. This means that files in the target folder that don't belong to the source folder will be deleted.

The next step is to copy the icon file (which is in the main project folder) to the project icon file.

This is the complete code:
B4X:
#IgnoreWarnings: 17

#If FULL
   #CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\fullonly ..\files\extra /MIR
   #CustomBuildAction: 1, c:\windows\system32\cmd.exe, /c copy ..\full_icon.png res\drawable\icon.png
#End If

#If TRIAL
   #CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\trialonly ..\files\extra /MIR
   #CustomBuildAction: 1, c:\windows\system32\cmd.exe, /c copy ..\trial_icon.png res\drawable\icon.png
#End If

Sub Activity_Create(FirstTime As Boolean)
   Activity.SetBackgroundImage(LoadBitmap(File.DirAssets, "extra/logo.png"))
End Sub

Notes

- The file names and the name of the "extra" folder must be lower-case.
- The files do not need to be read-only.
- #IgnoreWarnings 17 removes the warnings related to the usage of files not added to the project.
- In debug mode the icon file will only be updated after a "full installation". The files will be updated whenever needed.
- Note how the file is accessed. The subfolder must be part of the file, not File.DirAssets.
 
Last edited:

MoraviaVenus

Member
Licensed User
Longtime User
Tried directly from C:/, but, unfortuantely, again without success...

robocopy3.png


Under Windows 7 works fine. Don't I need to install something, or play somehow with access rights...?

I am desperate :(
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi Erel,

after posting previous screenshot I tested it also without commas, but with same result: poor message Access Denied...
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi community,

I did not manage to solve the problem, so I offer workaround for those with similar issue.

I have two versions of app named AreHeaven (AH) and ArtAngels (AA). In the main project folder I created two folders: condcompil_ah and condcompil_aa, where I put files related to particular version. Inside of each folder I created two more subfolders named extra and drawable. To the extra folder I put files, which are copied to files\extra by compilation, to the drawable folder I put files, which are copied to \objects\res\drawable. Files in source folders condcompil_aa\drawable and condcompil_ah\drawable are all read-only.

folders_min.png


In the Main module of project I put following commands at the beginning, which "mirror" files from \condcompil_aa\extra to \files\extra and from \condcompil_aa\drawable to \objects\res\drawable, separately for AH and AA:

#If AH
#CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\condcompil_ah\extra ..\files\extra /MIR
#CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\condcompil_ah\drawable ..\objects\res\drawable /MIR
#End If

#If AA
#CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\condcompil_aa\extra ..\files\extra /MIR
#CustomBuildAction: 1, c:\windows\system32\robocopy.exe, ..\condcompil_aa\drawable ..\objects\res\drawable /MIR
#End If

Works smoothly.
 
Last edited:

yo3ggx

Active Member
Licensed User
Longtime User
Hi,

It is possible that you want to write over a read only file (icon.png)?
Just make icon.png writable and write it in the same way for the other configuration.

If still doesn't work, just copy cmd.exe in the application source folder.
It works for me.

Dan
 
Top