2010年4月11日日曜日

Android-EclipsePluginのセットアップ


下記環境でAndroid開発環境をセットアップしてみようかと思います。
1. Eclipse
2. JDK1.6
3. OS Ubunts

下記を参考にしてみます。

Download the Android SDK


In particular, you may need to install the JDK (version 5 or 6 required) and Eclipse (version 3.4 or 3.5, needed only if you want develop using the ADT Plugin).


JDKとEclipseは入っているのでこのままで大丈夫そうです。


2. Download and install the SDK starter package


Select a starter package from the table at the top of this page
and download it to your development computer. To install the SDK,
simply unpack the starter package to a safe location and then add
the location to your PATH.


Eclipseの設定の前にAndroid SDKを設定しないといけなさそうです。下記のページを参考に設定してみます。

Installing the SDK


Optionally, you may want to add the location of the SDK's primary tools directory to your system PATH. The primary tools/ directory is located at the root of the SDK folder. Adding tools to your path lets you run Android Debug Bridge (adb) and the other command line tools without needing to supply the full path to the tools directory.


* On Linux, edit your ~/.bash_profile or ~/.bashrc file. Look for a line that sets the PATH environment variable and add the full path to the tools/ directory to it. If you don't see a line setting the path, you can add one:
export PATH=${PATH}:/tools


どうやらダウンロードしたSDKを展開後、bashのパス設定に展開したディレクトリを含めればいいようです。android-sdk_r05-linux_86.tgzをダウンロードして、自分のホームディレクトリに展開してみました。~/.bashrcに下記のように設定しておきます


export PATH=${PATH}:~/android-sdk-linux_86/tools


このSDKの説明に大事な事が書いてありまして、下記の用に実行するとソフトウェアアップデートがかかるみたいです。Googleの最新API用開発セットも含まれているようなのでせっかくなので全部入れておきます。もしかしてGoogle API アドオンに関係あるんでしょうか?


android update sdk


では、Eclipseを起動し直して、下記を参考にプラグインをインストールします。

ADT Plugin for Eclipse



1. Start Eclipse, then select Help > Install New Software.
2. In the Available Software dialog, click Add....
3. In the Add Site dialog that appears, enter a name for the remote site (for example, "Android Plugin") in the "Name" field.

In the "Location" field, enter this URL:

https://dl-ssl.google.com/android/eclipse/

Note: If you have trouble acquiring the plugin, you can try using "http" in the URL, instead of "https" (https is preferred for security reasons).

Click OK.
4. Back in the Available Software view, you should now see "Developer Tools" added to the list. Select the checkbox next to Developer Tools, which will automatically select the nested tools Android DDMS and Android Development Tools. Click Next.
5. In the resulting Install Details dialog, the Android DDMS and Android Development Tools features are listed. Click Next to read and accept the license agreement and install any dependencies, then click Finish.
6. Restart Eclipse.


このあとEclipseで使えるようにするには「Window->preferences->android」でAndroid SDKのパスを入力しないといけないようです。(今回の場合~/android-sdk-linux_86)

インストールもうまくいったので早速Hello, Worldを参考にプログラムを作ってみます。

まず事前にエミュレータの設定をする必要があるようです。


:~$ android create avd --target 2 --name my_avd
Android 1.5 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD 'my_avd' based on Android 1.5


もう一度Eclipseを起動しなおして、プロジェクトを作ります。詳しくはHello, Worldを参照の事。

ここで気になるのはビルドターゲットと最低SDKのバージョンの関係。下記は大事そうなのでメモをしておく。


ここで、選択した「ビルド ターゲット」で Android 1.1 プラットフォームが使用されることに注目してください。これは、作成するアプリケーションが Android 1.1 プラットフォームライブラリをターゲットとしてコンパイルされることを意味します。先ほど作成した AVD は Android 1.5 プラットフォームで実行されます。バージョンの数字が一致しませんが、Android アプリケーションには上方互換性があるため、1.1 プラットフォーム ライブラリをターゲットとして構築されたアプリケーションでも 1.5 プラットフォームで正常に動作します。ただしその逆の場合は正常に動作しません。


下記はサンプルのソースコードです。


package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}


コンパイルして実行してみます。どうもエミュレータ起動までものすごく時間がかかるようで計測したら1分15秒かかりました。
ここからがかなり大事なのですがAndroidの開発をEclipseでやる場合エミュレータの再起動は必要ありません。修正後、再コンパイルして実行すればエミュレータに最新のプログラムがインストールされます。エミュレータから操作してアプリを再起動させると反映されています。

0 件のコメント:

コメントを投稿