问题描述
我们正在使用 Android 库项目在我们的 Android 应用程序的不同构建(目标)之间共享核心类和资源.每个特定目标的 Android 项目参考核心库项目(在幕后,Eclipse 从引用的库项目中创建并引用一个 jar).
We're using an Android Library Project to share core classes and resources across different builds (targets) of our Android application. The Android projects for each specific target reference the Core library project (behind the scenes, Eclipse creates and references a jar from the referenced library project).
覆盖图像和 XML 布局等资源很容易.放置在目标项目中的资源文件,例如应用程序图标或 XML 布局,在构建应用程序时会自动覆盖核心库的同名资源.但是,有时需要重写一个类以启用特定于目标的行为.例如,亚马逊目标首选项屏幕不能包含指向 Google Play 应用页面的链接,需要更改亚马逊项目的首选项.xml 和首选项 Activity 类.
Overriding resources such as images and XML layouts is easy. Resource files placed in the target project, such as the app icon or an XML layout, automatically override the core library's resources with the same name when the app is built. However, sometimes a class needs to be overridden to enable target-specific behavior. For example, the Amazon target preferences screen cannot contain a link to the Google Play app page, requiring a change in the Amazon project's preferences.xml and preferences Activity class.
目标是减少目标项目之间的重复代码量,同时从 Core 库中删除尽可能多的特定于目标的代码.我们提出了几种方法来实现特定于不同目标的逻辑:
The goal is to reduce the amount of duplicate code among target projects while removing as much target-specific code from the Core library as possible. We've come up with a couple of approaches to implement logic specific to different targets:
- 在核心库类中编写特定于目标的函数,并使用 if/switch 块根据产品 SKU 选择行为.这种方法不是非常模块化,并且会使核心库代码库膨胀.
- 扩展目标项目中的特定核心类并根据需要覆盖基(核心)类函数.然后保留对 Core 库中基类对象的引用,并使用扩展类对象对其进行实例化(来自 如何覆盖 Android 库项目中的类?)
是否有其他策略可以覆盖或扩展 Android 库项目类?在 Android 应用目标之间共享和扩展公共类的最佳做法是什么?
Are there other strategies to override or extend an Android library project class? What are some of the best practices for sharing and extending common classes among Android app targets?
推荐答案
@yorkw 这不适用于最新版本的 ADT Plugin for Eclipsehttp://developer.android.com/sdk/eclipse-adt.html
@yorkw this is not true for the latest versions of ADT Plugin for Eclipsehttp://developer.android.com/sdk/eclipse-adt.html
从版本 17 更改日志
新的构建功能添加了自动设置 JAR 依赖项的功能./libs 文件夹中的任何 .jar 文件都会添加到构建配置中(类似于 Ant 构建系统的工作方式).此外,库项目所需的 .jar 文件也会自动添加到依赖于这些库项目的项目中.(更多信息)
更多信息http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
在此之前,更新覆盖 Activity from Library 项目很容易,只需排除该类即可.现在库是作为jar文件包含的,没有办法从jar依赖中排除class文件.
Before that, update overwriting of the Activity from Library project was easy, just exclude the class. Now the library is included as jar file, and there is no way to exclude class file from jar dependency.
我从库 jar 覆盖/扩展 Activity 的解决方案:
My solution to overwrete/extend Activity from library jar:
我创建了一个简单的工具类:
I created a simple util class:
public class ActivityUtil {
private static Class getActivityClass(Class clazz) {
// Check for extended activity
String extClassName = clazz.getName() + "Extended";
try {
Class extClass = Class.forName(extClassName);
return extClass;
} catch (ClassNotFoundException e) {
e.printStackTrace();
// Extended class is not found return base
return clazz;
}
}
public static Intent createIntent(Context context, Class clazz) {
Class activityClass = getActivityClass(clazz);
return new Intent(context, activityClass);
}
}
为了覆盖依赖于该库的项目库的SampleActivity"类,在同一个包中的项目中创建一个名为 SampleActivityExtended 的新类,并将新活动添加到您的 AndroidManifest.xml.
In order to overwrite a library's "SampleActivity" class it a the project which depends on that library, create a new class with the name SampleActivityExtended in the project in the same package and add the new activity to your AndroidManifest.xml.
重要提示:所有引用覆盖活动的意图都应通过 util 类以下列方式创建:
IMPORTANT: all intents referencing overwritten activities should be created through the util class in the following manner:
Intent intent = ActivityUtil.createIntent(MainActivity.this, SampleActivity.class);
...
startActivity(intent);
这篇关于最佳实践:扩展或覆盖 Android 库项目类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!