Apache Commons Codec with Android: Could Not Find Method

Apache Commons Codec with Android: could not find method

I had a similar problem while using android with an OAuth library I'm developing.

I also got from android that, although I had included apache.commons.codec in the classpath, a particular method (encodeBase64String) was not found.

Checking the javadocs, both methods claim to be 1.4 and greater only, so my guess is that android already includes an older version of commons.codec where these methods are indeed undefined.

My solution was to use an older method, like this:

String encodedString = new String(Base64.encodeBase64('string to encode'));

The method you want to use is different since it replaces + and / with url-safe values - and _. So you probably might use something like:

String encodedString = new String(Base64.encodeBase64('string to encode'));
String safeString = encodedString.replace('+','-').replace('/','_');

Hope that helps!

Method not found using DigestUtils in Android

I ran into the same issue trying to use DigestUtils in my Android app. This was the best answer I could find by searching, but I was reluctant to rebuild the .jar file with the namespace changed. After spending some time on this issue, I found an easier way to solve the problem for my case. The problem statement for my code was

String s = DigestUtils.md5Hex(data);

Replace this statement with the following and it will work:

String s = new String(Hex.encodeHex(DigestUtils.md5(data)));

Similarly, for shaHex exampl, you can change it to

String hash = new String(Hex.encodeHex(DigestUtils.sha("textToHash")));

This works because even though Android does not have encodeHexString(), it does have encodeHex(). Hope this would help others who run into the same issue.

Android cannot find method from JAR on runtime

Use Gradle dependencies, not a .jar:

dependencies {
compile 'commons-codec:commons-codec:1.10'
}

See: http://mvnrepository.com/artifact/commons-codec/commons-codec/1.10

Also, I see your are trying to use org.apache.commons.codec.binary.Base64.decodeBase6. Android has it's own Base64 library:

http://developer.android.com/reference/android/util/Base64.html

decodeHex method not found on Device, but working on Emulator

Some Android versions contain an older version of the Apache commons-codec library (1.3) where the decodeHex(String) method didn't yet exist. Try calling decodeHex(char[]) instead. I.e. modify your code like this:

byte[] decryptedBytes = cipher.doFinal(Hex.decodeHex(message.toCharArray()));

That should work with commons-codec v1.3.

How to resolve a library conflict (apache commons-codec)

Late reply but maybe usefull for someone.

Problem solved by using Maven Shade Plugin

This plugin allows to rename package names of conflicted library at compilation.

Usage :

   <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>com.example.shaded.org.apache.commons</shadedPattern>
</relocation>
</relocations>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
</configuration>
</execution>
</executions>
</plugin>


Related Topics



Leave a reply



Submit