javaandroideclipselibrary-project

Handling a core project and multiple derived from it in Android


I've searched around SO for this and found a few things, but I'm still not sure I fully understand, so I ask you for clarifications.

Here is what I need:

So, I have ProjectCore with activities and functionality. Project1 with a car icon and car image for splashscreen. Project2 with airplane icon and airplane image for splashscreen. Something like that. Each projects has a class with constants like'appId, appName, appServerURL"... All the web service call, data display is in Core as it's the same for all prohects, only the read is made from Constants class.

I was thinking of this approach

  1. Make ProjectCore a Library project with a package like com.domain.core and dummy images
  2. Make Project1, add reference to ProjectCore in it and with a package like com.domain.code.project1 and in resources folder, put images with same name as in core project
  3. Make Project2 on the same principle like project1

Will this approach work ?

Thanks.

Later Edit. I've tried as mentioned before. For instance in Core project I had in drawable a file called splash.png. In Project1's and Project2's drawable folder I've put spash.png file with other images. This works fine. Running the Project1 and Project2 on my phone, started each app with it's own image. So far so good.

Then, because I have different constants I need to use in my App, I went into Core library project and added:

public class C {
    public static String SomeConstant = "Project core!";
}

Here comes the problem, I need to have different constant values across Project1 and Project2. Because on Core project, the class is in com.domain.core.utils for instance... I can't add the same package in Project1 and Project2. How do I add the classes so I can update their values and be used on each project with particlar values ?

public class C {
    public static String SomeConstant = "Project 1 constant!";
}

public class C {
    public static String SomeConstant = "Project 2 constant!";
}

Thank you!


Solution

  • You want to create your functionality in a Library project and then have all of your Branded/OEM/3rdParty projects extend from this, overriding images and string resources where necessary.

    When you need to use "Constants" you should instead have a single "run once" portion of your code (such as a splash screen) load these strings from resource files:

    public static final String CONSTANT_ONE;
    
        public void onCreate() { CONSTANT_ONE = getResources().getString(R.String.CONSTANT_ONE); }
    

    EDIT

    I'm unsure on how initialising a final value on onCreate() will perform. If final doesn't work well and you're worried about changing the variable during program execution then make the variable private (so only that class can assign to it) and then create a public static String getConstantOne() function.