Androidプロジェクトのディレクトリ構成
今回は環境構築編で作成したhelloworldプロジェクトのディレクトリ構成について言及していきます。
まず、eclipseのパッケージエクスプローラでhelloworldプロジェクトを展開してみると以下のようになります。

では、このディレクトリ構成をざっと見ていきます。
srcディレクトリ
srcディレクトリはそのままですね。javaファイルが格納されます。
今回で言えば、helloworld.java及びR.javaがあります。
- jp.demo.helloworld.helloworld.java
helloworldのメインクラスです。
helloworldクラスはActivityクラスを継承していますが、
このActivityクラスについては次回詳しく見ていきます。
package jp.demo.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class helloworld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Activityの起動時に呼ばれるonCreate()メソッドで、Rクラスを介して
main.xmlのレイアウト情報を元に画面を設定しています。
リソースファイルにindexを付加しているクラスです。
これはコンパイラによって自動生成・自動更新されるファイルなので、
手動で変更してはいけません。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package jp.demo.helloworld;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
上記ソースコードを見てもらうとデフォルトではdrawable/icon.png、layout/main.xml、string.xmlにindexが張られていることが分かるかと思います。
assetsディレクトリ
このassetsディレクトリには動画などのストリームで扱うようなファイルを配置します。
詳しいことはよくわからないのですが、assetsディレクトリとresディレクトリについては、
以下のページにて詳しく書かれています。
恐らくresディレクトリで扱えないようなファイルを扱う場合はassetsディレクトリ配下に置くのだと思います。
http://developer.android.com/guide/topics/resources/index.html
resディレクトリ
リソースファイルを管理するディレクトリです。
新しいリソースを追加する場合はリソースを追加した後にプロジェクトを再読み込みすればR.javaが更新されます。
- res/drawable
- res/layout
イメージリソースを配置するディレクトリです。推奨フォーマットはpngです。
アプリのアイコンとなるicon.pngが配置されています。
画面のレイアウトを定義するためのxmlファイルを配置するディレクトリです。
ここにはmain.xmlが配置されており、中身は以下のようになっています。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
LinerLayoutはhtmlでいうdivタグやpタグのようなもので、レイアウト制御するようなクラスです。TextViewはテキスト表示用のクラスで、android:text属性で、”@string/hello”を指定していますが、これはstring.xmlのhelloという変数のことです。
文字列定義を行うためのxmlファイルを配置するディレクトリです。
ここにはstring.xmlが配置されており、中身は以下のようになっています。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, helloworld</string>
<string name="app_name">helloworld</string>
</resources>
layout.xmlから参照されているhelloは「Hello World, helloworld」という文字列になっていることが分かると思います。
AndroidManifest.xml
アプリケーションの概略を記述するファイルです。
Androidでは、外部リソース(電話帳やインターネットなど)を仕様するとき、
パーミッションを指定しなければなりませんが、
そういった情報もこのファイルに記述されます。
その他メインアクティビティやintent-filter等も記述可能です。
中身は以下のようになっています。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.demo.helloworld"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".helloworld"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
“@drawable/icon”(drawable/icon.png)や”@string/app_name”(string.xmlのapp_name変数)が指定されています。
これらのファイルのつながりを元に起動時の動きを確認すると、次のようになります。
helloworldを立ち上げると、onCreate()メソッドにてlayout/main.xmlに基づいて画面が設定され、このときmain.xml内のTextViewのtextでstring.xmlのhello変数が指定されているために画面に「Hello World, helloworld」と表示される。
これで大まかな全体像が掴めたかと思います。