Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

Commons CodecのBase64クラス使うとCould not find methodが発生する

API Version 4(Android OS 1.6)ではBase64エンコード/デコードするクラスが用意されていないので、Apache CommonsのCommons Codecを使ったところ、見事にハマったのでまとめときます。

現象

Commons CodecにあるBase64クラスを使ったコードをシミュレータ(Android OS 1.6)で実行すると、次のようなエラーが発生する。

ERROR/dalvikvm(574): Could not find method org.apache.commons.codec.binary.Base64.decodeBase64, referenced from method jp.xxxxxx.xxxxxx.xxxxxx

API Version 8(Android OS 2.2)上では、シミュレータ・実機とも問題なく動作する。

原因

android commons Could not find method」でググるといろいろと出てきますが、


I copied the java source code of Base64 to my project into the the namespace of org.apache.commons.codec.binary. The project compiled without any problem. However, at the runtime in Android simulator, I had the same error, java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String.

However, after I changed the namespace to com.apache.commons.codec.binary instead of org, the app runs like magic. I guess inside Android implementation, org.apache.commons.codec.binary is used and it doesn't allow you to use it in your code again.

な辺りが起因してるのか、と。

その他

API Version 8(Android OS 2.2)以降であればandroid.util.Base64クラスが用意されているので、この辺の問題は発生しないハズ。