[英]Java: How to share common application / UI code among several applications
I have several applications that differ mostly based on resources.我有几个主要基于资源不同的应用程序。 As of now, I'm copying the code around to each application.截至目前,我正在将代码复制到每个应用程序中。 This can be problematic.这可能是有问题的。 An example, fixing a bug in one, and forgetting to update to the others.一个例子,修复一个错误,忘记更新其他的。
I don't think creating a JAR is appropriate for this situation, as these are application specific UI classes, (actually android activity classes in specific) including the actual app start-up code.我认为创建 JAR 不适合这种情况,因为这些是特定于应用程序的 UI 类(实际上是 android 活动类),包括实际的应用程序启动代码。
It may be possible to include these source files into several packages, but then I have the problem that each file specifies a specific package name on the first line.可以将这些源文件包含到几个包中,但是我遇到的问题是每个文件在第一行指定一个特定的 package 名称。
Most of the code is related to the UI and Activity processing.大部分代码与 UI 和 Activity 处理有关。 (The actual common code is already in a library). (实际的通用代码已经在库中)。 A similar question is posted here . 此处发布了一个类似的问题。 Are there any elegant solutions to this situation?这种情况有什么优雅的解决方案吗?
A jar is absolutely appropriate for this situation. jar 绝对适合这种情况。 You should split your application into layers, separating the application-specific classes from the shared code.您应该将应用程序拆分为多个层,将特定于应用程序的类与共享代码分开。
I solved this by going with Android Library projects.我通过使用 Android 库项目解决了这个问题。 (Not sure of the details, perhaps they are ultimately jars) Check out details here , specifically the section 'Setting up a Library Project'. (不确定细节,也许它们最终是罐子) 在这里查看细节,特别是“设置图书馆项目”部分。 I basically put in all my activity classes (except for the start-up one) into the library.我基本上把我所有的活动课(除了启动课)都放进了图书馆。
For true non-UI bound code, JARs, do seem to be the way to go.对于真正的非 UI 绑定代码,JARs,似乎确实是通往 go 的方式。
I agree with artbristol.我同意艺术布里斯托尔。
I also recommend to use Maven and:我还建议使用 Maven 和:
Like this you don't break applications if you do some incompatible changes.像这样,如果您进行一些不兼容的更改,您不会破坏应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.