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

nbe_42 picture nbe_42 · Sep 5, 2012 · Viewed 10.5k times · Source

I have a problem with Android libraries.

I would like use the method Hex.encodeHexString(Byte Array) from the library org.apache.commons.codec.binary.Hex (version 1.6)

On my Android platform (SDK 2.3.1), the commons-codec library version 1.3 already exist but the method doesn't exist yet in this version (only encodeHex() ).

I added the jar library of version 1.6 into my Eclipse project (into /libs directory) but when I run the project on Emulator, I get this :

E/AndroidRuntime(1632): FATAL EXCEPTION: main
E/AndroidRuntime(1632): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Hex.encodeHexString

How can I indicate the OS where is the good library?

I'm using Eclipse Juno with Java 1.6.0 on Mac OS X

Sorry for my bad english and thanks in advance!

EDIT : My problem could be apparently solved with jarjar tool. http://code.google.com/p/google-http-java-client/issues/detail?id=75

Someone could help me with this tool? I don't know how to create an Ant Manifest or a jar file.

Thanks

Answer

nbe_42 picture nbe_42 · Jun 4, 2013

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>