이 튜토리얼에서는 안드로이드에서 Build.VERSION 클래스를 활용해 현재 기기의 SDK(API) 버전과 OS 릴리스 버전을 프로그래밍 방식으로 확인하고 화면에 출력하는 방법을 단계별로 살펴봅니다.
구현 단계
1단계: 새 프로젝트 생성
Android Studio에서 File → New Project 메뉴로 이동한 뒤, 프로젝트 생성에 필요한 정보를 모두 입력해 새 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 버전 정보를 표시할 TextView 하나를 배치한 간단한 세로 방향 LinearLayout입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:text=""
android:id="@+id/textView"
android:layout_marginTop="30dp"
android:textStyle="bold"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3단계: MainActivity 코드 작성
src/MainActivity.java에 아래 코드를 작성합니다. 여기서 핵심은 다음 두 가지 속성입니다.
- Build.VERSION.SDK_INT: 현재 기기의 API 레벨(SDK 버전)을 정수 값으로 반환합니다.
- Build.VERSION.RELEASE: 사용자에게 익숙한 OS 버전 문자열(예: "13", "14")을 반환합니다.
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
int versionAPI = Build.VERSION.SDK_INT;
String versionRelease = Build.VERSION.RELEASE;
textView.setText("API Version :" + versionAPI + "\n" + "Version Release : " + versionRelease);
}
}
4단계: AndroidManifest.xml 설정
androidManifest.xml에 아래 코드를 추가해 MainActivity를 런처 액티비티로 등록합니다. 이 예제에는 별도의 권한 선언이 필요하지 않습니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<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>
앱 실행 및 결과 확인
실제 안드로이드 기기를 컴퓨터에 연결한 상태에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭해 앱을 실행합니다. 기기 선택 창에서 자신의 모바일 기기를 고르면, 화면에 아래 이미지와 같이 API 버전과 릴리스 버전이 함께 표시되는 것을 확인할 수 있습니다.