compile과 implementation의 차이

Caesars·2022년 8월 16일
2
implementation 'com.android.support:appcompat-v7:25.0.0'
testImplementation 'junit:junit:4.12'


compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'

build.gradle 파일을 보다보면 라이브러리 추가 방식이 implementation과 compile 두 가지가 있는 것을 알 수 있습니다. 별 생각 없이 써왔었는데 Gradle 7 부터는 compile을 더 이상 사용하지 않게 되었다고 해서 무슨 이유인지 찾아봤습니다.


compile과 implementation의 차이

두 방식의 차이는 추가하는 라이브러리의 적용 범위입니다.

compile(api)를 사용하는 경우
compile은 build 시 implementation보다 더 많은 라이브러리를 가져옵니다.
예를 들어, Library A에서 compile로 Library C를 가져온 다음 App Module 에서 compile로 Library A를 가져올 경우 A가 가져왔던 Library C도 같이 가져오게 됩니다.

implementaion을 사용하는 경우
모듈을 수정하면, 직접 의존하고 있는 라이브러리만 재빌드.

따라서 implementaion를 사용하면 빌드가 빠르게 됩니다. 연결된 dependency가 줄어들고, change하는 것이 있더라도 recompile을 적게 하니 소요하는 시간이 적다.

또한 api의 노출의 차이가 있습니다. compile은 연결된 api가 모두 노출되고, implementaion은 노출되지 않는데, 아무래도 api가 노출되면 api로직에서 유효성 검사 및 기타 원하지 않는 형태의 데이터가 들어와 보안의 위협이 생깁니다.

gradle 7.0 이후 변경 필요

  • compile -> implementation
  • testCompile -> testImplementation
  • debugCompile -> debugImplementation
  • compileOnly나 runtimeOnly 는 여전히 사용가능합니다. 특수한 경우가 아니라면 implementation이 나아보입니다. 이유는 classpath를 통일시키기 때문입니다.

Java 프로젝트를 빌드하고 실행할 때 두 개의 클래스 경로가 있습니다.

  • compileClasspath – JDK가 Java 코드를 .class 파일 로 컴파일하는 데 필요한 종속성
  • runtimeClasspath – 컴파일된 Java 코드를 실제로 실행하는 데 필요합니다.

compileOnly – compileClasspath에만 종속성을 둡니다.
runtimeOnly – runtimeClasspath에만 종속성을 둡니다.
implementation - 두 classpath 모두에 대한 종속을 두기 때문에 복잡성을 줄일 수 있습니다.

기타 설정

  • implementation
    Implementation only dependencies.

  • compileOnly
    Compile time only dependencies, not used at runtime.

  • compileClasspath extends compileOnly, implementation
    Compile classpath, used when compiling source. Used by task compileJava.

  • annotationProcessor
    Annotation processors used during compilation.

  • runtimeOnly
    Runtime only dependencies.

  • runtimeClasspath extends runtimeOnly, implementation
    Runtime classpath contains elements of the implementation, as well as runtime only elements.

  • testImplementation extends implementation
    Implementation only dependencies for tests.

  • testCompileOnly
    Additional dependencies only for compiling tests, not used at runtime.

  • testCompileClasspath extends testCompileOnly, testImplementation
    Test compile classpath, used when compiling test sources. Used by task compileTestJava.

  • testRuntimeOnly extends runtimeOnly
    Runtime only dependencies for running tests.

  • testRuntimeClasspath extends testRuntimeOnly, testImplementation
    Runtime classpath for running tests. Used by task test.


결론

implementation 은 지정한 모듈만 가져오고 compile(api) 는 상위 모듈까지 전부 가져옵니다.
어떻게 해도 동작은 되겠지만 빌드 속도가 빠르고 필요한 모듈만 가져오는 implementation 을 사용하는게 낫습니다.


참고

https://jipang9-greedy-pot.tistory.com/m/16
https://stackoverflow.com/questions/44493378/whats-the-difference-between-implementation-api-and-compile-in-gradle

profile
잊기전에 저장

0개의 댓글