読者です 読者をやめる 読者になる 読者になる

うさがにっき

読書感想文とプログラムのこと書いてきます

antでビルドを自動化する

概要

Androidビルドをantで自動化する

詳細

Androidビルドをantで自動化することにより、ビルド、リリース時のエラーを無くす
android project直下で以下コマンドを入力し、プロジェクトにantビルド用ファイルを作成する

$ android update project --path ./
Updated local.properties
No project name specified, using Activity name 'MainActivity'.
If you wish to change it, edit the first line of build.xml.
Added file ./build.xml
Updated file ./proguard-project.txt
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.

これによりbuild.xmlとlocal.propertiesが作成される(proguard関係も修正される様子)

$ ls -lt
total 48
-rw-r--r--   1 test  jenkins  3924  7 12 13:42 build.xml
-rwxr-xr-x   1 test  jenkins   781  7 12 13:42 proguard-project.txt
-rw-r--r--   1 test  jenkins   426  7 12 13:41 local.properties
drwxr-xr-x  10 test  jenkins   340  6 22 18:04 bin
drwxr-xr-x   3 test  jenkins   102  6 22 15:34 gen
-rwxr-xr-x   1 test  jenkins   563  6 22 15:34 project.properties
drwxr-xr-x   2 test  jenkins    68  6 22 15:34 assets
drwxr-xr-x   3 test  jenkins   102  6 22 15:34 libs
drwxr-xr-x  11 test  jenkins   374  6 22 15:34 res
drwxr-xr-x   3 test  jenkins   102  6 22 15:34 src
-rwxr-xr-x   1 test  jenkins  1773  3 25 10:32 build.gradle
-rwxr-xr-x   1 test  jenkins   923  3 20 12:51 AndroidManifest.xml

local.propertiesの中にはandroidsdkの場所が記載されている
修正するなとファイルに書いてある

$ cat local.properties 
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Applications/android-sdk-macosx

リリース用apkに使う証明書を配置する
プロジェクト直下にkeystoreフォルダを作成し、そこに証明書ファイルを配置する
※今回はeclipseデバッグキーをそのまま使う

$ mkdir keystore
$ cd keystore/
$ ls
debug.keystore

ビルド時に使うプロパティとなるant.propertiesを作成する

$ cat ant.properties 
key.store=keystore/debug.keystore
key.alias=androiddebugkey
key.store.password=android
key.alias.password=android
sdk.dir=Applications/android-sdk-macosx

build.xmlを編集すれば、各環境ごとにビルドを設定できる

$ ant
Buildfile: build.xml

help:
     [echo] Android Ant Build. Available targets:
     [echo]    help:      Displays this help.
     [echo]    clean:     Removes output files created by other targets.
     [echo]               This calls the same target on all dependent projects.
     [echo]               Use 'ant nodeps clean' to only clean the local project
     [echo]    debug:     Builds the application and signs it with a debug key.
     [echo]               The 'nodeps' target can be used to only build the
     [echo]               current project and ignore the libraries using:
     [echo]               'ant nodeps debug'
     [echo]    release:   Builds the application. The generated apk file must be
     [echo]               signed before it is published.
     [echo]               The 'nodeps' target can be used to only build the
     [echo]               current project and ignore the libraries using:
     [echo]               'ant nodeps release'
     [echo]    instrument:Builds an instrumented package and signs it with a
     [echo]               debug key.
     [echo]    test:      Runs the tests. Project must be a test project and
     [echo]               must have been built. Typical usage would be:
     [echo]                   ant [emma] debug install test
     [echo]    emma:      Transiently enables code coverage for subsequent
     [echo]               targets.
     [echo]    install:   Installs the newly build package. Must either be used
     [echo]               in conjunction with a build target (debug/release/
     [echo]               instrument) or with the proper suffix indicating
     [echo]               which package to install (see below).
     [echo]               If the application was previously installed, the
     [echo]               application is reinstalled if the signature matches.
     [echo]    installd:  Installs (only) the debug package.
     [echo]    installr:  Installs (only) the release package.
     [echo]    installi:  Installs (only) the instrumented package.
     [echo]    installt:  Installs (only) the test and tested packages (unless
     [echo]               nodeps is used as well.
     [echo]    uninstall: Uninstalls the application from a running emulator or
     [echo]               device. Also uninstall tested package if applicable
     [echo]               unless 'nodeps' is used as well.

BUILD SUCCESSFUL
Total time: 0 seconds

参考

Signing Your Applications | Android Developers

良いAndroidアプリを作る139の鉄則

良いAndroidアプリを作る139の鉄則