Search code examples
androidgradleapache-commonsandroid-build

StringUtils.rightPad () in apache throws NoSuchMethodError in android


I've included

in app gradle

compile 'org.apache.commons:commons-lang3:3.0' 

in progaurd I included

-keep class org.apache.commons.lang3.StringUtils*

but when app tries to access the StringUtils.rightPad(). it throws an exception saying

java.lang.NoSuchMethodError: org.apache.commons.lang3.StringUtils.a

Related code

 public static String getSignedData(String data) {
        int blockSize = 64;
        byte[] key = StringUtils.rightPad(ApplicationConst.getCheckoutAccessKey(), blockSize, (char) 0x00).getBytes();
        byte[] iPad = StringUtils.repeat((char) 0x36, blockSize).getBytes();
        byte[] oPad = StringUtils.repeat((char) 0x5c, blockSize).getBytes();
        byte[] stage1 = hashMessage(concatenate(xor(key, iPad), data.getBytes()));
        byte[] stageFinal = hashMessage(concatenate(xor(key, oPad), stage1));
        return bytesToHex(stageFinal);
 }

Solution

  • Modify your proguard script and add below lines.

    -keep class org.apache.commons.lang3.StringUtils { *; }
    -keep public class org.apache.commons.lang3.StringUtils { public protected static *; }
    -keepclassmembers public class org.apache.commons.lang3.StringUtils { *; }
    -keepnames public class org.apache.commons.lang3.StringUtils { *; }