简体   繁体   中英

Why are some classes not public in the Android Support Library?

Why are some of the classes included in Android's Libraries not public? To me this makes no real sense and basically just introduces a barrier to developing temporary fixes to some of the bug-riddled widgets that have recently been released.

Specifically I'm talking about the TextInputLayout in the Android Design Support Library . For example if I want to make a custom widget which overrides some of the code in the constructor I can't do that because CollapsingTextHelper is not a public class. I would have to download the source code and copy the relevant classes to my project, but there isn't really anything that is preventing me from doing that anyway. So what's the point of not making it public in the first place?

As I understand it as a library gets updated and improved over time you can just add the @Deprecated annotation to indicate that a specific class is old or outdated and is only still around to prevent legacy code from breaking.
I know that it makes no sense for a library to simply make all classes public, but I don't understand why classes essential to the implementation of a widget shouldn't be public. Why can't they just add the @Deprecated annotation when one of the classes is replaced by a better version, or when widgets are implemented in a different way altogether?

In short - by making those classes non-public they're reserving the right to change/delete this class at any time without breaking any client's (your) code.

Look at how many @Deprecated methods/classes there are already and you'll see why this is important.

Once you add a public class to your library you basically have to maintain it. You cannot simply remove it because that would be a breaking change. As a result you can only mark it as @Deprecated and continue to maintain it far longer than you actually have a use for it.


But it's about more than just a class becoming deprecated. Mainly I am talking about the difference between API and implementation details.

If the inner workings of a widget are not public Google can completely change the implementation without having to mark any of those classes as deprecated. This has multiple advantages in the long run.

They can remove and add classes and change everything about them without having to worry about maintaining backwards compatibility. If a third party developer wants to use some of the non public classes he or she can do that pretty easily anyway.

In Android Studio or IntelliJ you can view the source code of the support library with simply by right clicking on the class and selecting "Go to" -> "Declaration" or with these shortcuts:

  • Mac: + B
  • Windows: Ctrl + B

From there on out it is pretty simple to copy the relevant classes to your project, modify them and use them however you like.


Suffice it say that in 99% of all cases you are not going to need to do that anyway. Google surely put a lot of thought into which classes should be public and as a result are part of the API and which are just part of the implementation of a widget.

For the same reason every other class/field/variable in java (and not only) has access modifiers.

If it's not public you are not meant to use it (same rules apply to other modifiers depending on the context; encapsulation etc.).

It's probably there in order to enchance or help the library's esoteric function.

For a specific answer please make your question less broad. Which class did you want to use that wasn't public and you thought it should be?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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