use of com.android.sdklib.internal.build.DebugKeyProvider 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);
}
}
Aggregations