본문 바로가기
Android/Tip

[Android] android java kotlin 같이 쓰기

by Jay Son 아기 냥이 해린 짱💖 2021. 5. 16.

개요 : 기존 자바로 구현되어 있는 앱에 Kotlin 코드와 같이 사용하고 싶을 경우 Kotlin SDK 추가 방법 및 API 호출 방법 공유

 

Project build.gradle에 kotlin plugin 선언

코틀린 버전은 원하는 버전으로 사용.

 

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.5.0'

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

 

Module build.gradle 설정

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.javawithkotlin"
        minSdkVersion 16
        targetSdkVersion 30
        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
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

기존 앱에 Kotlin 추가

Android 스튜디오에서는 Kotlin을 완벽하게 지원하므로 기존 프로젝트에 Kotlin 파일을 추가하고 자바 언어 코드를 Kotlin으로 변환할 수 있습니다. 그런 다음 Kotlin 코드와 함께 자동 완성, 린트 검사, 리팩터링, 디버깅 등 Android 스튜디오의 기존 도구를 모두 사용할 수 있습니다.

출처 : https://developer.android.com/kotlin/add-kotlin?hl=ko 

 

기존 앱에 Kotlin 추가  |  Android 개발자  |  Android Developers

Android 스튜디오에서는 Kotlin을 완벽하게 지원하므로 기존 프로젝트에 Kotlin 파일을 추가하고 자바 언어 코드를 Kotlin으로 변환할 수 있습니다. 그런 다음 Kotlin 코드와 함께 자동 완성, 린트 검사,

developer.android.com

 

Java 코드에서 Kotlin 코드 사용하기 알아보기

https://json8.tistory.com/156

 

[Android] Java 코드에서 Kotlin 코드 사용하기

기존앱에서 코틀린 추가 방법은 아래 링크 참조 https://json8.tistory.com/155 [Android] android java kotlin 같이 쓰기 개요 : 기존 자바로 구현되어 있는 앱에 Kotlin 코드와 같이 사용하고 싶을 경우 Kotlin..

json8.tistory.com

 

반응형