eclipse 在 Android 应用程序中使用 commonc 编解码器的 NoSuchMethodError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5147789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoSuchMethodError using commonc codec in Android application
提问by DVV
I added the commons codec from apache.org (commons-codec-1.4.jar) in eclipse for my Android application following the instruction here. There is no error in the code. But when I run the application and call the function that use the codec the application stop and need a fore close.
我按照此处的说明在 Eclipse 中为我的 Android 应用程序添加了来自 apache.org (commons-codec-1.4.jar) 的公共编解码器。代码中没有错误。但是当我运行应用程序并调用使用编解码器的函数时,应用程序停止并需要关闭。
In the logCat says:
在 logCat 中说:
Android Runtime: java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String
Android 运行时:java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String
the codeline is: String tmpStr = Base64.encodeBase64String(msg); //msg is a byte[]
代码行是: String tmpStr = Base64.encodeBase64String(msg); //msg是一个字节[]
The application is for a min SDK version = 7 (Android 2.1), so I can't use Android Base64
该应用程序适用于最小 SDK 版本 = 7 (Android 2.1),因此我无法使用Android Base64
Any idea how can I solve the problem?
知道如何解决问题吗?
回答by NickT
I couldn't get the commons codec to run on a 2.1 emulator either. I gave up in the end and made my own, copying the code from:
我也无法让公共编解码器在 2.1 模拟器上运行。最后我放弃了,自己做了,复制代码:
It's only just over a hundred lines or so.
它只有一百多行左右。
回答by zarthross
I experienced the exact same problem. So i started browsing the android source code, and as it turns out Don's guess about Android having an implementation of org.apache.commons.code.binary is correct. he's wrong about the ability to access it, you can, but its version 1.2 of the apache commons, not version 1.4 or even 1.5. You can see for your self in the android source.
我遇到了完全相同的问题。所以我开始浏览 android 源代码,结果证明唐关于 Android 有 org.apache.commons.code.binary 实现的猜测是正确的。他关于访问它的能力是错误的,你可以,但它的 apache 公共资源是 1.2 版,而不是 1.4 版甚至 1.5 版。您可以在 android源代码中自行查看。
Also as a note, this question is a duplicate of this post.
回答by Don
I think it has something to do with the name conflicts inside Android libraries.
我认为这与 Android 库中的名称冲突有关。
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.
我将Base64的java源代码复制到我的项目中到org.apache.commons.codec.binary的命名空间中。项目编译没有任何问题。但是,在 Android 模拟器的运行时,我遇到了同样的错误,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.
但是,在我将命名空间更改为 com.apache.commons.codec.binary 而不是 org 之后,该应用程序运行起来就像魔法一样。我猜在 Android 实现中,使用了 org.apache.commons.codec.binary 并且它不允许您再次在代码中使用它。
回答by Nick Campion
The reason you are having an issue is because those instructions are wrong but there must be a different version of the commons-codec or Base64 class present.
您遇到问题的原因是这些说明是错误的,但必须存在不同版本的 commons-codec 或 Base64 类。
My understanding is that you need to put your jar file into a 'libs' directory at the root of your project to ensure it ends up automatically inside the final apk. Once you put it there, right click the jar file and then go to Build Path-> Add to build path. Should work from there.
我的理解是,您需要将 jar 文件放入项目根目录下的“libs”目录中,以确保它自动在最终 apk 中结束。一旦你把它放在那里,右键单击 jar 文件,然后转到 Build Path-> Add to build path。应该从那里工作。