Search in sources :

Example 1 with IKeyGenOutput

use of com.android.sdklib.internal.build.DebugKeyProvider.IKeyGenOutput in project bazel by bazelbuild.

the class ApkBuilder method getDebugKey.

/**
     * Returns the key and certificate from a given debug store.
     *
     * It is expected that the store password is 'android' and the key alias and password are
     * 'androiddebugkey' and 'android' respectively.
     *
     * @param storeOsPath the OS path to the debug store.
     * @param verboseStream an option {@link PrintStream} to display verbose information
     * @return they key and certificate in a {@link SigningInfo} object or null.
     * @throws ApkCreationException
     */
public static SigningInfo getDebugKey(String storeOsPath, final PrintStream verboseStream) throws ApkCreationException {
    try {
        if (storeOsPath != null) {
            File storeFile = new File(storeOsPath);
            try {
                checkInputFile(storeFile);
            } catch (FileNotFoundException e) {
            // ignore these since the debug store can be created on the fly anyway.
            }
            // get the debug key
            if (verboseStream != null) {
                verboseStream.println(String.format("Using keystore: %s", storeOsPath));
            }
            IKeyGenOutput keygenOutput = null;
            if (verboseStream != null) {
                keygenOutput = new IKeyGenOutput() {

                    @Override
                    public void out(String message) {
                        verboseStream.println(message);
                    }

                    @Override
                    public void err(String message) {
                        verboseStream.println(message);
                    }
                };
            }
            DebugKeyProvider keyProvider = new DebugKeyProvider(storeOsPath, null, /*store type*/
            keygenOutput);
            PrivateKey key = keyProvider.getDebugKey();
            X509Certificate certificate = (X509Certificate) keyProvider.getCertificate();
            if (key == null) {
                throw new ApkCreationException("Unable to get debug signature key");
            }
            // compare the certificate expiration date
            if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
                // TODO, regenerate a new one.
                throw new ApkCreationException("Debug Certificate expired on " + DateFormat.getInstance().format(certificate.getNotAfter()));
            }
            return new SigningInfo(key, certificate);
        } else {
            return null;
        }
    } catch (KeytoolException e) {
        if (e.getJavaHome() == null) {
            throw new ApkCreationException(e.getMessage() + "\nJAVA_HOME seems undefined, setting it will help locating keytool automatically\n" + "You can also manually execute the following command\n:" + e.getCommandLine(), e);
        } else {
            throw new ApkCreationException(e.getMessage() + "\nJAVA_HOME is set to: " + e.getJavaHome() + "\nUpdate it if necessary, or manually execute the following command:\n" + e.getCommandLine(), e);
        }
    } catch (ApkCreationException e) {
        throw e;
    } catch (Exception e) {
        throw new ApkCreationException(e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) FileNotFoundException(java.io.FileNotFoundException) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) KeytoolException(com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IKeyGenOutput(com.android.sdklib.internal.build.DebugKeyProvider.IKeyGenOutput) KeytoolException(com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException) File(java.io.File) DebugKeyProvider(com.android.sdklib.internal.build.DebugKeyProvider)

Aggregations

DebugKeyProvider (com.android.sdklib.internal.build.DebugKeyProvider)1 IKeyGenOutput (com.android.sdklib.internal.build.DebugKeyProvider.IKeyGenOutput)1 KeytoolException (com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 Date (java.util.Date)1