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

Solution 1:

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.