Gradle实现与API配置 [英] Gradle Implementation vs API configuration
问题描述
我试图弄清楚 api
和实现
配置在建立我的依赖关系时有什么区别。
在文档中,它表示实现
具有更好的构建时间,但是,
看到评论在类似的问题,我不知道是否是真的。
因为我不是Gradle的专家,所以我希望有人能帮忙。我已阅读文档,但我想知道一个易于使用的方法,理解解释。
我认为这个话题需要更多的报道,因为对每个开发者来说可能都不那么直接。 >
Gradle compile
关键字已被弃用,以支持新的 api
和执行
关键字。
我不会解释 api
,因为它与使用旧的 compile
相同,所以如果用<$ c $替换所有的 compile
c> api 一切都会一如既往。
要理解实现
关键字,我们需要一个例子。
示例
我们有一个名为 MyLibrary
在内部我们使用另一个名为 InternalL的库ibrary
。像这样:
//内部库模块
public class InternalLibrary {
public static String giveMeAString( ){
返回hello;
//我的库模块
public class MyLibrary {
public String myString(){
return InternalLibrary.giveMeAString( );
$ / code>
build.gradle
MyLibrary
的依赖关系如下:
依赖关系{
api project(':InternalLibrary')
}
你要使用的代码 MyLibrary
,所以你应该有一个 build.gradle
这个依赖关系
依赖关系{
pre>
api project(':MyLibrary')
}
在您的应用程序代码中,使用
api
关键字(或者使用旧的编译)您可以访问
MyLibrary
和InternalLibrary
。//所以你可以访问这个库(因为它应该)
MyLibrary myLib = new MyLibrary();
System.out.println(myLib.myString());
//但你也可以访问内部库(你不应该)
System.out.println(InternalLibrary.giveMeAString());
通过这种方式,您可能会泄漏某些您不应该使用的内部实现,它不会被你直接导入。
为了防止这种情况,Gradle创建了新的
api 到实现关键字,所以现在if在
MyLibrary
implementation
>
依赖关系{
实施项目(':InternalLibrary')
}
并且在您的应用程序中
build.gradle
依赖关系{
实现项目(':MyLibrary')
}
您将无法再在应用程序代码中调用
InternalLibrary.giveMeAString()
。如果MyLibrary
使用api
关键字导入InternalLibrary
,在您的应用程序中,如果您使用api
或<您的应用程序,您可以独立调用InternalLibrary.giveMeAString()
code> implementation 将MyLibrary
添加到您的应用程序中。 使用此功能Android Gradle插件知道如果你在InternalLibrary
中编辑某些东西,它会触发重新编译MyLibrary
。它不会触发整个应用程序的重新编译,因为您无权访问InternalLibrary
。当你有很多嵌套的依赖关系时,这种机制可以加速构建过程。
结论
-
切换到新的Android Gradle插件3。 XX,您应该用
实现
关键字(1 *)替换所有compile
。然后尝试编译并测试您的应用程序。如果一切顺利,代码保持原样,如果您遇到问题,您的依赖关系可能会出现问题,或者您使用的是现在是私有的并且不易访问的内容。 Android Gradle插件工程师Jerome Dochez的建议(1 )* ) 如果您是图书馆mantainer你应该使用
api
来为你的库的公共API所需的每个依赖项,同时使用 implementation
来进行测试最终用户不能使用的依赖项或依赖项。 参考文献 $ b(这是为节省时间而分割的相同视频) Google I / O 2017 - 如何加速Gradle的构建(仅适用于NEW GRADLE PLUGIN 3.0.0)
Google I / O 2017 - 加速Gradle构建的速度(参考 1 * )
I'm trying to figure it out what is the difference between api
and implementation
configuration while building my dependencies.
In the documentation, it says that implementation
has better build time, but,
seeing this comment in a similar question I got to wonder if is it true.
Since I'm no expert in gradle, I hope someone can help. I've read the documentation already but I was wondering about a easy-to-understand explanation.
I think this topic needs a bit more coverage because maybe is not so immediate for every developer.
Gradle compile
keyword has been deprecated in favor of the new api
and implementation
keywords.
I will not explain api
, because it's the same thing as using the old compile
, so if you replace all your compile
with api
everything will works as always.
To understand the implementation
keyword we need an example.
EXAMPLE
We have this library called MyLibrary
where internally we are using another library called InternalLibrary
. Something like this:
//internal library module
public class InternalLibrary {
public static String giveMeAString(){
return "hello";
}
}
//my library module
public class MyLibrary {
public String myString(){
return InternalLibrary.giveMeAString();
}
}
The build.gradle
dependencies of MyLibrary
its like this:
dependencies {
api project(':InternalLibrary')
}
Now in your code you want to use MyLibrary
so you should have a build.gradle
with this dependency
dependencies {
api project(':MyLibrary')
}
In your application code, with the api
keyword (or using the old compile
) you can access both MyLibrary
and InternalLibrary
.
//so you can access the library (as it should)
MyLibrary myLib = new MyLibrary();
System.out.println(myLib.myString());
//but you can access the internal library too (and you shouldn't)
System.out.println(InternalLibrary.giveMeAString());
In this way you are potentially "leaking" the internal implementation of something that you shouldn't use because it's not directly imported by you.
To prevent this, Gradle has created the new implementation
keyword, so now if you switch api
to implementation
in your MyLibrary
dependencies {
implementation project(':InternalLibrary')
}
And in your app build.gradle
dependencies {
implementation project(':MyLibrary')
}
you won't be able to call InternalLibrary.giveMeAString()
in your app code anymore. While if MyLibrary
uses the api
keyword to import InternalLibrary
, in your app you will be able to call InternalLibrary.giveMeAString()
without problems, independently if you use api
or implementation
to add MyLibrary
to your app.
Using this sort of boxing strategy the Android Gradle plugin knows that if you edit something in InternalLibrary
it will trigger the recompilation of MyLibrary
only. It will not trigger the recompilation of your entire app because you don't have access to InternalLibrary
. This mechanism when you have a lot of nested dependencies can speed-up the build a lot.(Watch the video linked at the end for a full understanding of this)
CONCLUSIONS
When you switch to the new Android Gradle plugin 3.X.X, you should replace all your
compile
with theimplementation
keyword (1*). Then try to compile and test your app. If everything it's ok leave the code as is, if you have problems you probably have something wrong with your dependencies or you used something that now is private and not more accessible. Suggestion by Android Gradle plugin engineer Jerome Dochez (1)*)If you are a library mantainer you should use
api
for every dependency which is needed for the public API of your library, while useimplementation
for test dependencies or dependencies which must not be used by the final users.
REFERENCES (This is the same video splitted up for time saving)
Google I/O 2017 - How speed up Gradle builds (FULL VIDEO)
Google I/O 2017 - How speed up Gradle builds (NEW GRADLE PLUGIN 3.0.0 PART ONLY)
Google I/O 2017 - How speed up Gradle builds (reference to 1*)
这篇关于Gradle实现与API配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!