Gradle Android Studio signing with a properties file

前端 未结 4 589
予麋鹿
予麋鹿 2021-02-04 15:20

I´m trying to sign my app with the gradle build file. When I use the plain signingConfigs it works (signingConfigs.release)! But If I´m trying to read the properties from the pr

相关标签:
4条回答
  • 2021-02-04 15:53

    An alternative to juggling variables is to include/apply a gradle file containing the signing configs.

    So in your build.gradle

    android {
    // the part to include
        if (project.hasProperty("yourproject.signing") && new File(project.property("yourproject.signing")).exists()) {
            println 'apply yourproject signing property';
            apply from: project.property("yourproject.signing");
        } else {
            println 'no yourproject signing property';
        }
    // the rest of the usual stuff
        compileSdkVersion etc.
    

    In your .gradle directory (depends on your OS where it is) add a gradle.properties file, or if it exists, add the following line

    yourproject.signing=*the full path to your magic file*signing.gradle
    

    In the file pointed to by the above entry you include the signing configs as you would in the build.gradle file

    android {
        signingConfigs {
            release {
                keyAlias '*your release key alias*'
                keyPassword '*your key password*'
                storeFile file('*the full path to your keystore file*.jks')
                storePassword '*your keystore password*'
            }
        }
    }
    

    Yes, the extra android wrapping has to be there. The advantage being that the above is a regular gradle file, so you can do all the magic you want in there.

    Add more signingConfigs as needed, use the usual way, e.g.

    signingConfig signingConfigs.release
    
    0 讨论(0)
  • 2021-02-04 15:54

    I found the mistake!!! The problem was case-sensitive! I write

    keyPassword props["KeyPassword"]

    but the file contains only

    keyPassword

    Why does gradle don´t tell me?

    0 讨论(0)
  • 2021-02-04 15:58

    Here is a complete step-by-step that I took to use a properties file to move both of my keys out of the gradle.build file into a file that will not be included in any builds or repository commits.

    1) Create a gradle.properties (if you don't already have one).

    The location for this file depends on your OS:

       /home/<username>/.gradle/ (Linux)
       /Users/<username>/.gradle/ (Mac)
       C:\Users\<username>\.gradle (Windows)
    

    2) Add an entry pointing to yourprojectname.properties file. (example for Windows)

    yourprojectname.properties=c:\\Users\\<username>\\signing\\yourprojectname.properties
    

    3) Create yourprojectname.properties file in the location you specified in Step 2 with the following information:

    keystore=C:\\path\\to\\keystore\\yourapps.keystore
    keystore.password=your_secret_password
    

    4) Modify your gradle.build file to point to yourprojectname.properties file to use the variables.

    if(project.hasProperty("yourprojectname.properties")
            && new File(project.property("yourprojectname.properties")).exists()) {
    
        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("yourprojectname.properties"))))
    
        android {
            signingConfigs {
                release {
                    keyAlias 'release'
                    keyPassword props['keystore.password']
                    storeFile file(props['keystore'])
                    storePassword props['keystore.password']
                }
                debug {
                    keyAlias 'debug'
                    keyPassword props['keystore.password']
                    storeFile file(props['keystore'])
                    storePassword props['keystore.password']
                }
            }
            compileSdkVersion 19
            buildToolsVersion "20.0.0"
            defaultConfig {
                applicationId "your.project.app"
                minSdkVersion 16
                targetSdkVersion 17
            }
            buildTypes {
                release {
                }
            }
        }
    
    }
    
    dependencies {
        ...
    }
    

    5) Enjoy! Now all of your keys will be outside of the root of the directory and yet you still have the joys of automation for each build.

    If you get an error in your gradle.build file about the "props" variable it's because you are not executing the "android {}" block inside the very first if condition where the props variable gets assigned so just move the entire android{ ... } section into the condition in which the props variable is assigned then try again.

    I pieced these steps together from the information found here and here.

    0 讨论(0)
  • 2021-02-04 16:11

    This is what I do and it works for me:

    signingConfigs {
        release {
            def Properties localProps = new Properties()
            localProps.load(new FileInputStream(file('../local.properties')))
            def Properties keyProps = new Properties()
            assert localProps['keystore.props.file'];
            keyProps.load(new FileInputStream(file(localProps['keystore.props.file'])))
            storeFile file(keyProps["store"])
            keyAlias keyProps["alias"]
            storePassword keyProps["storePass"]
            keyPassword keyProps["pass"]
        }
    }
    

    My local.properties file contains a path to a "keystore.properties" file. That contains my info about the keystore:

    store=***
    alias=***
    pass=***
    storePass=***
    

    Perhaps the loading of two Propreties file is redundant, but it does work for me.

    0 讨论(0)
提交回复
热议问题