I have several android applications where approx 80% of the code is the same so I put it in a package "library". Eclipse seems to call this a library eventhough its a package and not like a class that I need to instantiate. The unique bits (ie. lookup tables) to setup things are separate from the package. This is working well and I can call package functions. Is there a way to go backwards. In each of my applications I want to have a function (ie foo() ) that does something different in each application. I'd like to be able to call foo() from a function in the package.
1 Answers
Consider everything as a library, even your main applications.
Then I think your situation is currently like this:
common-libapp1, requirescommon-libapp2, requirescommon-libapp3, requirescommon-lib
And you now want common-lib to refer to code in app1/2/3? That isn't possible, but maybe you can accomplish that using dependency injection.
Dependency Injection
Make an interface in common-lib that defines the actions you need to invoke. Then create classes implementing that interface in app1/2/3. Finally, make app1/2/3 pass an instance of their version of that interface to common-lib, so it can use it.
In common-lib:
interface Named {
void getName();
}
class MasterOfCeremonies {
static void introduce(Named guest) {
System.out.println("Please welcome " + guest.getName());
}
}
In app1:
public StudentJohn implements Named {
void getName() {
return "John";
}
}
In app2:
public StudentMary implements Named {
void getName() {
return "Mary";
}
}
This way, app1 can then invoke MasterOfCeremonies.introduce(new StudentJohn()), and app1 can then invoke MasterOfCeremonies.introduce(new StudentMary()), and common-lib will do different things for each of them.
Additional Note
I don't know your exact situation, but another thing that may be useful is making another even "more common" library, like this:
other-libcommon-lib, requiresother-libapp1, requirescommon-libapp2, requirescommon-libapp3, requirescommon-lib