I have a public transport app for one country and I want to create a separate app for another country. Most of the code will be sh开发者_JAVA技巧ared, but I need some classes to have different implementations for example TransitProvider.
Is it possible to share code using Android Library Project? Can I do the following?
- Have TransitProvider (that extends AbstractTransitProvider) in the library project. The class has methods left unimplemented.
- In application project > AndroidManifest.xml I have different package name than in library's manifest. I have also TransitProvider in this project, that is in the same package as the library's TransitProvider.
- When I use TP in library project code, the implementation from app. project should be used (ie application project's TP overrides library's TP).
Is it possible to share code using Android Library Project?
Yes. That is the primary purpose of a library project. If you do not need Android resources, you can also use an ordinary JAR, created in a separate project.
Can I do the following?
You cannot have the same class (in the same package) defined in two places, if I understand your proposed steps properly.
You should:
- Define
AbstractTransitProvider
in the library project or JAR - Optionally have one or more concrete implementations of
AbstractTransitProvider
, for straight-up reuse, in the library project or JAR - Apps using the library project or JAR can have their own concrete implementations of
AbstractTransitProvider
, in their own Java packages, in addition to using any concrete implementations supplied by the library project or JAR
精彩评论