问题描述
我在尝试获取外部 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
Library 的包原本
com.domain.libproject
改为:
com.dommain.androidproject.libproject
但到目前为止还没有运气.我通过以下方式将该库作为 Gradle 外部项目导入:
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 的库源代码,请修改它以使用工厂或模型提供者.如果没有,请扩展模型,添加 Parcelable 支持,并尝试使用它们而不是原始模型类.
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作为参数提供creator接口.另一种选择是将库方法签名更改为如下所示:
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
}
所以你可以直接使用parcelable子类.但根据您的层次结构,这可能是不可能的.最后,您可以尝试在 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 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!