Androidプロジェクトの構成
先週はAndroid Studioを使ってプロジェクトを作ってみました。今週はプロジェクトにどのようなファイルがあるかを見ていきましょう。
先週作ったプロジェクトをAndroid Studioで開いたら、まず、Projectウィンドウの表示を変更します。初期値は「Android」になっているので、これを「Project」に変更します。すると表示がフォルダのツリー構造になります。

アプリ本体に関するファイルはappフォルダの中にあります。src/mainと掘り下げていきましょう。

AndroidManifest.xml
src/mainフォルダの中にAndriodManifest.xmlというファイルがあります。このファイルには「アプリ名は何か」「アプリにはどんなコンポーネントが含まれているか」といった、アプリに関する情報が記述されています。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mokelab.demo.myapplication">
<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>
MainActivity.kt
src/main/javaフォルダの中にMainActivity.ktというファイルがあります。これはKotlinソースファイルです。
package com.mokelab.demo.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
リソースフォルダ
続いて、src/main/resフォルダの中を見てみましょう。ここにはアプリで使用するリソースファイルが入っています。

res/mipmap-xxxx
mipmapで始まるフォルダにはホーム画面などに表示されるアプリアイコンが入っています。-xxxxの部分はリソース修飾子と呼びます。詳しい説明は次週以降に詳しく説明するので、お楽しみに。

res/layout
ここには画面レイアウトのためのXMLファイル(レイアウトXMLと呼びます)が入っています。1画面につき1つ以上のXMLファイルを作ります。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
res/values
ここにはアプリで使用する文字列データや、テーマに関するファイルが入っています。

res/drawable
ここにはアプリで使う画像ファイルや、画像を作るためのXMLファイルなどが入っています。
app直下
続いてappフォルダ直下にあるファイルについて説明します。
app/build.gradle
これは、アプリをビルドするときに使用する設定ファイルです。AndroidアプリはGradleというツールを使ってビルドするため、拡張子が.gradleになっています。build.gradleファイルはappフォルダだけでなく、プロジェクト直下にもあるので注意しましょう。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mokelab.demo.myapplication"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
プロジェクト直下
プロジェクトフォルダの直下にもいくつかファイルがあります。
gradle.properties
Gradle本体に対する設定ファイルです。例えばGradleがどれだけメモリを使用するかといった設定を記述します。
gradlew / gradlew.bat
Gradleラッパーと呼ばれるスクリプトファイルです。「Gradleコマンドがインストールされていなかった場合、ダウンロードしてから実行する」といったスクリプトになっており、実際のアプリビルド時にはこれらが使用されます(Windows環境の場合はgradlew.batが使用されます)。
local.properties
Gradle本体に対する設定のうち、マシン固有の設定を記述するファイルです。例えばAndroid SDKがどこにインストールされているかなどを記述します。このファイルはgitのリポジトリに含めないようにしましょう(デフォルトで.gitignoreされています)。
settings.gradle
プロジェクトにどのようなモジュールが含まれているかを記述します。通常であればappモジュールだけが記述されています。
include ':app'
rootProject.name='My Application'
まとめ
Androidプロジェクトのファイル構成について簡単に説明しました。どのような種類のファイルがどのあたりに配置されているかを覚えておくとよいでしょう。
次回までの宿題
AndroidManifest.xmlがどんな構造になっているかを読んでみましょう。