假设我正在创建 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) 和运行时 (1.1.2)
哪种方式维护客户端代码的成本最低?
最佳答案
这不是关于客户端代码的最低维护成本。这是关于客户端是否需要传递依赖项 — ConstraintLayout
— 以便针对您的库进行编译。无论您使用 implementation
还是 api
,传递依赖始终是客户端的运行时依赖。
这是您需要问自己的问题:您是否在库的公共(public) API 的方法签名或属性中使用了 ConstraintLayout
或传递库的任何其他元素,即客户?如果是,则应将约束布局库声明为 api
。否则,使用实现
。
关于android - 添加依赖到android库 'api' vs 'implementation',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51674589/