简体   繁体   English

如何在Android库中创建图层而不将内部类暴露给库的使用者

[英]How to create layers in an Android library without exposing internal classes to consumer of the library

I'm creating an android library and wanted to organize with layers it something like this. 我正在创建一个android库,并希望将其分层组织。

PublicClassExposedToLibraryConsumer.java
logic.PublicFooLogicInterface1.java
logic.PackagePrivateFooLogicClass1.java
logic.PublicFooLogicInterface2.java
logic.PackagePrivateFooLogicClass2.java
domain.PublicFooDomainInterface1.java
domain.PackagePrivateFooDomainClass1.java
domain.PublicFooDomainInterface2.java
domain.PackagePrivateFooDomainClass2.java
repository.PublicFooRepoInterface1.java
repository.PackagePrivateFooRepoClass1.java
repository.PublicFooRepoInterface2.java
repository.PackagePrivateFooRepoClass2.java

1) I want a number of layers and I want to limit interaction between those layers by using interfaces. 1)我想要多个层,并且想通过使用接口来限制这些层之间的交互。

2) I want to only expose PublicClassExposedToLibraryConsumer.java to the consumer of the library. 2)我只想向库的使用者公开PublicClassExposedToLibraryConsumer.java。 They should not be able to access the other classes and interfaces. 他们应该不能访问其他类和接口。

Is this possible? 这可能吗? From what I've read in order to make something accessible to something consuming the library it needs to be public and to hide something from the consumer of the library it needs to be not public. 根据我的阅读,为了使使用图书馆的东西可以访问某些东西,它需要是公共的,并且对图书馆的使用者隐藏一些东西,它需要是不公开的。 By my reading this means that you can't separate layers without exposing something and you can't hide internal classes without being forced to use a completely flat architecture. 通过我的阅读,这意味着您不能在不暴露某些东西的情况下分离层,并且在不被迫使用完全平坦的体系结构的情况下也无法隐藏内部类。 I find this very hard to believe, I have to be missing something. 我觉得很难相信,我必须丢失一些东西。

You can try with annotations providing specific scope for your desired file to restrict to end-user of your library . 您可以尝试使用注释来提供所需文件的特定范围,以将限制为库的最终用户 Best way to do this in Android is using @RestrictTo support library annotation on class level . Android中执行此操作的最佳方法是在类级别使用@RestrictTo支持库注释。

Note : For Fields and Methods of particular entity can be scoped with access-modifiers like private, protected or package-protected etc. *(Just ignore if you already know that) 注意:对于特定实体的字段和方法 ,可以使用访问修饰符来限定范围,例如私有,受保护或受包保护等。 *(如果已经知道,请忽略)

@RestrictTo : Denotes that the annotated element should only be accessed from within a specific scope (as defined by RestrictTo.Scope ). @RestrictTo :表示带注释的元素只能在特定范围内访问(由RestrictTo.Scope定义)。

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

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