Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Kotlin으로 Android SDK 버전 확인하기 – 프로그래밍 방식 완벽 가이드

이 튜토리얼에서는 Kotlin을 사용하여 프로그래밍 방식으로 현재 Android 기기의 SDK 버전(API 레벨)과 릴리스 버전을 가져오는 방법을 단계별로 알아봅니다. Build.VERSION.SDK_INTBuild.VERSION.RELEASE 속성만 활용하면 누구나 쉽게 구현할 수 있습니다.

1단계: 새 프로젝트 생성

Android Studio를 실행하고 File ⇒ New Project 메뉴로 이동한 후, 새 프로젝트를 만들기 위해 필요한 모든 세부 정보(프로젝트 이름, 패키지명, 저장 위치 등)를 입력합니다. 언어는 반드시 Kotlin으로 선택하세요.

2단계: 레이아웃 파일 작성 (res/layout/activity_main.xml)

아래 코드를 activity_main.xml에 추가합니다. 화면 중앙에 SDK 버전 정보를 표시할 TextView를 배치했습니다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="4dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

3단계: MainActivity.kt 작성

핵심 로직은 매우 간단합니다. Build.VERSION.SDK_INT로 API 레벨(정수형)을, Build.VERSION.RELEASE로 사용자에게 익숙한 버전 문자열(예: "13", "14")을 가져올 수 있습니다.

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        textView = findViewById(R.id.textView)

        val versionAPI = Build.VERSION.SDK_INT
        val versionRelease = Build.VERSION.RELEASE

        textView.text = "API Version : $versionAPI\nVersion Release : $versionRelease"
    }
}

참고: 특정 기능이 최신 OS에서만 동작하도록 분기 처리할 때는 다음과 같이 활용할 수도 있습니다.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    // Android 13(API 33) 이상에서만 실행할 코드
}

4단계: AndroidManifest.xml 설정

이 예제는 특별한 권한이 필요하지 않으므로, 기본 매니페스트 파일을 그대로 사용하면 됩니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 스마트폰을 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 상단 툴바의 Run(실행) 아이콘을 클릭하세요.

실행 옵션 목록에서 연결된 모바일 기기를 선택하면, 앱이 설치되어 실행되며 화면 중앙에 해당 기기의 API 버전과 릴리스 버전이 아래와 같이 표시됩니다.

Kotlin으로 Android SDK 버전 확인하기 – 프로그래밍 방식 완벽 가이드

정리

  • Build.VERSION.SDK_INT: 정수형 API 레벨 반환 (예: 33, 34)
  • Build.VERSION.RELEASE: 버전 문자열 반환 (예: "13", "14")
  • 두 값을 조합하면 기기 호환성 체크, 조건부 기능 실행 등 다양한 시나리오에 활용할 수 있습니다.