Java ベースの Android アプリ開発を学ぶための授業用テキストです。 このテンプレートプロジェクトを通じて、Android アプリの基本構成を理解しましょう。
このテキストは以下の知識・環境を持つ学生を対象としています。
必要な知識:
- Java の基本文法(変数、データ型、演算子)
- 条件分岐(if 文、switch 文)とループ処理(for 文、while 文)
- クラスとオブジェクトの概念
- メソッドの定義と呼び出し
- 継承の基本的な理解
必要な環境:
- Android Studio がインストールされていること
- Android エミュレータまたは実機(Android 11 以上)が利用可能であること
- インターネット接続(ライブラリのダウンロードに必要)
このテキストでは、Android Studio を使用して Java ベースの Android アプリを開発する基礎を学びます。 「Hello World!」を表示するシンプルなアプリを題材に、Android アプリの構成要素を理解しましょう。
| 項目 | バージョン |
|---|---|
| Android Studio | Panda 1(2025.3.1)以降 |
| JDK | 11 |
| 最小 SDK | 30(Android 11) |
| ターゲット SDK | 36(Android 16) |
- Android アプリのプロジェクト構成を理解する
- Activity の役割とライフサイクルを理解する
- XML レイアウトの基本を理解する
- アプリのビルドと実行ができるようになる
JecAndroidJavaTemplate/
├── app/ # アプリモジュール
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/ # Java ソースコード
│ │ │ │ └── jp/ac/jec/cm0199/jecandroidjavatemplate/
│ │ │ │ └── MainActivity.java
│ │ │ ├── res/ # リソースファイル
│ │ │ │ ├── layout/ # レイアウト XML
│ │ │ │ ├── values/ # 文字列・色・テーマ
│ │ │ │ ├── drawable/ # 画像・図形
│ │ │ │ └── mipmap/ # アプリアイコン
│ │ │ └── AndroidManifest.xml
│ │ ├── test/ # 単体テスト
│ │ └── androidTest/ # 計測テスト
│ └── build.gradle.kts # アプリのビルド設定
├── gradle/
│ └── libs.versions.toml # 依存ライブラリのバージョン管理
├── build.gradle.kts # プロジェクト全体のビルド設定
└── settings.gradle.kts # プロジェクト設定
| ファイル | 役割 |
|---|---|
MainActivity.java |
アプリのメイン画面を制御する Activity |
activity_main.xml |
メイン画面のレイアウト定義 |
AndroidManifest.xml |
アプリの基本情報と構成を宣言 |
strings.xml |
文字列リソース(多言語対応用) |
build.gradle.kts |
ビルド設定と依存ライブラリ |
package jp.ac.jec.cm0199.jecandroidjavatemplate;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}package jp.ac.jec.cm0199.jecandroidjavatemplate;- Java クラスが属するパッケージを宣言します
- パッケージ名はアプリを一意に識別するための ID となります
- 慣例として逆ドメイン形式(例:
jp.ac.jec.xxx)を使用します
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;| クラス | 役割 |
|---|---|
Bundle |
Activity 間のデータ受け渡しや状態保存に使用 |
EdgeToEdge |
画面端までコンテンツを表示する機能 |
AppCompatActivity |
後方互換性のある Activity 基底クラス |
Insets |
画面の余白情報を保持 |
ViewCompat |
View 操作の互換性ヘルパー |
WindowInsetsCompat |
システムバーの余白情報 |
public class MainActivity extends AppCompatActivity {MainActivityはAppCompatActivityを継承していますAppCompatActivityを使うことで、古い Android バージョンでも新しい機能が使えます
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
}- Activity が作成されたときに呼ばれるメソッドです
@Overrideは親クラスのメソッドを上書きすることを示しますsuper.onCreate()で親クラスの初期化処理を実行します
EdgeToEdge.enable(this);- 画面の端(ステータスバーやナビゲーションバーの下)までコンテンツを表示します
- モダンな Android アプリのデザインに対応します
setContentView(R.layout.activity_main);res/layout/activity_main.xmlをこの Activity の画面として設定しますRクラスはリソースへの参照を自動生成したクラスです
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});- EdgeToEdge を有効にした際、コンテンツがシステムバーと重ならないよう余白を設定します
findViewById()で XML 内の要素を取得します- ラムダ式
(v, insets) -> { ... }でリスナーを簡潔に記述しています
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout><?xml version="1.0" encoding="utf-8"?>- XML ファイルであることと文字エンコーディングを宣言します
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"| 名前空間 | 用途 |
|---|---|
android: |
Android 標準の属性 |
app: |
ライブラリ固有の属性 |
tools: |
開発時のみ使用する属性(実行時は無視) |
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">| 属性 | 値 | 説明 |
|---|---|---|
android:id |
@+id/main |
この要素の識別子(@+id/ は新規作成) |
android:layout_width |
match_parent |
親要素の幅いっぱいに広がる |
android:layout_height |
match_parent |
親要素の高さいっぱいに広がる |
android:orientation |
vertical |
子要素を縦方向に並べる |
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />| 属性 | 値 | 説明 |
|---|---|---|
android:layout_width |
wrap_content |
テキストの幅に合わせる |
android:layout_height |
wrap_content |
テキストの高さに合わせる |
android:text |
Hello World! |
表示するテキスト |
| 値 | 説明 |
|---|---|
match_parent |
親要素のサイズに合わせる |
wrap_content |
コンテンツのサイズに合わせる |
100dp |
固定サイズ(dp: density-independent pixels) |
res/values/strings.xml にアプリで使用する文字列を定義します。
<resources>
<string name="app_name">JecAndroidJavaTemplate</string>
</resources>文字列リソースを使う理由:
- 多言語対応が容易になる
- 文字列の一元管理ができる
- コード内にハードコードしない
使用方法:
- XML 内:
@string/app_name - Java 内:
getString(R.string.app_name)
res/values/colors.xml に色を定義します。
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>色の形式: #AARRGGBB(AA: 透明度, RR: 赤, GG: 緑, BB: 青)
アプリの基本情報を宣言するファイルです。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.JecAndroidJavaTemplate">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>| 要素/属性 | 説明 |
|---|---|
<application> |
アプリ全体の設定 |
android:icon |
アプリアイコン |
android:label |
アプリ名 |
android:theme |
アプリのテーマ |
<activity> |
画面(Activity)の宣言 |
android:exported="true" |
外部からの起動を許可 |
intent-filter |
Activity の起動条件 |
action.MAIN |
メインエントリーポイント |
category.LAUNCHER |
ランチャーに表示 |
- Android Studio でプロジェクトを開く
- エミュレータまたは実機を接続
- Run ボタン(緑の三角)をクリック
# デバッグ APK を生成
./gradlew assembleDebug
# 接続端末にインストール
./gradlew installDebug
# ユニットテストを実行
./gradlew test
# Android Lint を実行
./gradlew lintビルドした APK は以下に出力されます:
app/build/outputs/apk/debug/app-debug.apk
activity_main.xml の TextView を編集して、表示されるテキストを「こんにちは!」に変更してください。
解答例
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="こんにちは!" />strings.xmlに新しい文字列リソースを追加- TextView で
@string/を使って参照
解答例
strings.xml:
<resources>
<string name="app_name">JecAndroidJavaTemplate</string>
<string name="greeting">こんにちは!</string>
</resources>activity_main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/greeting" />LinearLayout 内に 3 つの TextView を追加し、それぞれ異なるテキストを表示してください。
解答例
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1行目" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2行目" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3行目" />
</LinearLayout>- TextView に
android:idを設定 - MainActivity.java で
findViewById()を使って TextView を取得 setText()メソッドでテキストを変更
解答例
activity_main.xml:
<TextView
android:id="@+id/text_greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
// TextView を取得してテキストを変更
TextView textGreeting = findViewById(R.id.text_greeting);
textGreeting.setText("Java から変更しました!");
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}※ import android.widget.TextView; の追加が必要です。
このテキストでは以下を学びました:
- プロジェクト構成: Android アプリの基本的なディレクトリ構造
- Activity: 画面を制御するクラスと onCreate ライフサイクル
- レイアウト XML: LinearLayout と TextView の基本
- リソース: 文字列・色の管理方法
- ビルド: アプリの実行方法
次のステップとして、ボタンの追加やユーザー入力の処理を学んでいきましょう。