front-end/android studio

기본 생성되는 gradle 파일

study-minjeong 2024. 7. 17. 15:23

gradle 파일

어떤 타입의 소프트웨어든 유연하게 빌드할 수 있도록 설계된 오픈 소스 빌드 자동화 툴이다.

- compile, test, run과 같은 과정을 한번에 편리하게 실행할 수 있다.

- 다양한 repository와 라이브러리를 편하게 추가/삭제할 수 있다.

- 한번 빌드된 프로젝트는 다음 빌드에서 더 빠른 속도로 빌드된다.

- jvm 기반으로 동작하기 때문에 jdk 설치가 되어있어야 한다.

 

1. gradlew (gradle-wrapper)

새로운 환경에서 개발을 시작하더라도, gradle을 설치할 필요없이 동일한 환경에서 실행할 수 있도록 해주는 내장 gradle이다. 

프로젝트를 생성하면 gradle-wrapper.jar과 gradle-wrapper.properties 파일이 생긴다.

 

2. gradle.properties

빌드 설정과 관련된 여러가지 옵션을 제공한다. 최상단에 위치한 파일이므로, 프로젝트 전체에 적용된다. 

 

3. build.gradle (Project)

프로젝트 최상위 레벨에 대한 gradle 정보가 들어가있다. 

buildscript {
    repository {
       google()
    }
    dependencies {
       classpath 'com.google.gms:google-services:4.3.10'
    }
}

plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
}

tasks.withType(JavaCompile) {
    CompileOptions.encoding = 'UTF-8'
}

(1) buildscript : 모든 모듈에 공통되는 gradle 저장소와 종속 항목을 정의한다.

     - repository : 외부 저장소 설정

     - dependencies : gradle 플러그인 버전 설정

(2) plugins : 앱에서 사용할 plugin을 선언한다. plugin은 사전에 만들어져있는 gradle task의 집합이다.

     - 최상위 프로젝트에서는 apply 옵션을 false로 둬야한다.

(3) tast : 특정한 동작을 수행하기 위한 task를 선언할 수 있다. 

(4) exit : 프로젝트 수준에서 특정 속성을 정의해서, 다른 모듈 설정 파일에서 참조 및 공유 가능하다.

 

4. build.gradle (Module)

모듈 수준의 gradle 설정 파일로, 각 모듈마다 생성된다.

모듈의 종류는 app, 웨어러블, 안드로이드 tv 등이 있다. (보통 app)

 

1) plugins

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

- 앱 모듈에서 사용할 플러그인을 선언한다.

 

2) android

android {
    namespace 'com.example.test'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.test"
        minSdk 26
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        viewBinding = true
        dataBinding = true
    }
}

 

(1) compileSdk : 프로젝트를 컴파일할 api level 설정

(2) defaultConfig : 앱 아이디, 버전명, 버전 이름, 최소 지원 os 등 기본 설정을 정의

(3) buildTypes: 어떤 식으로 빌드할 건지 정의되어 있음

    - debug : apk 용량이 크지만 개발하면서 디버깅이 가능

    - release : 난독화를 시키고, apk 용량을 축소시키는 옵션이 적용됨

(4) compileOptions: 컴파일 옵션 정의

(5) kotlinOptions: 코틀린에서 바이트 코드를 생성할 때 대상 java 버전 설정

(6) buildFeatures: 현재 view binding을 사용할 수 있도록 설정해둠

 

+) defaultConfig에서 sdk 설정 내용은 아래의 글에 정리되어있다~~

2024.07.17 - [front-end/android studio] - 안드로이드 sdk 버전 설정

 

3) dependencies

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

외부 라이브러리를 설정해주면 빌드 시 import 된다.

implementation, api, compile 등 다양한 옵션이 있어 필요한 옵션을 사용하면 된다.