Sign APK without putting keystore info in build.gradle

后端 未结 12 977
栀梦
栀梦 2020-11-28 17:51

I am trying to setup signing process so that keystore password and key password are not stored in the project\'s build.gradle file.

Cur

12条回答
  •  鱼传尺愫
    2020-11-28 18:02

    Inspired from https://stackoverflow.com/a/33218300/1673761 with some improvements

    Pros

    • keystore properties (file name, alias, passwords) are stored outside of git
    • Debug mode still works even if we don't have a keystore file or keystore.properties
    • The keystore and its properties are in the root folder side by side

    In the root of your project (or in android folder for React Native):

    • Add your generated keystore file ie: app.keystore
    • create a file named keystore.properties
    • Add these two files to .gitignore

    In keystore.properties add

    STORE_FILE=app.keystore
    KEY_ALIAS=app_alias
    STORE_PASSWORD=your_password
    KEY_PASSWORD=your_password
    

    In app/build.gradle add

    // Load keystore
    def keystoreProperties = new Properties()
    try {
        def keystorePropertiesFile = rootProject.file("keystore.properties");
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    } catch(IOException e) {
        // We don't have release keys, ignoring
    }
    
    ...
    
    android {
      ...
      signingConfigs {
        release {
          if (keystoreProperties['STORE_FILE']) {
            storeFile rootProject.file(keystoreProperties['STORE_FILE'])
            storePassword keystoreProperties['STORE_PASSWORD']
            keyAlias keystoreProperties['KEY_ALIAS']
            keyPassword keystoreProperties['KEY_PASSWORD']
          }
        }
      }
      buildTypes {
        release {
          signingConfig signingConfigs.release
        }
      }
    }
    

    PS: Edits to improve the groovy logic are welcome

提交回复
热议问题