other_program_languages/코틀린, 안드로이드

코틀린 실습 1 : Project, Application and Activity 생성

MasterOfAI 2022. 4. 3. 19:59

New project

자동 다운로드가 시작되고, 완료되면 아래와 같이 tree 생성 

이제 tree를 Project로 변경한다. 

>build.gradle: 전체 package 에 대한 gradle 의 build 설정, 변경할 필요 없다. 

>app->build.gradle : 이것은 변경이 필요하다. 

 

app->build.gradle 상세 

 

plugins {
    id 'com.android.application'   //안드로이드를 위한 plugin
    id 'org.jetbrains.kotlin.android' //코틀린을 위한 plugin
}

android {
    compileSdk 32  //특정 안드로이드 OS 버젼에 맞추어 빌드 

    defaultConfig {
        applicationId "com.example.learningsppon"
        minSdk 26      //최소 지원 OS 버젼 
        targetSdk 32   //타깃 지원 OS qjwus 
        versionCode 1   //해당 App 의 version code
        versionName "1.0" //해당 App 의 Version Name

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"  //소프트웨어 테스트 코드 
    }

    buildTypes {   
        release {  //실제 relese 관련 설명
            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'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0' 
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'  //안드로이드 코드 테스트 유닛
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

 

신규 Activity 만들기 

MainActivity 가 생성됨
해당 아이콘을 눌러 보면 , Manifest.xml 과 layout을 볼 수 있다.

 

신규 Application 만들기 

Class 를 선택하고 , 타이핑

그리고 아래와 같이 코드를 구현 하면 된다. 

package com.example.learningsppon

import android.app.Application

class LearningSpoonApplication : Application(){
    // 전처리 과정
    // 전역 변수
    // 전역 처리 과정이 존재

    override fun onCreate() {
        super.onCreate()
    }
}

override fun onCreate()를 하기 위해서는, 

1. write click 

2. Generate (ALT + Insert)

3. Override Methods 선택

4. onCreate 선택