맞춤 빌드 논리 적용
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이 섹션에서는 Android Gradle 플러그인을 확장하거나 플러그인을 작성할 때 유용한 고급 주제에 관해 설명합니다.
변형 종속 항목을 맞춤 로직에 게시
라이브러리에 다른 프로젝트 또는 하위 프로젝트에서 사용하려는 기능이 포함되어 있을 수 있습니다. 라이브러리 게시는 라이브러리를 사용자에게 제공하는 과정입니다. 라이브러리에서 사용자가 컴파일 및 런타임 시 액세스할 수 있는 종속 항목을 관리할 수 있습니다.
아래에 설명한 대로 사용자가 라이브러리를 이용하기 위해 사용해야 하는 각 클래스 경로의 전이 종속 항목이 포함된 두 개의 별도 구성이 있습니다.
variant_nameApiElements
: 이 구성에는 사용자가 컴파일 시에 이용 가능한 전이 종속 항목이 포함됩니다.
variant_nameRuntimeElements
: 이 구성에는 사용자가 런타임에 이용 가능한 전이 종속 항목이 포함됩니다.
여러 구성 간의 관계에 관한 자세한 내용은 자바 라이브러리 플러그인 구성을 참고하세요.
맞춤 종속 항목 해결 전략
한 프로젝트에 동일한 라이브러리의 두 버전에 관한 종속 항목이 포함될 수 있으며 이 때문에 종속 항목 충돌이 발생할 수 있습니다.
예를 들어 프로젝트가 모듈 A의 버전 1과 모듈 B의 버전 2에 종속되고 모듈 A가 모듈 B의 버전 3에 전이 종속된 경우 종속 항목 충돌이 발생합니다.
이 충돌을 해결하기 위해 Android Gradle 플러그인에서는 다음과 같은 종속 항목 해결 전략을 사용합니다. 즉 플러그인에서 종속 항목 그래프에 동일한 모듈의 여러 버전이 있음을 감지하면 기본적으로 버전 번호가 가장 높은 버전을 선택합니다.
그러나 이 전략이 항상 의도한 대로 작동하는 것은 아닙니다. 종속 항목 해결 전략을 맞춤설정하려면 다음 구성을 사용하여 작업에 필요한 변형의 특정 종속 항목을 해결해야 합니다.
variant_nameCompileClasspath
: 이 구성에는 주어진 변형의 컴파일 클래스 경로에 맞는 해결 전략이 포함되어 있습니다.
variant_nameRuntimeClasspath
: 이 구성에는 주어진 변형의 런타임 클래스 경로에 맞는 해결 전략이 포함되어 있습니다.
Android Gradle 플러그인에는 각 변형의 구성 객체에 액세스하는 데 사용할 수 있는 getter가 포함되어 있습니다. 따라서 변형 API를 사용하여
아래 예와 같이 종속 항목 해결을 쿼리해야 합니다.
Kotlin
android {
applicationVariants.all {
// Return compile configuration objects of a variant.
compileConfiguration.resolutionStrategy {
// Use Gradle's ResolutionStrategy API
// to customize how this variant resolves dependencies.
...
}
// Return runtime configuration objects of a variant.
runtimeConfiguration.resolutionStrategy {
...
}
// Return annotation processor configuration of a variant.
annotationProcessorConfiguration.resolutionStrategy {
...
}
}
}
Groovy
android {
applicationVariants.all { variant ->
// Return compile configuration objects of a variant.
variant.getCompileConfiguration().resolutionStrategy {
// Use Gradle's ResolutionStrategy API
// to customize how this variant resolves dependencies.
...
}
// Return runtime configuration objects of a variant.
variant.getRuntimeConfiguration().resolutionStrategy {
...
}
// Return annotation processor configuration of a variant.
variant.getAnnotationProcessorConfiguration().resolutionStrategy {
...
}
}
}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Apply custom build logic\n\nThis section describes advanced topics that are useful when you want to extend\nthe Android Gradle plugin or write your own plugin.\n\nPublish variant dependencies to custom logic\n--------------------------------------------\n\nA library can have functionalities that other projects or sub-projects might\nwant to use. Publishing a library is the process by which the library is made\navailable to its consumers. Libraries can control which dependencies its\nconsumers have access to at compile time and runtime.\n\nThere are two separate configurations that hold the transitive dependencies of\neach classpath which must be used by consumers to consume the library as\ndescribed below:\n\n- \u003cvar translate=\"no\"\u003evariant_name\u003c/var\u003e`ApiElements`: This configuration holds the transitive dependencies that are available to consumers at compile time.\n- \u003cvar translate=\"no\"\u003evariant_name\u003c/var\u003e`RuntimeElements`: This configuration holds the transitive dependencies that are available to consumers at runtime.\n\nTo learn more about the relationships between the different configurations,\ngo to [The Java Library\nplugin configurations](https://docs.gradle.org/current/userguide/java_library_plugin.html#s\nec:java_library_configurations_graph).\n\nCustom dependency resolution strategies\n---------------------------------------\n\nA project may include a dependency on two different versions of the same library\nwhich can lead to dependency conflicts.\nFor example, if your project depends on version 1 of module A and version 2 of\nmodule B, and module A transitively depends on version 3 of module B,\nthere arises a dependency version conflict.\n\nTo resolve this conflict, the Android Gradle plugin uses the following\ndependency resolution strategy: when the plugin detects that different versions\nof the same module are in the dependency graph, by default, it chooses the one\nwith the highest version number.\n\nHowever, this strategy might not always work as you intend. To customize the\ndependency resolution strategy, use the following configurations to\nresolve specific dependencies of a variant that are needed for your task:\n\n- \u003cvar translate=\"no\"\u003evariant_name\u003c/var\u003e`CompileClasspath`: This configuration contains the resolution strategy for a given variant's compile classpath.\n- \u003cvar translate=\"no\"\u003evariant_name\u003c/var\u003e`RuntimeClasspath`: This configuration contains the resolution strategy for a given variant's runtime classpath.\n\nThe Android Gradle plugin includes getters that you can use to access the\nconfiguration objects of each variant. Thus, you can use the variant API to\nquery the dependency resolution as shown in the example below: \n\n### Kotlin\n\n```kotlin\nandroid {\n applicationVariants.all {\n // Return compile configuration objects of a variant.\n compileConfiguration.resolutionStrategy {\n // Use Gradle's https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html\n // to customize how this variant resolves dependencies.\n ...\n }\n // Return runtime configuration objects of a variant.\n runtimeConfiguration.resolutionStrategy {\n ...\n }\n // Return annotation processor configuration of a variant.\n annotationProcessorConfiguration.resolutionStrategy {\n ...\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n applicationVariants.all { variant -\u003e\n // Return compile configuration objects of a variant.\n variant.getCompileConfiguration().resolutionStrategy {\n // Use Gradle's https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html\n // to customize how this variant resolves dependencies.\n ...\n }\n // Return runtime configuration objects of a variant.\n variant.getRuntimeConfiguration().resolutionStrategy {\n ...\n }\n // Return annotation processor configuration of a variant.\n variant.getAnnotationProcessorConfiguration().resolutionStrategy {\n ...\n }\n }\n}\n```"]]