/// <summary> /// Synchronously calls the apksigner tool to sign the specified APK using APK Signature Scheme V2. /// </summary> /// <returns>An error message if there was a problem running apksigner, or null if successful.</returns> public virtual string Sign(string apkFilePath) { string keystoreName; string keystorePass; string keyaliasName; string keyaliasPass; if (string.IsNullOrEmpty(_keystoreName) || string.IsNullOrEmpty(_keyaliasName)) { Debug.Log("No keystore and/or no keyalias specified. Signing using Android debug keystore."); var homePath = Application.platform == RuntimePlatform.WindowsEditor ? Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") : Environment.GetEnvironmentVariable("HOME"); if (string.IsNullOrEmpty(homePath)) { return("Failed to locate directory that contains Android debug keystore."); } keystoreName = Path.Combine(homePath, Path.Combine(".android", "debug.keystore")); keystorePass = "******"; keyaliasName = "androiddebugkey"; keyaliasPass = "******"; } else { keystoreName = _keystoreName; keystorePass = _keystorePass; keyaliasName = _keyaliasName; keyaliasPass = _keyaliasPass; } if (!File.Exists(keystoreName)) { return(string.Format("Failed to locate keystore file: {0}", keystoreName)); } // Sign the file {3} using key {2} contained in keystore file {1}. // ApkSignerResponder will provide passwords using stdin; this is the default for apksigner // so there is no need to specify "--ks-pass" or "--key-pass" arguments. // ApkSignerResponder will encode the passwords with UTF8, so we specify "--pass-encoding utf-8" here. var arguments = string.Format( "-jar {0} sign --ks {1} --ks-key-alias {2} --pass-encoding utf-8 {3}", CommandLine.QuotePath(_apkSignerJarPath), CommandLine.QuotePath(keystoreName), CommandLine.QuotePath(keyaliasName), CommandLine.QuotePath(apkFilePath)); var promptToPasswordDictionary = new Dictionary <string, string> { // Example keystore password prompt: "Keystore password for signer #1: " { "Keystore password for signer", keystorePass }, // Example keyalias password prompt: "Key \"androiddebugkey\" password for signer #1: " { "Key .+ password for signer", keyaliasPass } }; var responder = new ApkSignerResponder(promptToPasswordDictionary); var result = CommandLine.Run(_javaUtils.JavaBinaryPath, arguments, ioHandler: responder.AggregateLine); return(result.exitCode == 0 ? null : result.message); }
/// <summary> /// Synchronously calls the apksigner tool to sign the specified APK using APK Signature Scheme V2. /// </summary> /// <returns>true if the specified APK was successfully signed, false otherwise</returns> public static bool Sign(string apkPath) { string keystoreName; string keystorePass; string keyaliasName; string keyaliasPass; if (string.IsNullOrEmpty(PlayerSettings.Android.keystoreName)) { Debug.Log("No keystore specified. Signing using Android debug keystore."); var homePath = Application.platform == RuntimePlatform.WindowsEditor ? Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") : Environment.GetEnvironmentVariable("HOME"); if (string.IsNullOrEmpty(homePath)) { Debug.LogError("Failed to locate home directory that contains Android debug keystore."); return(false); } keystoreName = Path.Combine(homePath, AndroidDebugKeystore); keystorePass = "******"; keyaliasName = "androiddebugkey"; keyaliasPass = "******"; } else { keystoreName = PlayerSettings.Android.keystoreName; keystorePass = PlayerSettings.Android.keystorePass; keyaliasName = PlayerSettings.Android.keyaliasName; keyaliasPass = PlayerSettings.Android.keyaliasPass; } if (!File.Exists(keystoreName)) { Debug.LogErrorFormat("Failed to locate keystore file: {0}", keystoreName); return(false); } // This command will sign the APK file {3} using key {2} contained in keystore file {1}. // ApkSignerResponder will provide passwords using the default method of stdin, so no need // to specify "--ks-pass" or "--key-pass". ApkSignerResponder will encode the passwords // with UTF8, so we specify "--pass-encoding utf-8" here. var arguments = string.Format( "-jar {0} sign --ks {1} --ks-key-alias {2} --pass-encoding utf-8 {3}", CommandLine.QuotePathIfNecessary(GetApkSignerJarPath()), CommandLine.QuotePathIfNecessary(keystoreName), keyaliasName, CommandLine.QuotePathIfNecessary(apkPath)); var promptToPasswordDictionary = new Dictionary <string, string> { // Example keystore password prompt: "Keystore password for signer #1: " { "Keystore password for signer", keystorePass }, // Example keyalias password prompt: "Key \"androiddebugkey\" password for signer #1: " { "password for signer", keyaliasPass } }; var responder = new ApkSignerResponder(promptToPasswordDictionary); var result = CommandLine.Run(JavaUtilities.JavaBinaryPath, arguments, ioHandler: responder.AggregateLine); if (result.exitCode == 0) { return(true); } Debug.LogErrorFormat("APK re-signing failed: {0}", result.message); return(false); }
/// <summary> /// Synchronously calls the apksigner tool to sign the specified APK using APK Signature Scheme V2. /// </summary> /// <returns>true if the specified APK was successfully signed, false otherwise</returns> public static bool Sign(string apkPath) { string keystoreName; string keystorePass; string keyaliasName; string keyaliasPass; if (string.IsNullOrEmpty(PlayerSettings.Android.keystoreName)) { Debug.Log("No keystore specified. Signing using Android debug keystore."); var homePath = Application.platform == RuntimePlatform.WindowsEditor ? Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") : Environment.GetEnvironmentVariable("HOME"); if (string.IsNullOrEmpty(homePath)) { Debug.LogError("Failed to locate home directory that contains Android debug keystore."); return(false); } keystoreName = Path.Combine(homePath, AndroidDebugKeystore); keystorePass = "******"; keyaliasName = "androiddebugkey"; keyaliasPass = "******"; } else { keystoreName = PlayerSettings.Android.keystoreName; keystorePass = PlayerSettings.Android.keystorePass; keyaliasName = PlayerSettings.Android.keyaliasName; keyaliasPass = PlayerSettings.Android.keyaliasPass; } if (!File.Exists(keystoreName)) { Debug.LogErrorFormat("Failed to locate keystore file: {0}", keystoreName); return(false); } var arguments = string.Format( "-jar {0} sign --ks {1} --ks-key-alias {2} --pass-encoding utf-8 {3}", GetApkSignerJarPath(), keystoreName, keyaliasName, apkPath); var promptToPasswordDictionary = new Dictionary <string, string> { // Example keystore password prompt: "Keystore password for signer #1: " { "Keystore password for signer", keystorePass }, // Example keyalias password prompt: "Key \"androiddebugkey\" password for signer #1: " { "password for signer", keyaliasPass } }; var responder = new ApkSignerResponder(promptToPasswordDictionary); var result = CommandLine.Run(JavaUtilities.JavaBinaryPath, arguments, ioHandler: responder.AggregateLine); if (result.exitCode == 0) { return(true); } Debug.LogErrorFormat("\"java {0}\" failed with exit code {1}", arguments, result.exitCode); return(false); }