Language/Kotlin

Kotlin - Kotlin on Android (Android Studio for Windows)

TechNote.kr 2018. 7. 16. 01:07
728x90

 Kotlin에 관심을 가지게 된 이유는 Android를 위한 App build가 가능하다는 점 때문이었다. Kotlin을 개발한 IntelliJ에서 Android Studio 또한 개발하고 있다. 이 때문인지 Android Studio 상에서 Kotlin을 통해 쉽게 android app build가 가능하다.


먼저 Android Studio 3.0 이상에서 기본적으로 지원이 되고, 그 이하 버전에서는 별도로 kotlin plugin 을 설치해 주어야 한다고 한다.


The Kotlin plugin is bundled with Android Studio starting from version 3.0. If you use an earlier version, you'll need to install the Kotlin plugin. Go to File | Settings | Plugins | Install JetBrains plugin… and then search for and install Kotlin. If you are looking at the "Welcome to Android Studio" screen, choose Configure | Plugins | Install JetBrains plugin… You'll need to restart the IDE after this completes.


2018년 7월 16일 기준으로 최신의 Android Studio 3.1.3 을 설치하였기에 별도의 kotlin plugin 은 설치해주지는 않았다. kotlin을 이용한 Android app build 방법은 다음과 같다. 



1) Android Studio 를 실행하고 "Start a new Android Studio project" 를 선택




2) 일반 Java 기반의 android app project 를 만들듯이 Application name, Company domain, Project location을 설정하고. "Include Kotlin support"를 선택




3) Minimum SDK 를 선택




4) 작성하고자 하는 Activity 선택




5) Activity name 설정




6) 자동으로 생성된 코드 파일을 보면 확장자가 kotlin source code를 의미하는 .kt 이고, 실제 코드 또한 kotlin 으로 되어 있음을 확인 가능




7) 해당 program을 run 하면 아래와 같이 app 실행 확인 가능




실제 기본 android 를 위한 kotlin app project 를 만드는 과정을 보면 "Include Kotlin support" 외에는 모두 동일함을 알 수 있다. kotlin을 기반한 project의 뼈대는 위와 같이 쉽게 생성이 가능하다.



build.gradle의 buildscript를 보면 빌드할 때 사용되는 kotlin의 버전을 확인하 수 있다. 




뿐만 아니라 kotlin-android가 plugin으로 포함되어 있음을 확인할 수 있다.




지금은 코드 자체가 kotlin code로 생성되어 메뉴가 비활성화되어 있지만 "Code > Convert Java File to Kotlin File" 메뉴를 통해 Java 파일을 Kotlin 파일로 변환 또한 가능하다.





728x90

'Language > Kotlin' 카테고리의 다른 글

Kotlin - for/while Loops  (0) 2018.07.24
Kotlin - when expression  (0) 2018.07.23
Kotlin - if expression  (0) 2018.07.17
Kotlin - Control Flow (제어 흐름)  (0) 2018.07.17
Kotlin - Packages  (0) 2018.07.16
Kotlin - Kotlin on JVM (the IntelliJ IDEA Community Edition for Windows)  (0) 2018.07.12
Kotlin - Strings (문자열)  (0) 2018.07.11
Kotlin - Arrays (배열)  (0) 2018.07.11