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

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

问题描述

我们正在使用安卓图书馆计划跨共享核心类和资源不同的构建我们的Andr​​oid应用程序(目标)。 Android的项目,每一个具体的目标<一href="http://developer.android.com/guide/developing/projects/projects-eclipse.html#ReferencingLibraryProject">reference核心库项目(幕后,Eclipse将创建并从引用的库项目引用的JAR)。

重写资源诸如图像和XML布局是容易的。放置资源文件的目标项目,如应用程序图标或XML布局,自动覆盖核心库的资源具有相同的名称时,该应用程序是内置。但是,有时一类需要重写,使目标的特定行为。例如,亚马逊的目标preferences屏幕不能包含一个链接到谷歌Play应用页面,需要在亚马逊项目的preferences.xml和preferences Activity类的变化。

的目标是减少靶的项目之间的重复code中的量,而从核心库尽可能除去尽可能多的靶标特异性code。我们已经走过了几个途径来实现逻辑具体到不同的目标:

  1. 写入核心类库中的目标,具体功能和使用,如果/开关模块,选择基于产品SKU的行为。这种方法是不是很模块化和涨大核心库codeBase的。
  2. 扩展特定核心类的目标项目,并根据需要覆盖基本(核心)类函数。然后保持引用基类对象中的核心库和扩展的类对象实例化它(从<一个href="http://stackoverflow.com/questions/6564133/android-library-project-how-to-overwrite-a-class">Android库项目 - 如何覆盖类)

还有没有其他的策略来覆盖或扩展一个Android库项目类?有哪些针对Android应用程序的目标之间共享和扩展通用类的最佳实践?

解决方案
  

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

@yorkw这是不正确的ADT插件的Eclipse的最新版本 http://developer.android.com/sdk/eclipse-adt.html

从版本17更改日志

  

新版本的功能   新增功能自动安装JAR依赖。在/ libs文件夹中所有.jar文件添加到构建配置(类似于Ant构建系统的工作原理)。此外,需要的库项目.jar文件也会被自动添加到依赖这些库项目的项目。 (详细信息)

更多信息 http://tool​​s.android.com/recent/dealingwithdependenciesinandroidprojects

在此之前,从库项目活动的更新覆盖很简单,只是排除类。现在该库包括作为jar文件,并且没有办法从罐子依赖性排除的类文件。

编辑:

我的解决方案,以overwrete /从库jar延长活动时间:

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

 公共类ActivityUtil {

私有静态类getActivityClass(类clazz所){

    //检查扩展活动
    字符串extClassName = clazz.getName()+扩展;
    尝试 {
        类extClass =的Class.forName(extClassName);
        返回extClass;
    }赶上(ClassNotFoundException异常E){
        e.printStackTrace();
        //扩展类未找到返回基地
        返回clazz所;
    }
}

公共静态意图createIntent(上下文的背景下,类clazz所){
    类activityClass = getActivityClass(clazz所);
    返回新意图(背景下,activityClass);
}
}
 

为了覆盖图书馆的SampleActivity级是一个依赖于该库中的项目,创建一个新的类SampleActivityExtended项目在同一个包的名称,添加新的活动,你的Andr​​oidManifest.xml。

重要提示:引用覆盖活动所有的意图,应通过以下方式使用util类来创建的:

 意向意图= ActivityUtil.createIntent(MainActivity.this,SampleActivity.class);
...
startActivity(意向);
 

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 Android library project - How to overwrite a class?)

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天全站免登陆