【Android開発】アプリのビルドバージョン(バージョン名・バージョンコード)を取得する方法
この記事では、Androidアプリのビルドバージョン情報(アプリ名・バージョン名・バージョンコード)を取得して画面に表示する方法を、サンプルコードとともにわかりやすく解説します。
手順1:新規プロジェクトの作成
まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」⇒「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成してください。
手順2:レイアウトファイル(activity_main.xml)の編集
次に、res/layout/activity_main.xml に以下のコードを追加します。ここでは、アプリ名・バージョン名・バージョンコードをそれぞれ表示する3つの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.javaの編集
src/MainActivity.java に以下のコードを追加します。BuildConfig クラスの VERSION_NAME と 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の設定
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端末をパソコンに接続しているものとします。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの実行アイコンをクリックします。候補が表示されたら接続中のモバイルデバイスを選択してください。端末には以下のように、アプリ名・バージョン名・バージョンコードが表示された画面が現れます。

補足:PackageManagerを使って動的に取得する方法
BuildConfig を使う方法以外にも、PackageManager 経由で PackageInfo からバージョン情報を取得する方法があります。こちらは実行時に動的に情報を取得できるため、ライブラリやSDKの開発などでも広く利用されています。なお、API 28以降では versionCode の代わりに getLongVersionCode() の使用が推奨されています。
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
String versionName = info.versionName;
long versionCode = info.getLongVersionCode(); // API 28以降
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
用途に応じて両者の使い分けを行うことで、アプリのバージョン管理やアップデート通知などの機能を柔軟に実装できます。
-
【Android開発】プログラムから現在のSDKバージョン(APIレベル)を取得する方法
この記事では、Androidアプリ内から現在のSDKバージョン(APIレベル)をプログラムで取得する方法を、実際のサンプルコードとともにわかりやすく解説します。使用するクラス:android.os.BuildAndroidでは、android.os.Build.VERSION クラスを使うことで、端末のOS情報を簡単に取得できます。主に以下の2つのフィールドを使用します。Build.VERSION.SDK_INT … APIレベルを整数値で返します(例:33)Build.VERSION.RELEASE … OSバージョンを文字列で返します(例:「13」)手順1:新規プロジェクトの作成まず、An
-
【Android開発】システムバージョン(OSバージョン・APIレベル)を確認・表示する方法
Androidのシステムバージョンをプログラムで確認する方法 この記事では、Androidアプリ内で端末のシステムバージョン(OSバージョン・コードネーム・APIレベル)を取得し、画面に表示する方法を、実際のコード例とともにステップごとに解説します。バージョンによって処理を分岐させたい場合などに役立つテクニックです。 手順1:Android Studioで新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目(プロジェクト名、パッケージ名、保存先など)を入力して、新しいプロジェクトを作成してください。