How to create multiple Android apps from one code base

后端 未结 5 1804
醉梦人生
醉梦人生 2021-02-05 06:40

I have an Android code base which uses APIs with settings to get different data for several apps. All apps use the same code base but with one or two design tweaks. So how do I

5条回答
  •  悲哀的现实
    2021-02-05 06:51

    Note: This answer is basically obsolete now that one can create .aar libraries with resources. It still works, though, and there may be times when the portability of a .jar is desirable, so I'm leaving it here.

    Blumer's answer is a good one, and you should definitely look into Android's idea of library projects. However, there is another alternative. If you have a module that contains only Java code, but no resources of any kind (images, layouts, etc.), you can compile it to a .jar file separately and use the resulting .jar in multiple application projects. It works like this:

    • Create a new Java project in Eclipse (not an Android project) and move the source code of your module there.
    • If your module references any classes from the SDK, you'll need to add the Android SDK .jar to the project's classpath (Project > Properties > Java Build Path > Libraries > Add JAR).
    • When your module is ready to use, bundle up its .class files into a .jar. You can do this manually, or you can look around to figure out how to get Eclipse to do it for you.
    • Copy your module .jar file into the "libs" directory of your app's main project.
    • Add the module .jar to the project's classpath (again, Project > Properties > Java Build Path > Libraries > Add JAR).

    Now you should be able to build multiple apps using the same .jar, while maintaining only one copy of the module's source code.

    Depending on your particular situation, this may or may not work any better for you than the standard Android library mechanism. But it's worth considering as an alternative.

提交回复
热议问题