Android:具有相同软件包的2个Aar库 [英] Android: 2 aar libraries with the same package

查看:513
本文介绍了Android:具有相同软件包的2个Aar库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:在以下链接中发布了基于此讨论的后续问题.

A follow-up question based on this discussion was published in the following link.

我有两个android aar库项目:使用ClassA的LibA和使用ClassB的LibB.两个库具有相同的基本软件包.这两个库都使用名为BaseClass的相同类,当前分别位于程序包名称"common"中的每个库中. BaseClass包含一个名为baseMethod的方法.

I have two android aar library projects: LibA using ClassA, and LibB using ClassB. Both libs have the same base package. both libs use the same class named BaseClass, currently resides separately within each lib in package name 'common'. BaseClass contains one method named baseMethod.

这将使用具有相同名称和不同实现的类创建两个库.

这是类的样子:

ClassA:

package mybasepackage.a;

import mybasepackage.common.BaseClass;

public class ClassA {

    BaseClass baseClass;

    public ClassA() {
        this.baseClass= new BaseClass();
    }

    public String myPublicMethod(){
        return this.baseClass.baseMethod();
    }
}

ClassB:

package mybasepackage.b;

import mybasepackage.common.BaseClass;

public class ClassB {

    BaseClass baseClass;

    public ClassB() {
        this.baseClass = new BaseClass();
    }

    public String myPublicMethod(){
        return this.baseClass.baseMethod();
    }
}

LibA中的BaseClass:

package mybasepackage.common;

 public class BaseClass{

   public String baseMethod() {
        return "Called from ClassA";
    }
}

LibB中的BaseClass:

package mybasepackage.common;

 public class BaseClass{

   public String baseMethod() {
        return "Called from ClassB";
    }
}

当我尝试在同一个应用程序中编译两个库时,会抛出重复的类error: "Program type already present: mybasepackage.common.BaseClass",这是因为编译器不知道要编译哪个BaseClass,因为它驻留在两个库中.

When I try to compile both libs in the same app, it throws a duplicated class error: "Program type already present: mybasepackage.common.BaseClass", this happens because the compiler cannot know which BaseClass to compile since it resides within both libs.

我的目标是允许两个aar库在同一应用程序中成功编译,同时为BaseClass提供不同的实现.更正式地讲,LibA和LibB应该在同一应用程序中进行编译,例如:

My goal is to allow both aar libs to compile successfully within the same app, while providing different implementations for the BaseClass. More formally, LibA and LibB should compile in the same application such as:

呼叫new ClassA().baseMethod()将返回从ClassA呼叫".

Calling new ClassA().baseMethod() will return "Called from ClassA".

呼叫new ClassB().baseMethod()将返回从ClassB呼叫".

Calling new ClassB().baseMethod() will return "Called from ClassB".

前提条件:我无法在其中一个库中更改基本程序包名称,因为它本质上会造成不必要的BaseClass重复.

Pre condition: I cannot change the base package name in one of the libs because it essentially creates an unwanted duplication of BaseClass.

注意:我知道通过aar方法可能无法做到这一点.如果确实如此,我将考虑其他部署架构,只要我能够使用问题的描述,使用相同的通用类使用不同的实现来编译这些库.

NOTE: I'm aware this may not be possible via the aar approach. If that is truly the case, I'm willing to consider other deployment architectures as long as I'll be able to compile these libs with the same common class using different implementations, as described in the question.

推荐答案

鉴于您的先决条件,您不能以此方式进行操作.您不能在Java中使用相同的程序包名称使用2个不同的库,这是引发错误(而不是类的名称)的主要问题.

Given your precondition you just can't do that in this way. You cannot have 2 different libraries in java with the same package name, which is the main problem that throws your error (and not the name of the classes).

您可以做的,可能的话,也许最好的处理方法是将两个库合并为一个,然后在其中添加两个子包,然后将它们导入:

What you can do and maybe if possible is the best way to handle with that is to merge the two libraries into just one and add two subpackages inside and then just import them:

import mybasepackage.common.a_name.BaseClass; // class A
import mybasepackage.common.b_name.BaseClass; // class B

这将防止重复错误,因为它们的名称相同但来自不同的程序包.

This will prevent the duplication error because they just have the same name but from different packages.

如果这种方法不符合您的期望,另一个想法是通过实现另一个抽象层来更改体系结构,在该抽象层中您将BaseClass定义为抽象方法:

Another idea if this way doesn't fit your expectation is to change the architecture by implementing another abstraction layer in which you define your BaseClass as an abstract method:

package mybasepackage.common;

 public class abstract BaseClass{

   public String myPublicMethod();
 }

,然后只需在ClassAClassB中实现该方法:

and then you just implement the method inside ClassA and ClassB:

public class ClassA implements BaseClass{

    public ClassA() {
        super();
    }

    @Override
    public String myPublicMethod(){
        // logic for A
    }
}

请注意,以上对类A的实现只是一个存根,不应按原样工作.适应您的需求.

NB note that the above implementation of class A is just a stub and it is not supposed to work as it is. Adapt to your need.

在任何情况下,您都不能拥有两个具有相同类名的软件包.

In any case by the way you can't have two packages with same classes name.

这篇关于Android:具有相同软件包的2个Aar库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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