最佳实践:扩展或覆盖Android库项目类 [英] Best practice: Extending or overriding an Android library project class

查看:176
本文介绍了最佳实践:扩展或覆盖Android库项目类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Android图书馆计划分享我们的Android应用程序的不同构建(目标)的核心类和资源。针对每个特定目标的Android项目参考Core库项目(幕后,Eclipse从引用的库项目创建并引用了一个jar)。



覆盖资源(如图像和XML布局)很简单。新新旗新新新200新200新新200新200新200新200新200新新200新200新新200新200新新200新200新200新新200新新200新新200新200新新200新新200新200新新200新200新新200新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新然而,有时需要覆盖类来实现特定于目标的行为。例如,亚马逊目标偏好设置屏幕不能包含指向Google Play应用页面的链接,需要更改Amazon项目的preferences.xml和首选项活动类。



目标是减少目标项目中重复代码的数量,同时从Core库中尽可能多地删除目标特定的代码。我们提出了几种实现不同目标的逻辑的方法:


  1. 在Core库类中编写目标特定的函数并使用if / switch块来选择基于产品SKU的行为。这种方法不是非常模块化,并且会使Core Library代码库膨胀。

  2. 扩展目标项目中的特定Core类,并根据需要覆盖基本(Core)类函数。然后保留对Core库中的基类对象的引用,并使用扩展类对象(从如何覆盖Android图书馆项目中的课程?

是否有其他覆盖或扩展Android图书馆项目类的策略?在Android应用目标中共享和扩展常见类的一些最佳做法是什么?

解决方案


库项目被引用为原始项目依赖(基于源的机制),而不是作为编译的jar依赖(基于编译代码的库机制)。


@yorkw对于最新版本的ADT Plugin for Eclipse
http://developer.android.com/sdk/eclipse-adt.html



从版本17更改日志


新建功能
添加了自动设置JAR依赖关系的功能。新新200新新新新旗新新新旗新新新旗新新旗新新旗新新旗新新旗新新旗旗新新旗新新旗新新旗新新旗新新200新新旗新新200新新旗新新200新新旗新新200新新旗新新200新新旗新新200新新旗2001-此外,库项目所需的.jar文件也会自动添加到依赖于这些库项目的项目中。 (更多信息)


更多信息 http://tools.android.com/recent/dealingwithdependenciesinandroidprojects



之前,从库项目更新活动的覆盖很容易,只是排除了类。现在该库被包含为jar文件,并且没有办法从jar依赖关系中排除类文件。



编辑:



/ p>

我创建了一个简单的util类:

  public class ActivityUtil {

private static Class getActivityClass(Class clazz){

//检查扩展活动
String extClassName = clazz.getName()+Extended;
try {
Class extClass = Class.forName(extClassName);
return extClass; X-454545454545 X-4545 X-4545 X-4545 X- 20045 X-454545 X-4545 X- 20045 X-454545 X- 20045 X-
//扩展类没有找到return base
return clazz;
}
}

public static Intent createIntent(Context context,Class clazz){
Class activityClass = getActivityClass(clazz);
返回新意图(context,activityClass);
}
}

为了覆盖库的SampleActivity类,一个依赖该库的项目,在同一个包中在项目中创建一个名为SampleActivityExtended的新类,并将新的活动添加到您的AndroidManifest.xml。



重要提示:引用覆盖活动的所有内容都应通过util类以下列方式创建:

  Intent intent = ActivityUtil.createIntent MainActivity.this,SampleActivity.class); 
...
startActivity(intent);


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).

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.

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:

  1. Write the target-specific functions within Core library classes and use if/switch blocks to select behavior based on product SKU. This approach is not very modular and bloats the Core library codebase.
  2. Extend the particular Core class in a target project and override the base (Core) class functions as needed. Then keep a reference to the base-class object in the Core library and instantiate it with an extended class object (from How to override a class within an Android library project?)

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?

解决方案

Library project is referenced as a raw project dependency (source-based mechanism), not as a compiled jar dependency (compiled-code based library mechanism).

@yorkw this is not true for the latest versions of ADT Plugin for Eclipse http://developer.android.com/sdk/eclipse-adt.html

From version 17 Change log

New build features Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)

More info http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

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.

EDIT:

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);
}
}

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.

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库项目类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆