集成外部纯Java库并可以访问Android类 [英] Integrating an external pure Java library and having Android classes access on it

查看:92
本文介绍了集成外部纯Java库并可以访问Android类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试获取外部Java项目时遇到了麻烦,因此我也可以在其上使用Android类.该库已经集成在Android项目中.例如:我上面有几个模型类,我想实现Parcelable,以便可以对它们进行序列化,但是没有一个Android类可用.

I'm having troubles trying to get my external Java project so I can use Android classes on it as well. The library is already integrated on the Android project. For instance: I have several model classes on it that I would want to implement Parcelable so they can be seriallized accordingly, but none of the Android classes are available on them.

澄清,我这样做只是为了尝试解决问题

Clarification I only did this in order to try to solve the issue

到目前为止,我只尝试过:

So far I've only tried:

  • 更改并匹配外部库的包:

Android中的包裹名称

com.domain.androidproject

原始的图书馆软件包

com.domain.libproject

更改为:

com.dommain.androidproject.libproject

但是到目前为止还没有运气.我将库作为Gradle外部项目vía导入:

But no luck so far. I imported the library as a Gradle external project vía:

compile project(path: ':LibProject')

谢谢您的帮助.

推荐答案

您必须定义纯Java库和android之间的绑定.您可以使用依赖注入使用类签名来注入模型,然后在应用程序内部(或在另一个项目,如插件中)定义可打包模型.或者,您也可以使用泛型实现相同的目标.请记住,由于Java库已经被编译,从技术上讲,您无法通过将其导入到android项目中来进行更改(我见过人们从依赖项重写"某些文件,然后将它们添加到整个原始路径中)愚弄类路径,但这具有很高的风险,因为您将无法与依赖项的其余代码进行交互,并且如果发生某些更改,则该操作会中断.)

You'll have to define a binding between your pure java library and android. You could use Dependency injection to inject the models using the class signature, and then define the parcelable models inside the app (or into another project, like a plugin). Or you could achieve the same using generics. keep in mind, since the java library is already compiled, technically, you can't change it by importing it into the android project (I've seen people "rewriting" some files from a dependency and then adding them with the whole original path to fool the classpath, but that's highly risky since you are not gonna be able to interact with the rest of the dependency's code and if something changes, the thing will break).

如果您有权访问纯Java的库源代码,则将其修改为使用工厂或模型提供者.如果没有,请扩展模型,添加可分割的支持,然后尝试使用那些模型代替原始模型类.

if you have access to the pure java's library sourcecode, then modify it to use factories or providers of models. If not, extend the models, add parcelable support, and attempt to use those instead of the original model classes.

示例:

让我们假设有一个模型和一些使用它的函数:

let's suppose we have a model and some functions using it:

public class myModel{

    private int id;
    private String name;

    public void setId(int id){
        this.id = id;
    }

    //more getters and setters
}

public interface myModelCreator<T>{
    public myModel create(T toModel);
    public T uncreate(myModel fromModel);
}

public static void doSomething(myModel model){
    //some library operations
}

现在,在android项目中:

Now, in the android project:

public class myAndroidModel extends myModel implements Parcelable{
    /*Implements the parcelable methods using the class accessors, or you can change the myModel members to protected.*/
}

public class myAndroidModelCreator implements myModelCreator<myAndroidModel>{

    @Override
    public myModel create(myAndroidModel toModel){
        //create the myModel using the parcelable class.
    }

    @Override    
    public myAndroidModel uncreate(myModel fromModel){
        //reverse operation.
    }

}

现在,在android项目中,您可以在任何地方使用parcelable子类,并且每次需要调用库时,都可以使用parcelables作为参数来提供创建者接口. 另一种选择是将库方法签名更改为以下内容:

Now, in the android project, you can use the parcelable subclass everywhere, and everytime you need to call the library, you can supply the creator interface using the parcelables as arguments. Another alternative would be changing the library method signatures to something like this:

public static void<T extends myModel> doSomething(T model){
    //some library operations
}

因此,您可以直接使用可打包的子类.但是根据您的层次结构,这可能是不可能的.最后,您可以尝试使用android项目中的Guice和Roboguice将依赖项注入到Java项目中.由于roboguice使用guice,因此它们有可能可以互操作,但这是一个漫长的尝试.

So you can directly consume the parcelable subclasses. But depending on your hierarchy, that may be not possible. Lastly, you could attempt to implement dependency injection into the java project using Guice and Roboguice in the android project. Since roboguice uses guice, it is possible they can interoperate, but that's a long shot.

这篇关于集成外部纯Java库并可以访问Android类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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