티스토리 뷰

[Android Groovy] build.gradle plugins apply false? 왜 false 인가

안드로이드는 Groovy 를 통해 build.gradle 를 작성하여 빌드 설정을 할 수 있다. 이때 플러그인 적용 시 문법 중에 아래와 같은 문법이 있다.

plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

여기서 apply 에 항상 false 를 넣어주는 것 같아서 의아했다. 플러그인을 적용하려면 apply true 가 되어야 하는거 아닌가? 하는 의문이었다. 한 번 살펴보자.

package org.gradle.plugin.use;

import javax.annotation.Nullable;
import org.gradle.api.Incubating;
import org.gradle.api.provider.Provider;

public interface PluginDependencySpec {
    PluginDependencySpec version(@Nullable String var1);

    @Incubating
    default PluginDependencySpec version(Provider<String> version) {
        return this.version((String)version.get());
    }

    PluginDependencySpec apply(boolean var1);
}

apply 정의 내부로 들어가보면 위와 같이 boolean 타입의 var1 을 받는 것을 알 수 있다. 그렇다 이 것만 봐서는 알 수 있는게 전혀 없다. 😓

docs.gradle.org 에서 gradle 7.5.1 문서를 뒤져보았다.

Groovy apply

PluginDependencySpec apply​(boolean apply)
Specifies whether the plugin should be applied to the current project. Otherwise it is only put on the project's classpath.
This is useful when reusing classes from a plugin or to apply a plugin to sub-projects:

 plugins {
     id "org.company.myplugin" version "1.0" apply false
 }

 subprojects {
     if (someCondition) {
         apply plugin: "org.company.myplugin"
     }
 }
 
Parameters:
apply - whether to apply the plugin to the current project or not. Defaults to true
Returns:
this

살펴보니, apply 로 전달되는 불리언 값은 현재 플러그인을 현재 프로젝트에 (전체) 적용할지 또는 오로지 프로젝트의 classpath 에만 적용을 해줄 지 여부에 대해서 설정하는 옵션이었다.

default 값은 true 이기때문에, apply false 를 통해서 클래스 경로에만 적용을 해주는 것. 때로는 위 예시로 보면 subproject 전체에 적용해주고 싶은 경우에는 이를 생략하여 전달해주면 된다.

결론

이제 왜 apply false 인지 알게되었다. applyProject 로 좀 더 직관적으로 알 수 있게 해주면 더 좋지 않았을까. 😶‍🌫️

2022.09.30 - [Android/Retrofit] - [Android] Retrofit HTTP Converter 결과값 파싱이 null 로 올 때 ☠️

2022.08.23 - [SELECT *] - [Android] 해상도별 drawable 이미지 리소스 추가하기 (dpi 별)

2022.04.24 - [SELECT *] - [Hilt] Hilt Gradle 빌드 설정하기

댓글
최근에 올라온 글
최근에 달린 댓글
네이버 이웃추가
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함