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:

MaFu

Well-Known Member
Licensed User
Longtime User
No, the 1 defines the type of the CustomBuildAction.
At the moment, five Actions are available:
1 - Before the compiler cleans the objects folder (it happens after the code is parsed).
2 - Before R.java file is generated.
3 - Before the package is signed (the APK file at this point is: bin\temp.ap_).
4 - Before the APK is installed.
5 - After the APK is installed.​

Therefore, you should CustomBuildAction 4 or 5 for your requirement.
 

avacondios

Active Member
Licensed User
Longtime User
Hi Erel,

What kind of code we can write for before and after APK is installed on device ? Where this code is going to execute ?
 

avacondios

Active Member
Licensed User
Longtime User
Can we write a code that it is going to execute after on installation of apk on android device and before to run the app ? ( I mean, the code to execute on device)
 

DavideV

Active Member
Licensed User
Longtime User
I found this way to change only the app icon depending on build configuration.

supposing you have 2 different build configurations with 2 different icons you can try this way:

1)choose the icon for the first configuration using the usual way in the ide (if you have already done skip this step).
2)choose a second icon, name it icon2 but don't add with the ide, copy it manually into
your project folder>Objects>res>drawable and make sure it's set as read only.
If you have other drawable like hdpi,xhdpi... copy icon2 into these folders too and set them as read only.
3)In the ide create the 2 build configurations, for example app1 and app2.
4)In the ide open the manifest and modify it as:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.

'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136

AddManifestText(

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17"/>

<supports-screens android:xlargeScreens="true"
    android:largeScreens="true"
    android:normalScreens="true"
   android:smallScreens="true"
   android:anyDensity="true"/>)


#if app1   

    SetApplicationAttribute(android:icon, "@drawable/icon")

#end if



#if app2   

    SetApplicationAttribute(android:icon, "@drawable/icon2")

#end if


SetApplicationAttribute(android:label, "$LABEL$")

'End of default text.

compiling for app1 will create your app with 'icon'
compiling for app2 will create your app with 'icon2'

Hope it's useful for someone :)

DavideV
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings, all, and thank you in advance for answering my question.

Thanks to Erel for the first post in this thread and thanks, cb56, for the post here related to App Label.

I am struggling to combine the two concepts: two builds Trial and Full along with different app labels for each TRIAL and FULL ApplicationLabel.

QUESTION:
What do I have to add to this code from the first post by Erel to have different application labels for Trial and Full?

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

Best regards.

Sandy
 

DavideV

Active Member
Licensed User
Longtime User
Greetings, all, and thank you in advance for answering my question.

Thanks to Erel for the first post in this thread and thanks, cb56, for the post here related to App Label.

I am struggling to combine the two concepts: two builds Trial and Full along with different app labels for each TRIAL and FULL ApplicationLabel.

QUESTION:
What do I have to add to this code from the first post by Erel to have different application labels for Trial and Full?

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

Best regards.

Sandy


Do you mean this?

B4X:
#Region  Project Attributes

    #If iracer
          'label for app name 1
        #ApplicationLabel: I-Racer advanced
    #End If
    #If blueremote
          'label for app name 2
        #ApplicationLabel: BlueRemote
    #End If
   
    'VersionCode need for market, new releases must be a number higher than the current
    #VersionCode: 3000   
    #VersionName: 3.00M
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False   
   
    #If withads
        #AdditionalRes: C:\Android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    #End If

#End Region

Bye
DavideV
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, David.

Gee, you've got to love this community! David's response posted 10 minutes after I posted the question!

Best regards.

Sandy
 

DavideV

Active Member
Licensed User
Longtime User
Thank you, David.

Gee, you've got to love this community! David's response posted 10 minutes after I posted the question!

Best regards.

Sandy

Yes Sandy, i love.
Most of the times i find what i need here, so i like to help too... ;)
Bye
DavideV
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi all,

I am using CustomBuildAction for a long time and it worked smoothly, until I upgraded my end device to Windows 8.1.

When I tried to compile and run my code with CustomBuildAction commands, I got error:
B4A version 4.30
Parsing code. 0.11
Running custom action. 0.03
Running custom action. Error
Access is denied.


When I comment out CustomBuildAction commands, it works OK.

Could you please recommend me, what should I change to continue using CustomBuildAction also under Win 8.1?

My commands are very similar to those from Erel's first post in this thread.

Thank you.
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi Erel,

unfortunately, it did not help. I run B4A as an admin, configured all paths and license and private key, but, still the same issue.

Any other idea?

Thank you.
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi Erel, I tried it, the result is attached.

robocopy.png

I am trying to copy the app icon (icon_free.png) from the root folder of my app.

Please, any suggestions?
 

MoraviaVenus

Member
Licensed User
Longtime User
Hi Erel, I tried it, file has been found but somehow I face "Access denied" despite the fact that I run cmd as administrator...

robocopy2.png


Any idea why and how to make it work?
 
Top