Skip to content

Instantly share code, notes, and snippets.

@Ehsanul-Hoque
Last active April 8, 2020 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ehsanul-Hoque/f41d49081f15d316a9db14e7ee52f1fe to your computer and use it in GitHub Desktop.
Save Ehsanul-Hoque/f41d49081f15d316a9db14e7ee52f1fe to your computer and use it in GitHub Desktop.
Python code for generating release apk automatically (Part 3)
import os
from subprocess import Popen, PIPE
# ...
# Function for creating keystore file
def createKeystore(item_dict):
global input_key_filename
global input_store_pass
global input_key_alias
global input_key_pass
global keystore_exists
if keystore_exists:
# If keystore already exists, there is no need to create a new keystore
if os.path.exists(constants.KEYSTORE_FOLDER + input_key_filename + '.jks'):
print('\nKeystore file found!')
return True
# If keystore does not exist, and user did not give enough information
# to create a new keystore, then we have nothing to do
else:
print(('\nNo keystore file is found with the given name,'
+ ' and you did not give enough information to create a new keystore.'))
keystore_exists = False
return False
# If keystore already exists, there is no need to create a new keystore
elif os.path.exists(constants.KEYSTORE_FOLDER + input_key_filename + '.jks'):
print(('\nA keystore file is found with same name.'
+ ' So new keystore file will not be created and the existing keystore file will be used.'))
keystore_exists = True
return True
keystore_exists = False
print('\nCREATING keystore file...')
cn = item_dict[constants.JSON_KEY_KEYSTORE_OWNER]
ou = item_dict[constants.JSON_KEY_KEYSTORE_OU]
o = item_dict[constants.JSON_KEY_ORGANIZATION]
c = item_dict[constants.JSON_KEY_COUNTRY]
process = Popen(('keytool -genkeypair -keyalg RSA -keysize 2048 -validity 20000 -dname "cn='
+ cn + ', ou=' + ou + ', o=' + o + ', c=' + c + '" -alias ' + input_key_alias
+ ' -keypass ' + input_key_pass + ' -keystore "' + constants.KEYSTORE_FOLDER + input_key_filename
+ '.jks" -storepass ' + input_store_pass), shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
output, errors = process.communicate()
# Non zero returncode means process error
if process.returncode:
# Show output and error for debugging purpose.
# You can comment out the print statements if you want.
print('keystore file creation output = ' + output)
print('keystore file creation error = ' + errors)
print('keystore file creation process return code = ' + str(process.returncode))
print('Keystore file creation failed!')
return False
else:
print('Keystore file created = ' + input_key_filename + '.jks')
return True
# Function for saving keystore information in a file
def saveKeystoreInfo():
global input_key_filename
global input_store_pass
global input_key_alias
global input_key_pass
# keystore_exists = True means a keystore file already existed and no new keystore was created
if keystore_exists:
return
print('\nSaving keystore informations in a file...')
with open(constants.KEYSTORE_FOLDER + input_key_filename + constants.KEYSTORE_PROPERTIES_FILE_NAME_SUFFIX, "w") as file_object:
# Write keystore properties
file_object.write('STORE_FILE=' + constants.KEYSTORE_FOLDER + input_key_filename + '.jks\n')
file_object.write('KEY_STORE_PASSWORD=' + input_store_pass + '\n')
file_object.write('KEY_ALIAS=' + input_key_alias + '\n')
file_object.write('KEY_PASSWORD=' + input_key_pass + '\n')
print('Keystore information saved')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment