Intelligent Technology's Technical Blog

株式会社インテリジェントテクノロジーの技術情報ブログです。

PhoneGap3系利用時に、はまった事

こんにちは、石尾です。

2か月ほど前、間藤から投稿したPhoneGap Buildの記事の中に、
PhoneGap3.0のインストールの説明もありました。
その説明の中で、アプリケーションのプロジェクト作成(phonegap create実行)時にエラーが発生した説明がありましたが、
石尾の方で、Windows7(64bit)上にPhoneGapのAndroid開発環境を構築した際、
また別の問題が発生しました。

今回は、その問題点と対処方法を紹介したいと思います。

PhoneGapバージョンは、執筆時点の3.2を利用しました。
またインストール手順や環境設定は、下記のPhoneGapサイトを参考にしました。
PhoneGap | Install PhoneGap
PhoneGap API Documentation

1.アプリケーションのビルド実行時

プロジェクトsampleを作成し、そのsampleディレクトリに移動後、アプリケーションのビルドコマンド[phonegap build local android]を実行します。

> phonegap create sample
> cd sample
> phonegap build local android

次のようなエラーが出力されました。

C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\phonegap\build.js:73
    self.phonegap.emit('log', 'detecting', platform.human, 'SDK environment...
                                                   ^
TypeError: Cannot read property 'human' of undefined
    at BuildCommand.execute (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\phonegap\build.js:73:52)
    at BuildCommand.run (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\phonegap\build.js:55:10)
    at PhoneGap.build (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\phonegap\util\command.js:28:25)
    at CLI.module.exports [as build] (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\cli\build.js:35:14)
    at CLI.module.exports [as argv] (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\lib\cli\argv.js:66:17)
    at Object.<anonymous> (C:\Users\ishio\AppData\Roaming\npm\node_modules\phonegap\bin\phonegap.js:24:21)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

原因は、コマンド引数の[build]と[local]を逆に指定していたことが原因です。引数の値をそのまま利用したため、humanプロパティが認識できなかったのでしょう。ただ、この出力結果からは、引数が間違っていることを気付かないかも知れません。
正しい順序の引数でコマンドを実行すると、

> phonegap local build android

次のような出力結果となり、コマンドは成功します。

[phonegap] adding the Android platform...
[phonegap] missing library cordova/android/3.2.0
[phonegap] downloading https://git-wip-us.apache.org/repos/asf?p=cordova-android
.git;a=snapshot;h=3.2.0;sf=tgz...
[phonegap] compiling Android...
[phonegap] successfully compiled Android app

2.アプリケーションのプラグイン追加時

sampleディレクトリに移動後、プラグイン追加コマンド[phonegap local plugin add]を実行します。ここでは、カメラ用プラグインを追加しています。

> cd sample
> phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

次のようなエラーが出力されました。

[phonegap] adding the plugin: https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
   [error] Error fetching plugin: Error: "git" command line tool is not installed: make sure it is accessible on your PATH.

原因は、出力内容とおり、gitコマンドが利用できないことが原因です。
環境変数PATHに、Gitツールパス「C:\Program Files (x86)\Git\bin」を設定します。
結果、次のような出力結果となり、コマンドは成功します。

[phonegap] adding the plugin: https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
[phonegap] successfully added the plugin

※プロキシ設定が必要な環境の場合、次のようなエラーが出力されます。

[phonegap] adding the plugin: https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
   [error] Error fetching plugin: Error: failed to get the plugin via git from URL https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git, output: Cloning into 'plugman-tmp1384507816624'...
fatal: unable to access 'https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git/': Clold not resolve host: git-wip-us.apache.org

次のコマンドを実行して、Gitのプロキシ設定をしてください。

> git config --global http.proxy http://プロキシサーバ:ポート番号
> git config --global https.proxy https://プロキシサーバ:ポート番号

このコマンドで設定していない場合、環境変数HTTP_PROXY、HTTPS_PROXYが利用されます。また、次のコマンドを実行しておくと、「git://」ではなくて「https://」でアクセスするようになります。

> git config --global url."https://".insteadOf git://

最後に

間藤の記事から2ヶ月で、PhoneGapのバージョンは、3.0から3.2にアップされています。バージョンアップにより改良されるのはよいですが、ドキュメントに関しても、どのツールが必要など分かりやすくなると、PhoneGap利用者は増えるのではないでしょうか。