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

前端 未结 9 1571
北荒
北荒 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:25

    Brief Solution:

    The better approach is to replace all compile dependencies with implementation dependencies. And only where you leak a module’s interface, you should use api. That should cause a lot less recompilation.

     dependencies {
             implementation fileTree(dir: 'libs', include: ['*.jar'])
     
             implementation 'com.android.support:appcompat-v7:25.4.0'
             implementation 'com.android.support.constraint:constraint-layout:1.0.2'
             // …
     
             testImplementation 'junit:junit:4.12'
             androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
                 exclude group: 'com.android.support', module: 'support-annotations'
             })
     }
    

    Explain More:

    Before Android Gradle plugin 3.0: we had a big problem which is one code change causes all modules to be recompiled. The root cause for this is that Gradle doesn’t know if you leak the interface of a module through another one or not.

    After Android Gradle plugin 3.0: the latest Android Gradle plugin now requires you to explicitly define if you leak a module’s interface. Based on that it can make the right choice on what it should recompile.

    As such the compile dependency has been deprecated and replaced by two new ones:

    • api: you leak the interface of this module through your own interface, meaning exactly the same as the old compile dependency

    • implementation: you only use this module internally and does not leak it through your interface

    So now you can explicitly tell Gradle to recompile a module if the interface of a used module changes or not.

    Courtesy of Jeroen Mols blog

提交回复
热议问题