본문 바로가기
카테고리 없음

Kotlin gradle plugin 의존 타입 (api, implemantation, compileOnly, runtimeOnly)

by hamcheeseburger 2024. 4. 1.

의존 타입 (Dependency types)에 대한 설명

타입 설명 언제 사용할까?
api Used both during compilation and at runtime and is exported to library consumers.

컴파일, 런타임 시 모두 적용하고 싶을 때 사용함. 해당 라이브러리 사용자들에게 의존한 라이브러리가 노출됨.
If any type from a dependency is used in the public API of the current module, use an api dependency.

의존하는 타입이 현재 모듈의 public API에 사용될 때.
implementation Used during compilation and at runtime for the current module, but is not exposed for compilation of other modules depending on the one with the `implementation` dependency.

현재 모듈에서 컴파일, 런타임 시 모두 적용하고 싶을 때 사용함.
단, `implementation` 의존한 것이 컴파일 시에 다른 모듈에 노출되지 않음.
Use for dependencies needed for the internal logic of a module.
If a module is an endpoint application which is not published, use implementation dependencies instead of api dependencies.

모듈의 내부로직에 필요하여 의존하는 경우.
외부에 제공되지 않는 endpoint 애플리케이션 인 경우 api 의존 방법 말고 implemetation 의존 방법을 사용할 수 있음.
compileOnly Used for compilation of the current module and is not available at runtime nor during compilation of other modules.

현재 모듈의 컴파일에 사용되며 런타임이나 다른 모듈의 컴파일 중에는 사용할 수 없음.
Use for APIs which have a third-party implementation available at runtime.

런타임에 사용할 수 있는 타사 구현이 있는 API에 사용.
runtimeOnly Available at runtime but is not visible during compilation of any module.

런타임시에 적용하고 싶을 때만 사용. 어떤 모듈이든 컴파일 시엔 노출되지 않음.
 

 

api vs implementation 차이를 확인하기 위한 예제

모듈명 클래스 구성
common - Model
domain - DomainService
test-api - TestController

 

// domain
class DomainService() {
    fun findModel() : Model {
		// 생략
        return model
    }
}

 

// test-api
class TestController(private val domainService: DomainService) {
	fun findModel() {
    	val model:Model = domainService.findModel()
    }
}

 

Case1

common <--implemetation-- domain <--implementation-- test-api

- test-api는 domain을 impementation 한다. domain은 common을 implementation한다.

 

-> 이 경우에는 test-api의 TestController에서 Model 객체 참조 시 컴파일 에러가 발생한다.

 

Case2

common <--api-- domain <--implementation-- test-api

- test-api는 domain을 impementation 한다. domain은 common을 api 의존한다.

 

-> 이 경우에는 TestController에서 Model 객체 참조 시 컴파일 에러가 발생하지 않는다.

api를 사용하는 케이스는 상단 표에서 '의존하는 타입이 현재 모듈의 public API에 사용될 때' 라고 했다.

 

domain 모듈의 findModel()은 다른 모듈에게 제공되는 public API라고 볼 수 있고, common 모듈의 Model은 해당 API에 활용되고 있다.

이 경우에는 내부 구현 로직에 적합한 implementation 의존 보다 api 의존이 적합한 것을 확인할 수 있다.

 

출처

- https://kotlinlang.org/docs/gradle-configure-project.html

 

Configure a Gradle project | Kotlin

 

kotlinlang.org

이전 댓글