简体   繁体   English

集成外部纯Java库并对其进行Android类访问

[英]Integrating an external pure Java library and having Android classes access on it

I'm having troubles trying to get my external Java project so I can use Android classes on it as well. 我在尝试获取外部Java项目时遇到了麻烦,因此我也可以在其上使用Android类。 The library is already integrated on the Android project. 该库已集成在Android项目中。 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. 例如:我有几个模型类,我想要实现Parcelable因此可以相应地进行串行化,但是没有一个Android类可用。

Clarification I only did this in order to try to solve the issue 澄清我只是为了试图解决这个问题而这样做

So far I've only tried: 到目前为止我只尝试过:

  • Changing and matching the external library's package: 更改和匹配外部库的包:

Package name in Android Android中的包名称

com.domain.androidproject

Library's package originally 图书馆的包最初

com.domain.libproject

Changed to: 变成:

com.dommain.androidproject.libproject

But no luck so far. 但到目前为止没有运气。 I imported the library as a Gradle external project vía: 我将库导入为Gradle外部项目vía:

compile project(path: ':LibProject')

Thank you for your help. 谢谢您的帮助。

You'll have to define a binding between your pure java library and android. 你必须在纯Java库和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). 您可以使用依赖注入来使用类签名注入模型,然后在应用程序内部(或插入另一个项目,如插件)中定义parcelable模型。 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库已经编译,从技术上讲,你不能通过将它导入android项目来改变它(我看到人们从一个依赖项“重写”一些文件,然后用整个原始路径添加它们欺骗类路径,但这是非常危险的,因为你不能与依赖的代码的其余部分进行交互,如果有什么变化,事情就会破坏)。

if you have access to the pure java's library sourcecode, then modify it to use factories or providers of models. 如果您可以访问纯java的库源代码,则将其修改为使用工厂或模型提供者。 If not, extend the models, add parcelable support, and attempt to use those instead of the original model classes. 如果没有,请扩展模型,添加parcelable支持,并尝试使用它们而不是原始模型类。

Example: 例:

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
}

Now, in the android project: 现在,在android项目中:

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

}

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. 现在,在android项目中,您可以在任何地方使用parcelable子类,并且每次需要调用库时,您都可以使用parcelables作为参数提供创建者接口。 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
}

So you can directly consume the parcelable subclasses. 所以你可以直接使用parcelable子类。 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. 最后,您可以尝试使用Android项目中的Guice和Roboguice在java项目中实现依赖注入。 Since roboguice uses guice, it is possible they can interoperate, but that's a long shot. 由于roboguice使用guice,它们可以互操作,但这是一个很长的镜头。

I like Fco P. 's answer, but for the sake of completness, here is an alternative answer. 我喜欢Fco P.的回答,但为了完整性,这里有一个替代答案。

Use json to serialize objects, rather than Parcelable . 使用json序列化对象,而不是Parcelable You can then put your serialized json as a string extra in intent or as string in bundles. 然后,您可以将序列化的json作为一个额外的意图字符串或捆绑包中的字符串。

Generally if you want to make use of classes in another project/library: 通常,如果您想在另一个项目/库中使用类:

File -> New -> Import Module -> Navigate to the directory of an old project/Library -> Ok 文件 - >新建 - >导入模块 - >导航到旧项目/库的目录 - >确定

Check off the modules you want to import -> OK 选中要导入的模块 - >确定

Right click the app module -> Open Module Settings -> dependencies -> + -> Module -> The new Module. 右键单击应用程序模块 - >打开模块设置 - >依赖项 - > + - >模块 - >新模块。

Your project should then be usable in whatever project you just did that for. 然后,您的项目应该可用于您刚刚执行的任何项目。

Create an android library project with packagename com.domain.libproject 使用packagename com.domain.libproject创建一个android库项目

Copy all the sources in src folder. 复制src文件夹中的所有源。

Update jar dependencies in build.gradle and after that you can make your class in the library parcelable. 更新build.gradle中的jar依赖项,之后您可以使库中的类可以进行parcelable。

Let me know if any issues. 如果有任何问题,请告诉我。

Best regds 最好的regds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM