Cloning application sample

The HelloClonedWorld example

NeoMAD comes with an example application demonstrating this functionality : HelloClonedWorld.

The code is based on the HelloWorld project with additions allowing different applications using conditional coding to be produced.

The URS file

<urs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://www.neomades.com/XSD/3.0/urs.xsd">
   <parameters>
      <mainclassname>HelloWorld</mainclassname>
      <applicationname>${APP_NAME}</applicationname>
      <vendor>Neomades</vendor>
      <description>HelloClonedWord application example.</description>
      <packagename>com.neomades.helloworld</packagename>
      <applicationidentifier>com.neomades.${APP_ID}</applicationidentifier>
      <version>1.0.0</version>
      <srcpath>src</srcpath>
      <outputpath>out</outputpath>
   </parameters>
   <binaryname filename="${BINARY_NAME}" />
</urs>

Differences with the HelloWorld project are the addition of constants that will be computed according to the arguments provided to the command line (see ???).

HelloWorld and HelloWorldScreen classes

These classes are almost the same as those of the HelloWorld project except for HelloWorldScreen where we set the screen background color. The value of the background color will be computed in the constants file.

// set the background color of the layout
layout.setBackgroundColor(
             Color.rgb(Constants.MAIN_SCREEN_BACKGROUND_COLOR));

The Constants class

In this class we will compute all values allowing us to build different applications.

We first define a constant that determines the background color of the application:

/* Application background color **/
public static int MAIN_SCREEN_BACKGROUND_COLOR;

Then we define the constants used in the URS file:

/* Application specific resources folder **/
public static String RES_FOLDER;

/* Application identifier **/
public static String APP_ID;

/* Application name **/
public static String APP_NAME;

/* Application binary name **/
public static String BINARY_NAME;

Then we define boolean constants that will determine the application’s identity and we compute the previously declared variables:

/* When specified with -d option, sets the
 * application "red" or "green Default is "blue" **/
public static boolean RED = false;
public static boolean GREEN = false;

/* The static block allows us to compute
 * values of static variables **/
static {
   if (GREEN){
      MAIN_SCREEN_BACKGROUND_COLOR = 0x3c7b5c;
      RES_FOLDER = "green";
      APP_ID = "hellogreenworld";
      APP_NAME = "HelloGreenWorld";

   } else if (RED) {
      MAIN_SCREEN_BACKGROUND_COLOR = 0xb90000;
      RES_FOLDER = "red";
      APP_ID = "helloredworld";
      APP_NAME = "HelloRedWorld";

   } else {
      MAIN_SCREEN_BACKGROUND_COLOR = 0x146da7;
      RES_FOLDER = "blue";
      APP_ID = "helloblueworld";
      APP_NAME = "HelloBlueWorld";
   }

   /* The default value for BINARY_NAME is computed with the
    * given template : $MAINCLASSNAME_$TARGET_$VERSION_$LANGUAGE
    * Let’s put the APP_NAME in it to distinguish our applications
    * This name must be computed after APP_NAME has been computed
    **/
   BINARY_NAME = APP_NAME + "_$TARGET_$VERSION_$LANGUAGE";
}

Building the project

By using the correct build parameters, we are now able to produce different applications sharing the same source code but with different resources.

For instance, for the Android target, the following build commands:

neomad -t ANDROID -l 1 HelloClonedWorld.urs
neomad -t ANDROID -l 1 -d RED=true HelloClonedWorld.urs
neomad -t ANDROID -l 1 -d GREEN=true HelloClonedWorld.urs

will produce the following binaries:

HelloBlueWorld_ANDROID_1.0.0_en.apk
HelloRedWorld_ANDROID_1.0.0_en.apk
HelloGreenWorld_ANDROID_1.0.0_en.apk

Conclusion

With this sample application we demonstrate the power and efficiency of conditional coding with the use of the applicationidentifier tag.

By giving different versions of the application a different name and package name, we make sure that those applications will be identified by all platform systems as different and can therefore be installed on the same device without any conflict.

In this example, we only varied the project resources, but, according to your needs, you can also change classes, methods or whatever else you want.