I have a project that must be exported as library.
The project has multiple packages and I want that some public methods/classes can't be accessed from the application that will host my library. Quick example:
package test.package1
import test.package2.*;
//Class1 can be accessed from external
public class Class1 {
public Class1() {}
public doSomething(){
//Class2, instead, should be only "library-internal" and not accessed from external
Class2 c2 = new Class2();
c2.doSomething();
}
}
package test.package2
//Library private class
public class Class2 {
public doSomething(){
}
}
I've found many post that was speaking about this problem:
But none of these solved my problem in a clear mode (for me) so, what I'm asking, is if what I'm doing wrong is the packages structure or if exists an elegant solution to my problem.
I think that I can quickly resolve the problem just placing all classes in the same package and declaring public only what I want that can be accessed from external, but I don't think that is a good solution and make worse the project readability.
==============
Edit 1: I try to extend my question with a more specific sample
Imagine that I have a package named "test.model" where I put all classes that will interact with a SQLite database.
Now, all classes that are in this package, MUST be accessibile within the library by all others "internal" packages.
Always in the same application I have a package named "test.ui" where I did put all fragments and activities. All these ui components NEED to access to classes present in "test.model" package, but I don't want that "test.model" package can be visible from the application that will host the library.
Activities and fragments present in "test.ui", instead, can be used from the application that is hosting the library.
Below an example of the project structure:
//I want that this package is for library "internal" use only
--- package test.model
------ Author.java
------ Book.java
//This package can be accessed from project hosting my library
--- package test.ui
----- BookListFragment.java //Can access, with a private method, to Book class
----- AuthorListFragment.java //Can access, with a private method, to Author class