본문으로 바로가기

[Android App] 모듈 build.gradle 을 .kts 로 변경하기

category Android_app 2년 전

 : 기본적으로 Empty Compose Activity 를 생성한 상태에서 모듈 app 의 build.gradle 파일을

    build.gradle.kts 파일로 변경시 수정사항을 정리해 봤습니다.

    kts 파일과 gradle 파일이 같이 있어도 빌드에는 문제가 없습니다.

 

 

▶  module: app  의 build.gradle 이름을 build.gradle.kts 로 변경후 Sync now 클릭합니다.

refactor ==> Rename

 

build.gradle.kts 파일 우측상단

 아래와 같은 에러가  발생합니다.

\app\build.gradle.kts:2:8: Unexpected tokens (use ';' to separate expressions on the same line)

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

 

 

▶ id 부분을 아래처럼 변경후 Sync now 또는 Try Again 클릭

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

 

▶ 에러내용은 같고 위치만 변경 됩니다.

namespace 'com.example.test_app'

to

namespace ="com.example.test_app"

 

8번라인 

compileSdk 33
==>
compileSdk = 33

 

defaultConfig 

    defaultConfig {
        applicationId "com.example.test_app"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }
    
    ==> 
    
    defaultConfig {
        applicationId ="com.example.test_app"
        minSdk = 24
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

 

 

▶ buildTypes

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    ==> 
    buildTypes {
        getByname("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

 

 

  compileOptions 수정

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
   
    ==> 
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

 

  kotlinOptions 수정

    kotlinOptions {
        jvmTarget = '1.8'
    }
	==>
    kotlinOptions {
        jvmTarget = "1.8"
    }

    or
    kotlinOptions {
        val options = this as org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
        options.jvmTarget = "1.8"
    }

 

 

buildFeatures 수정

    buildFeatures {
        compose true
    }    
    ==> 
    buildFeatures {
        compose = true
    }

 

 

 

 composeOptions 수정

    composeOptions {
        kotlinCompilerExtensionVersion '1.2.0'
    }
    ==>
    composeOptions {
        kotlinCompilerExtensionVersion = "1.2.0"
    }

 

 

 

▶ dependencies 수정

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.2.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}

==>
dependencies {

    implementation("androidx.core:core-ktx:1.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
    implementation("androidx.activity:activity-compose:1.3.1")
    implementation("androidx.compose.ui:ui:$compose_ui_version")
    implementation("androidx.compose.ui:ui-tooling-preview:$compose_ui_version")
    implementation("androidx.compose.material:material:1.2.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_ui_version")
    debugImplementation("androidx.compose.ui:ui-tooling:$compose_ui_version")
    debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_ui_version")
}

 

packagingOptions

    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    
    ==> 
    packagingOptions {
        resources.excludes.add("/META-INF/{AL2.0,LGPL2.1}")
    }

 

  Unresolved reference: compose_ui_version

dependencies{
   // 임시로 변수를 사용해 봤습니다.
    val compose_ui_version = "1.2.0"
    ~~ 중략 ~~
}

 

< 기타 >

   ▶ 참고 사이트

https://developer.android.com/studio/build/migrate-to-kts?hl=ko 

 

Groovy에서 KTS로 빌드 구성 이전  |  Android 개발자  |  Android Developers

Gradle 구성 파일을 Groovy에서 KTS로 이전합니다.

developer.android.com

 

 

간단히 build.gradle 파일을 build.gradle.kts 파일 변경후 빌드를 해봤습니다.

참고하세요.

반응형