Aditya Jaitly
Aditya Jaitly

Reputation: 292

App Crashes on KitKat Devices Caused by java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets

  Caused by java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets
   common.HMACSha1Util.sha1 + 66(HMACSha1Util.java:66)
   network.NetHTTPConnection.postSecureRequest + 122(NetHTTPConnection.java:122)
   NetHTTPConnection.httpPostRequest + 62(NetHTTPConnection.java:62)
   network.AppNetworkController$1.onBackgoundProcess + 68(AppNetworkController.java:68)
   at network.AppNetworkController$AsyncRequest.doInBackground + 137(AppNetworkController.java:137)
   at network.AppNetworkController$AsyncRequest.doInBackground + 125(AppNetworkController.java:125)
   at android.os.AsyncTask$2.call + 287(AsyncTask.java:287)
   at java.util.concurrent.FutureTask.run + 234(FutureTask.java:234)
   at android.os.AsyncTask$SerialExecutor$1.run + 230(AsyncTask.java:230)
   at java.util.concurrent.ThreadPoolExecutor.runWorker + 1080(ThreadPoolExecutor.java:1080)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run + 573(ThreadPoolExecutor.java:573)
   at java.lang.Thread.run + 841(Thread.java:841)

Code I use where it occurring

      public static String sha1(Map<String,String> nvps) {

        try {
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = AppProperties.VERIFICATION_KEY
                .getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(getStringFromMap(nvps)
                .getBytes());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        // Covert array of Hex bytes to a String
        return new String(hexBytes, StandardCharsets.UTF_8);
    } catch (Exception e) {

    }
}

Error occuring on return new String(hexBytes, StandardCharsets.UTF_8);

I know StandardCharset is not available in Kikat but need help how can i change it should work kitkat device

Upvotes: 1

Views: 462

Answers (1)

Droidman
Droidman

Reputation: 11608

To avoid StandardCharsets use

new String(hexBytes, Charset.forName("UTF-8"));

Upvotes: 3

Related Questions