What's the difference between implementation and compile in Gradle?

前端 未结 9 1478
北荒
北荒 2020-11-22 03:01

After updating to Android Studio 3.0 and creating a new project, I noticed that in build.gradle there is a new way to add new dependencies instead of comp

9条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 03:38

    Since version 5.6.3 Gradle documentation provides simple rules of thumb to identify whether an old compile dependency (or a new one) should be replaced with an implementation or an api dependency:

    • Prefer the implementation configuration over api when possible

    This keeps the dependencies off of the consumer’s compilation classpath. In addition, the consumers will immediately fail to compile if any implementation types accidentally leak into the public API.

    So when should you use the api configuration? An API dependency is one that contains at least one type that is exposed in the library binary interface, often referred to as its ABI (Application Binary Interface). This includes, but is not limited to:

    • types used in super classes or interfaces
    • types used in public method parameters, including generic parameter types (where public is something that is visible to compilers. I.e. , public, protected and package private members in the Java world)
    • types used in public fields
    • public annotation types

    By contrast, any type that is used in the following list is irrelevant to the ABI, and therefore should be declared as an implementation dependency:

    • types exclusively used in method bodies
    • types exclusively used in private members
    • types exclusively found in internal classes (future versions of Gradle will let you declare which packages belong to the public API)

提交回复
热议问题