diff options
author | Scott Barta <sbarta@google.com> | 2012-12-18 15:02:31 -0800 |
---|---|---|
committer | Scott Barta <sbarta@google.com> | 2012-12-21 12:09:15 -0800 |
commit | 740e3ddee7dee67055daf6c542b96b5cf7be3c40 (patch) | |
tree | c7626ab4d4ac4cc4dfcc6f97290133f5b40aaea6 /builder | |
parent | d654e5915e38b7a14fc3c423819bb9b0c272de70 (diff) | |
download | build-740e3ddee7dee67055daf6c542b96b5cf7be3c40.tar.gz |
Keystore objects.
Replaces keystore attributes on build types and flavors with a
SigningKeystore object that can be accessed by reference from
those types. Eliminates the debugSigning attribute in favor
of a default debug keystore that's automatically configured
for all projects.
Change-Id: Icbfc66487e26068ae6edff8395ebc29d0d28f46c
Diffstat (limited to 'builder')
6 files changed, 237 insertions, 115 deletions
diff --git a/builder/src/main/java/com/android/builder/AndroidBuilder.java b/builder/src/main/java/com/android/builder/AndroidBuilder.java index b0ce8f3..8eea262 100644 --- a/builder/src/main/java/com/android/builder/AndroidBuilder.java +++ b/builder/src/main/java/com/android/builder/AndroidBuilder.java @@ -719,7 +719,6 @@ public class AndroidBuilder { * @param packagedJars the jars that are packaged (libraries + jar dependencies) * @param javaResourcesLocation the processed Java resource folder * @param jniLibsLocation the location of the compiled JNI libraries - * @param debugSigned whether the app is signed with the debug key * @param debugJni whether the app should include jni debug data * @param signingStoreLocation signing store location (if not debug signed) * @param signingStorePassword signing store password (if not debug signed) @@ -736,9 +735,8 @@ public class AndroidBuilder { @NonNull List<File> packagedJars, @Nullable String javaResourcesLocation, @Nullable String jniLibsLocation, - boolean debugSigned, boolean debugJni, - @Nullable String signingStoreLocation, + @Nullable File signingStoreLocation, @Nullable String signingStorePassword, @Nullable String signingKeyAlias, @Nullable String signingKeyPassword, @@ -750,28 +748,15 @@ public class AndroidBuilder { SigningInfo signingInfo = null; try { - if (debugSigned) { - String storeLocation = DebugKeyHelper.defaultDebugKeyStoreLocation(); - File storeFile = new File(storeLocation); - if (storeFile.isDirectory()) { - throw new RuntimeException( - String.format("A folder is in the way of the debug keystore: %s", - storeLocation)); - } else if (!storeFile.exists()) { - if (!DebugKeyHelper.createNewStore( - storeLocation, null /*storeType*/, mLogger)) { - throw new RuntimeException(); - } - } - - // load the key - signingInfo = DebugKeyHelper.getDebugKey(storeLocation, null /*storeStype*/); - } else if (signingStoreLocation != null && + if (signingStoreLocation != null && signingStorePassword != null && signingKeyAlias != null && signingKeyPassword != null) { + if (DebugKeyHelper.defaultDebugKeyStoreLocation().equals(signingStoreLocation)) { + createDebugKeystore(signingStoreLocation.getAbsolutePath()); + } signingInfo = KeystoreHelper.getSigningInfo( - signingStoreLocation, + signingStoreLocation.getAbsolutePath(), signingStorePassword, null, /*storeStype*/ signingKeyAlias, @@ -817,4 +802,24 @@ public class AndroidBuilder { throw new RuntimeException(e); } } + + /** + * Creates a debug signing keystore at the given location if one does not already exist. + * + * @param storeLocation the fully-qualified path where the keystore should live. + * @throws KeytoolException + */ + private void createDebugKeystore(String storeLocation) throws KeytoolException { + File storeFile = new File(storeLocation); + if (storeFile.isDirectory()) { + throw new RuntimeException( + String.format("A folder is in the way of the debug keystore: %s", + storeLocation)); + } else if (!storeFile.exists()) { + if (!DebugKeyHelper.createNewStore( + storeLocation, null /*storeType*/, mLogger)) { + throw new RuntimeException("Unable to recreate missing debug keystore."); + } + } + } } diff --git a/builder/src/main/java/com/android/builder/BuildType.java b/builder/src/main/java/com/android/builder/BuildType.java index 9d8abec..e454436 100644 --- a/builder/src/main/java/com/android/builder/BuildType.java +++ b/builder/src/main/java/com/android/builder/BuildType.java @@ -29,10 +29,10 @@ public class BuildType extends BuildConfig { private final String mName; private boolean mDebuggable; private boolean mDebugJniBuild; - private boolean mDebugSigned; private String mPackageNameSuffix = null; private String mVersionNameSuffix = null; private boolean mRunProguard = false; + private Keystore mKeystore = null; private boolean mZipAlign = true; @@ -48,14 +48,13 @@ public class BuildType extends BuildConfig { private void initDebug() { mDebuggable = true; mDebugJniBuild = true; - mDebugSigned = true; mZipAlign = false; + mKeystore = new Keystore(Keystore.DEBUG); } private void initRelease() { mDebuggable = false; mDebugJniBuild = false; - mDebugSigned = false; } public String getName() { @@ -80,15 +79,6 @@ public class BuildType extends BuildConfig { return mDebugJniBuild; } - public BuildType setDebugSigned(boolean debugSigned) { - mDebugSigned = debugSigned; - return this; - } - - public boolean isDebugSigned() { - return mDebugSigned; - } - public BuildType setPackageNameSuffix(@Nullable String packageNameSuffix) { mPackageNameSuffix = packageNameSuffix; return this; @@ -125,6 +115,16 @@ public class BuildType extends BuildConfig { return mZipAlign; } + public BuildType setKeystore(@Nullable Keystore keystore) { + mKeystore = keystore; + return this; + } + + @Nullable + Keystore getKeystore() { + return mKeystore; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -135,7 +135,6 @@ public class BuildType extends BuildConfig { if (!mName.equals(buildType.mName)) return false; if (mDebugJniBuild != buildType.mDebugJniBuild) return false; - if (mDebugSigned != buildType.mDebugSigned) return false; if (mDebuggable != buildType.mDebuggable) return false; if (mRunProguard != buildType.mRunProguard) return false; if (mZipAlign != buildType.mZipAlign) return false; @@ -147,6 +146,10 @@ public class BuildType extends BuildConfig { !mVersionNameSuffix.equals(buildType.mVersionNameSuffix) : buildType.mVersionNameSuffix != null) return false; + if (mKeystore != null ? + !mKeystore.equals(buildType.mKeystore) : + buildType.mKeystore != null) + return false; return true; } @@ -157,11 +160,11 @@ public class BuildType extends BuildConfig { result = 31 * result + (mName.hashCode()); result = 31 * result + (mDebuggable ? 1 : 0); result = 31 * result + (mDebugJniBuild ? 1 : 0); - result = 31 * result + (mDebugSigned ? 1 : 0); result = 31 * result + (mPackageNameSuffix != null ? mPackageNameSuffix.hashCode() : 0); result = 31 * result + (mVersionNameSuffix != null ? mVersionNameSuffix.hashCode() : 0); result = 31 * result + (mRunProguard ? 1 : 0); result = 31 * result + (mZipAlign ? 1 : 0); + result = 31 * result + (mKeystore != null ? mKeystore.hashCode() : 0); return result; } @@ -171,12 +174,11 @@ public class BuildType extends BuildConfig { .add("name", mName) .add("debuggable", mDebuggable) .add("debugJniBuild", mDebugJniBuild) - .add("debugSigned", mDebugSigned) .add("packageNameSuffix", mPackageNameSuffix) .add("versionNameSuffix", mVersionNameSuffix) .add("runProguard", mRunProguard) .add("zipAlign", mZipAlign) - .omitNullValues() + .add("keystore", mKeystore) .toString(); } } diff --git a/builder/src/main/java/com/android/builder/Keystore.java b/builder/src/main/java/com/android/builder/Keystore.java new file mode 100644 index 0000000..674d207 --- /dev/null +++ b/builder/src/main/java/com/android/builder/Keystore.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.builder; + +import com.android.annotations.NonNull; +import com.android.builder.internal.signing.DebugKeyHelper; +import com.android.prefs.AndroidLocation; +import com.google.common.base.Objects; + +/** + * Keystore encapsulates the information necessary to access certificates in a keystore file + * that can be used to sign APKs. + */ +public class Keystore extends BuildConfig { + private static final long serialVersionUID = 1L; + + /** + * The name of the default keystore that can sign debug builds. + */ + public static final String DEBUG = "debug"; + + private final String mName; + private String mStoreLocation = null; + private String mStorePassword = null; + private String mKeyAlias = null; + private String mKeyPassword = null; + + /** + * Creates a Keystore with a given name. + * + * @param name the name of the keystore. + * + * @see #DEBUG + */ + public Keystore(@NonNull String name) { + mName = name; + if (DEBUG.equals(name)) { + initDebug(); + } + } + + public String getName() { + return mName; + } + + private void initDebug() { + try { + mStoreLocation = DebugKeyHelper.defaultDebugKeyStoreLocation(); + mStorePassword = "android"; + mKeyAlias = "androiddebugkey"; + mKeyPassword = "android"; + } catch (AndroidLocation.AndroidLocationException e) { + throw new RuntimeException(e); + } + } + + public String getStoreLocation() { + return mStoreLocation; + } + + public Keystore setStoreLocation(String storeLocation) { + mStoreLocation = storeLocation; + return this; + } + + public String getStorePassword() { + return mStorePassword; + } + + public Keystore setStorePassword(String storePassword) { + mStorePassword = storePassword; + return this; + } + + public String getKeyAlias() { + return mKeyAlias; + } + + public Keystore setKeyAlias(String keyAlias) { + mKeyAlias = keyAlias; + return this; + } + + public String getKeyPassword() { + return mKeyPassword; + } + + public Keystore setKeyPassword(String keyPassword) { + mKeyPassword = keyPassword; + return this; + } + + public boolean isSigningReady() { + return mStoreLocation != null && + mStorePassword != null && + mKeyAlias != null && + mKeyPassword != null; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + Keystore that = (Keystore) o; + + if (mName != null ? !mName.equals(that.mName) : that.mName != null) return false; + if (mKeyAlias != null ? + !mKeyAlias.equals(that.mKeyAlias) : + that.mKeyAlias != null) + return false; + if (mKeyPassword != null ? + !mKeyPassword.equals(that.mKeyPassword) : + that.mKeyPassword != null) + return false; + if (mStoreLocation != null ? + !mStoreLocation.equals(that.mStoreLocation) : + that.mStoreLocation != null) + return false; + if (mStorePassword != null ? + !mStorePassword.equals(that.mStorePassword) : + that.mStorePassword != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (mName != null ? mName.hashCode() : 0); + result = 31 * result + (mStoreLocation != null ? + mStoreLocation.hashCode() : 0); + result = 31 * result + (mStorePassword != null ? + mStorePassword.hashCode() : 0); + result = 31 * result + (mKeyAlias != null ? mKeyAlias.hashCode() : 0); + result = 31 * result + (mKeyPassword != null ? mKeyPassword.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("name", mName) + .add("storeLocation", mStoreLocation) + .add("storePassword", mStorePassword) + .add("keyAlias", mKeyAlias) + .add("keyPassword", mKeyPassword) + .toString(); + } +} diff --git a/builder/src/main/java/com/android/builder/ProductFlavor.java b/builder/src/main/java/com/android/builder/ProductFlavor.java index e92ed62..ddc8113 100644 --- a/builder/src/main/java/com/android/builder/ProductFlavor.java +++ b/builder/src/main/java/com/android/builder/ProductFlavor.java @@ -41,11 +41,7 @@ public class ProductFlavor extends BuildConfig { private String mPackageName = null; private String mTestPackageName = null; private String mTestInstrumentationRunner = null; - - private String mSigningStoreLocation = null; - private String mSigningStorePassword = null; - private String mSigningKeyAlias = null; - private String mSigningKeyPassword = null; + private Keystore mKeystore = null; /** * Creates a ProductFlavor with a given name. @@ -144,49 +140,15 @@ public class ProductFlavor extends BuildConfig { return mTestInstrumentationRunner; } - public String getSigningStoreLocation() { - return mSigningStoreLocation; - } - - public ProductFlavor setSigningStoreLocation(String signingStoreLocation) { - mSigningStoreLocation = signingStoreLocation; - return this; - } - - public String getSigningStorePassword() { - return mSigningStorePassword; + public Keystore getKeystore() { + return mKeystore; } - public ProductFlavor setSigningStorePassword(String signingStorePassword) { - mSigningStorePassword = signingStorePassword; + public ProductFlavor setKeystore(Keystore keystore) { + mKeystore = keystore; return this; } - public String getSigningKeyAlias() { - return mSigningKeyAlias; - } - - public ProductFlavor setSigningKeyAlias(String signingKeyAlias) { - mSigningKeyAlias = signingKeyAlias; - return this; - } - - public String getSigningKeyPassword() { - return mSigningKeyPassword; - } - - public ProductFlavor setSigningKeyPassword(String signingKeyPassword) { - mSigningKeyPassword = signingKeyPassword; - return this; - } - - public boolean isSigningReady() { - return mSigningStoreLocation != null && - mSigningStorePassword != null && - mSigningKeyAlias != null && - mSigningKeyPassword != null; - } - /** * Merges the flavor on top of a base platform and returns a new object with the result. * @param base the flavor to merge on top of @@ -207,12 +169,8 @@ public class ProductFlavor extends BuildConfig { flavor.mTestInstrumentationRunner = chooseString(mTestInstrumentationRunner, base.mTestInstrumentationRunner); - flavor.mSigningStoreLocation = chooseString(mSigningStoreLocation, - base.mSigningStoreLocation); - flavor.mSigningStorePassword = chooseString(mSigningStorePassword, - base.mSigningStorePassword); - flavor.mSigningKeyAlias = chooseString(mSigningKeyAlias, base.mSigningKeyAlias); - flavor.mSigningKeyPassword = chooseString(mSigningKeyPassword, base.mSigningKeyPassword); + flavor.mKeystore = + mKeystore != null ? mKeystore : base.mKeystore; return flavor; } @@ -241,22 +199,6 @@ public class ProductFlavor extends BuildConfig { !mPackageName.equals(that.mPackageName) : that.mPackageName != null) return false; - if (mSigningKeyAlias != null ? - !mSigningKeyAlias.equals(that.mSigningKeyAlias) : - that.mSigningKeyAlias != null) - return false; - if (mSigningKeyPassword != null ? - !mSigningKeyPassword.equals(that.mSigningKeyPassword) : - that.mSigningKeyPassword != null) - return false; - if (mSigningStoreLocation != null ? - !mSigningStoreLocation.equals(that.mSigningStoreLocation) : - that.mSigningStoreLocation != null) - return false; - if (mSigningStorePassword != null ? - !mSigningStorePassword.equals(that.mSigningStorePassword) : - that.mSigningStorePassword != null) - return false; if (mTestInstrumentationRunner != null ? !mTestInstrumentationRunner.equals(that.mTestInstrumentationRunner) : that.mTestInstrumentationRunner != null) @@ -267,6 +209,9 @@ public class ProductFlavor extends BuildConfig { if (mVersionName != null ? !mVersionName.equals(that.mVersionName) : that.mVersionName != null) return false; + if (mKeystore != null ? + !mKeystore.equals(that.mKeystore) : that.mKeystore != null) + return false; return true; } @@ -283,12 +228,7 @@ public class ProductFlavor extends BuildConfig { result = 31 * result + (mTestPackageName != null ? mTestPackageName.hashCode() : 0); result = 31 * result + (mTestInstrumentationRunner != null ? mTestInstrumentationRunner.hashCode() : 0); - result = 31 * result + (mSigningStoreLocation != null ? - mSigningStoreLocation.hashCode() : 0); - result = 31 * result + (mSigningStorePassword != null ? - mSigningStorePassword.hashCode() : 0); - result = 31 * result + (mSigningKeyAlias != null ? mSigningKeyAlias.hashCode() : 0); - result = 31 * result + (mSigningKeyPassword != null ? mSigningKeyPassword.hashCode() : 0); + result = 31 * result + (mKeystore != null ? mKeystore.hashCode() : 0); return result; } @@ -303,11 +243,7 @@ public class ProductFlavor extends BuildConfig { .add("packageName", mPackageName) .add("testPackageName", mTestPackageName) .add("testInstrumentationRunner", mTestInstrumentationRunner) - .add("signingStoreLocation", mSigningStoreLocation) - .add("signingStorePassword", mSigningStorePassword) - .add("signingKeyAlias", mSigningKeyAlias) - .add("signingKeyPassword", mSigningKeyPassword) - .omitNullValues() + .add("keystore", mKeystore) .toString(); } diff --git a/builder/src/main/java/com/android/builder/VariantConfiguration.java b/builder/src/main/java/com/android/builder/VariantConfiguration.java index 83bf96a..f53069f 100644 --- a/builder/src/main/java/com/android/builder/VariantConfiguration.java +++ b/builder/src/main/java/com/android/builder/VariantConfiguration.java @@ -633,6 +633,19 @@ public class VariantConfiguration { return fullList; } + public Keystore getKeystore() { + Keystore keystore = mBuildType.getKeystore(); + if (keystore != null) { + return keystore; + } + return mMergedFlavor.getKeystore(); + } + + public boolean isSigningReady() { + Keystore keystore = getKeystore(); + return keystore != null && keystore.isSigningReady(); + } + protected void validate() { if (mType != Type.TEST) { File manifest = mDefaultSourceProvider.getManifestFile(); diff --git a/builder/src/test/java/com/android/builder/BuildTypeTest.java b/builder/src/test/java/com/android/builder/BuildTypeTest.java index 6bdff49..aad3e69 100644 --- a/builder/src/test/java/com/android/builder/BuildTypeTest.java +++ b/builder/src/test/java/com/android/builder/BuildTypeTest.java @@ -25,7 +25,8 @@ public class BuildTypeTest extends TestCase { assertTrue(type.isDebuggable()); assertTrue(type.isDebugJniBuild()); - assertTrue(type.isDebugSigned()); + assertNotNull(type.getKeystore()); + assertTrue(type.getKeystore().isSigningReady()); } public void testRelease() { @@ -33,6 +34,5 @@ public class BuildTypeTest extends TestCase { assertFalse(type.isDebuggable()); assertFalse(type.isDebugJniBuild()); - assertFalse(type.isDebugSigned()); } } |