Terminal IDEでアプリ開発!?(その2)
前回はサンプルのビルドを行ったが、今回は世の中にアップされているオープンライブラリを使用して、簡単なアプリを開発してみようと思う。
実際に使用するのは、テスティングフレームワークで有名なJUnitである。
JARファイルをDEXファイルに変換
Android端末ではJARファイルのオープンライブラリをそのまま使用できないので、DEXファイル(Dalvik仮装マシンのバイトコード)に変換する必要がある。
メモり不足となるので、残念ながらTerminal IDE上では変換できないため、PC上で変換する。
実際の変換はdxコマンドを使用する。
今回使用したAndroid SDK 23.0.2では、dxコマンドは以下に存在する。
ZIP展開先\android-sdk\build-tools\20.0.0\dx.bat
以下のコマンドを実行し、JUnitライブラリをDEXファイルに変換する。
dx --dex --no-srtict --output=junit-4.11.dex.jar junit-4.11.jar dx --dex --no-srtict --output=hamcrest-core-1.3.dex.jar hamcrest-core-1.3.dex.jar
JARファイルとDEXファイルをAndroid端末にコピーする。
コピーしたファイルをTerminal IDEにおいて「~/work/libs/」以下に配置する。
サンプルプログラムの作成
以下のサンプルプログラムを「~/work/src/」に作成する。
- Thing.java
public class Thing { private String name; // 3 characters or over public String getName() { return this.name; } public void setName(String newName) { if (newName == null || newName.length() < 3) { throw new IllegalArgumentException("illegal name"); } this.name = newName; } }
- ThingTest.java
import org.junit.Test; import static org.junit.Assert.*; public class ThingTest { @Test public void instanciate() { Thing thing = new Thing(); assertNull(thing.getName()); } @Test public void setNameOk() { Thing thing = new Thing(); thing.setName("abc"); assertEquals(thing.getName(), "abc"); } @Test public void setNameOk2() { Thing thing = new Thing(); thing.setName("鱗田太郎"); assertEquals(thing.getName(), "鱗田太郎"); } @Test public void setNameNull() { boolean hasError = false; Thing thing = new Thing(); try { thing.setName(null); } catch (IllegalArgumentException e) { hasError = true; } assertEquals(hasError, true); } @Test public void setNameNullString() { boolean hasError = false; Thing thing = new Thing(); try { thing.setName(""); } catch (IllegalArgumentException e) { hasError = true; } assertEquals(hasError, true); } @Test public void setNameTooShort() { boolean hasError = false; Thing thing = new Thing(); try { thing.setName("12"); } catch (IllegalArgumentException e) { hasError = true; } assertEquals(hasError, true); } }
Makefileの作成
makeコマンドでビルド&実行を行うために、以下のMakefileを「~/work/src/」に作成する。
CLASSPATH=../libs/junit-4.11.jar:../libs/hamcrest-core-1.3.jar CLASSFILES=Thing.class ThingTest.class DEXFILE=Thing.dex.jar .SUFFIXES: .class .java .java.class: javac -cp $(CLASSPATH) $< all: $(DEXFILE) run $(DEXFILE): $(CLASSFILES) dx --dex --verbose --no-strict --output=$@ $(CLASSFILES) LIBFILES=../libs/junit-4.11.dex.jar:../libs/hamcrest-core-1.3.dex.jar MAIN=org.junit.runner.JUnitCore ARGS=ThingTest run: java -jar $(LIBFILES):$(DEXFILE) $(MAIN) $(ARGS)
makeコマンドで実行
makeコマンドを実行すると、必要に応じてコンパイルが行われ、ビルドされた単体テストが実行される。
$ cd ~/work/src $ make javac -cp ../libs/junit-4.11.jar:../libs/hamcrest-core-1.3.jar Thing.java javac -cp ../libs/junit-4.11.jar:../libs/hamcrest-core-1.3.jar ThingTest.java dx --dex --verbose --no-strict --output=Thing.dex.jar Thing.class ThingTest.class processing Thing.class... processing ThingTest.class... writing classes.dex; size 1972... java -jar ../libs/junit-4.11.dex.jar:../libs/hamcrest-core-1.3.dex.jar:Thing.dex.jar org.junit.runner.JUnitCore ThingTest JUnit version 4.11 ...... Time: 0.013 OK (6 tests) $