개요
이 튜토리얼에서는 안드로이드 앱 내부에서 빌드 버전 정보, 즉 버전 이름(Version Name)과 버전 코드(Version Code)를 가져와 화면에 표시하는 방법을 단계별로 살펴봅니다.
앱의 현재 버전 정보는 업데이트 안내, 디버깅, 사용자에게 버전 공개 등 다양한 상황에서 유용하게 활용됩니다.
1단계: 새 프로젝트 생성
Android Studio에서 File ⇒ New Project 메뉴로 이동한 뒤, 새 프로젝트를 생성하는 데 필요한 모든 세부 정보를 입력하여 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 앱 이름, 버전 이름, 버전 코드를 표시할 TextView 세 개를 포함하고 있습니다.
<?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:gravity="center_horizontal"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvAppName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvVersionName"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvVersionCode"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>3단계: MainActivity 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다. 핵심은 BuildConfig.VERSION_NAME과 BuildConfig.VERSION_CODE를 통해 빌드 시 자동 생성된 버전 정보를 읽어오는 부분입니다.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvAppName, versionName, versionCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvAppName = findViewById(R.id.tvAppName);
versionName = findViewById(R.id.tvVersionName);
versionCode = findViewById(R.id.tvVersionCode);
tvAppName.setText(getResources().getString(R.string.app_name));
versionName.setText(String.format("Version Name: %s", BuildConfig.VERSION_NAME));
versionCode.setText(String.format("Version Code: %s", String.valueOf(BuildConfig.VERSION_CODE)));
}
}4단계: 매니페스트 파일 설정
AndroidManifest.xml 파일에 아래 코드를 추가합니다.
<?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>실행 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 다음, 툴바의 Run 아이콘을 클릭하세요. 실행 옵션으로 본인의 모바일 기기를 선택하면, 기기 화면에 아래와 같이 앱 이름과 함께 버전 정보가 표시되는 것을 확인할 수 있습니다.
