AndroidでPythonを動かす方法

SL4AのProcessクラスがやれスレッドだの、Proxyだの、Serviceだので面倒だったのでバッサリ切って使ってみる


前回同様、libs/script.jarを自分のプロジェクトにコピーして、Eclipseでビルドパスに追加
ほんで libs/armeabi/libcom_googlecode_android_scripting_Exec.so をコピー
後はソース書くだけ

public InterpreterConfiguration conf_;
conf_ = new InterpreterConfiguration(this);
conf_.startDiscovering();

File source = new File("/var/www/public_html/cgi-bin/hello.py");
Interpreter script = conf_.getInterpreterForScript(source.getName());

HashMap<String, String> env = new HashMap<String, String>();
env.putAll(System.getenv());
env.putAll(script.getEnvironmentVariables());
List<String> envs = new ArrayList<String>();
for(Entry<String, String> entry : env.entrySet())
{
	envs.add(entry.getKey() + "=" + entry.getValue());
}

int[] pid = new int[1];
FileDescriptor fd = Exec.createSubprocess(
	script.getBinary().getAbsolutePath(),
	new String[] {String.format(script.getScriptCommand(), source.getAbsolutePath())},
	envs.toArray(new String[envs.size()]),
	null,
	pid);
Exec.waitFor(pid[0]);

プロセス起動っぽいソースで普通に書けるんだけど、script.getEnvironmentVariables こいつ
こいつを環境変数にセットしないと .so が見つからへんどと怒られる
ほんで問題点が二つ


SL4AでやっているAndroid系のAPIが全く使えない
あれはAP_HOSTとAP_PORTという環境変数で接続先サーバを指定してサーバ側に処理をさせるものだから
普通AP_HOSTをlocalhostにし、JSONでデータを連携してサーバ側をコントロールするっぽい
今回WebサーバにCGIとして組み込みたいので必要ないからはしょった
Webサーバにアクセスするとカメラの画像が見れる定点観測的なものが作りたくなったらなんかするかも


もういっこがよく分からなくて
fdから標準出力を読むとなんでかこうなる

print "Hello Python"

#=>
dlopen libpython2.6.so
Hello Python

なんでやねん
だれやdlopenなんて出力に吐いたん・・・もしかしたら標準エラーに吐いたんかもしれんけど
FileDescriptorを標準出力と標準エラーに分ける方法が分からない
素人には分からんわ