Search code examples
androidkeystoreandroid-keystorejks

Cannot sign an APK with Android Studio - Getting exception: DerInputStream.getlength()


I just inherited an old project, which created back in 2013.

The Keystore is probably created with jdk 7.

When I try to generate the APK in Android studio, an exception is thrown:
Cannot load key store: Unable to initialize, java.io.IOException: DerInputStream.getLength(): Redundant length bytes found

enter image description here

What I tried is:

  • Convert the Keystore to pk12 - Here I'm getting the same exception as above
  • Open the keystore in KeyStore Explorer - Not working either, I'm unable to view the key
  • I also tried to contact google (play store support), but they respond with "our team isn’t trained to provide technical support for key."

Related posts:
Android keystore corrupted
Unable to open keystore in AndroidStudio - "Redundant length bytes found"

I have no idea what to do next.

The only thing that works is by manually sign an unsigned apk in terminal.
This solution is not optimal. Furthermore, it only works when using an old jdk version (jdk1.7.0_80 in this case) So I cannot use this solution for a long term.


Solution

  • Well, instead of trying to fix the original keystore, I managed to solve it by moving to Play app signing.

    I created a new upload key which I'm using from now on to create signed APK.

    For those whom it might help:

    • Sign it to play store and navigate to your application

    • Go to Setup -> App Integrity

    • Choose Export and upload a key from Java keystore, and follow the instructions*

    *Please notice that in this stage you will probably get the same exception as above (DerInputStream.getlength()....).
    This happens because the private key generation command uses java - the same java version which caused the exception in the first place.
    So here's the catch:
    You should use a different java 8 version, in which the exception won't occur (like 1.8.0_112).
    It won't work with java 7 and earlier because the pepk tool is build with java 8.

    something like this:

    /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/bin/java -jar pepk.jar ....

    Continue with the new upload key creation from here and you should be all set up.