What to do with multiple Android apps that share about 90% of source code? – Education Career Blog

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 shared, 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?

  1. Have TransitProvider (that extends
    AbstractTransitProvider) in the
    library project. The class has methods left unimplemented.
  2. 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.
  3. 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:

  1. Define AbstractTransitProvider in the library project or JAR
  2. Optionally have one or more concrete implementations of AbstractTransitProvider, for straight-up reuse, in the library project or JAR
  3. 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

Leave a Comment