将依赖项添加到android库'api'与'implementation'

时间:2018-08-03 14:14:39

标签: android gradle build.gradle

让我们假设我正在创建Android库,并想使用ConstraintLayout。我看到其他库创建者有两种不同的方法:

1.使用implementation关键字添加它。缺点是,如果有人将我的库添加到项目中,并且在依赖项中没有约束布局,则构建将因以下错误而失败:error: attribute 'com.example.test:layout_constraintBottom_toBottomOf' not found.

2.使用“ api”添加它。这样,它可以很好地进行编译,但是如果任何其他库在其他版本中使用约束布局,则会产生错误Android dependency 'com.android.support.constraint:constraint-layout:1.1.2' has different version for the compile (1.0.0) and runtime (1.1.2)

哪种方式对客户代码的维护成本最低?

1 个答案:

答案 0 :(得分:1)

这与客户端代码的最低维护成本无关。有关客户端是否需要传递依赖项-ConstraintLayout-以便针对您的库进行编译。无论您使用implementation还是api,传递依赖始终是客户端的运行时依赖。

这是您需要问自己的问题:您是在库的公共API的方法签名或属性中使用ConstraintLayout还是可传递库的任何其他元素,即客户端使用的东西?如果是,则应将约束布局库声明为api。否则,请使用implementation

相关问题