I have a game I have built that will have 2 versions. One "lite" and one "full". For this purpose I put the large part of the code in a library-project.
Thing is that within part of the code I have to start an Activity whose code will be in the program project开发者_JS百科 and not the library one. I found that it will not compile.
What I did to solve it was to create a dummy class for the activity within the library-project but it is probably not the right way to go.
If youre saying that some part of the library project is dependent on a class that will have one implementation in one app project and another or possibly non-existant implementation in another app project, then I suspect a simple interface would help.
Declare an interface in the library project and change all references in that project to your interface. In the app projects, provide different classes that implement that interface and vary their behavior according to whether it is lite or full.
I cant see how a dependency on an Activity would manifest itself since in most every case you reference an activity it's by the ComponentName via string.
As as small example, pretend we want to be able to inject an ad banner image view into a view group in our lite edition, but do nothing, or maybe hide the view group in our full edition.
In the library project declare an interface for AdBannerProvider:
public interface AdBannerProvider
{
public void InjectAd(ViewGroup v);
}
In Activities in the library project that maybe need an ad depending on lit or full, reference the interface as in:
private void getAdBanner(AdBannerProvider pProvider, ViewGroup pAdView)
{
pProvider.InjectAd(pAdView);
}
Then in the Lite edition, provide an implementation of AdBannerProvider that does the right things for that edition:
public class LiteAdBannerProviderImpl implements AdBannerProvider {
@Override
public void InjectAd(ViewGroup v) {
// do something useful for the lite edition, like add an imageview to the passed viewgroup
}
}
And in the full edition, another class the does the right thing for that edition:
public class FullAdBannerProviderImpl implements AdBannerProvider {
@Override
public void InjectAd(ViewGroup v) {
// do something useful for the full edition, like maybe set the viewgroup to GONE or INVISIBLE
}
}
Application project should depend on the components/resources of the library project.If an activity/service needs to be started from library then probably posting intent with the correct component set might work.
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.pkg", "com.pkg.class"));
startActivity(intent);
精彩评论