Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Changing and matching the external library's package:

Package name in 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:

compile project(path: ':LibProject')

Thank you for your help.

like image 889
Chisko Avatar asked Feb 03 '17 19:02

Chisko


1 Answers

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

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.

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:

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

like image 179
Fco P. Avatar answered Nov 03 '22 08:11

Fco P.