在构建应用程序的调试版本时是否可以忽略storeFile?

时间:2018-03-01 15:39:19

标签: android gradle build.gradle

问题的关键在于,并非每个在此应用程序上工作的人都可以访问商店文件,并且必须注释掉这些行,以便进行同步:

signingConfigs {
    release {
//        storeFile file('.../android_keystore.keystore')
//        storePassword RELEASE_STORE_PASSWORD
//        keyAlias RELEASE_KEY_ALIAS
//        keyPassword RELEASE_KEY_PASSWORD
    }
}

在我们的构建类型中,我们定义了debug中没有signedconfig:

buildTypes{
     release {
          ...
     }
     debug {
          singingConfig null
          ...
     }
}

问题是Gradle Syncs与构建类型无关,因此它每次都会检查签名配置(storePassword,keyAlias,keyPassword),除非我对这些行进行注释。

是否有更自动化的方式来忽略这些线?

2 个答案:

答案 0 :(得分:4)

您可以使用以下内容:

android {

    signingConfigs {
        release
    }

    buildTypes {
            release {
                signingConfig signingConfigs.release
            }
    }
}

def Properties props = new Properties()
def propFile = new File('signing.properties')
if (propFile.canRead()){
    props.load(new FileInputStream(propFile))

    if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        println 'signing.properties found but some entries are missing'
        android.buildTypes.release.signingConfig = null
    }
}else {
    println 'signing.properties not found'
    android.buildTypes.release.signingConfig = null
}

其中signing.properties是:

STORE_FILE=/path/to/your.keystore
STORE_PASSWORD=yourkeystorepass
KEY_ALIAS=projectkeyalias
KEY_PASSWORD=keyaliaspassword

答案 1 :(得分:2)

将@ Gabriele的答案标记为答案,因为它更完整,更正确,但希望发布有关我的最终(简单和简单)解决方案的更新,我不会有在没有他答案的情况下想通了;

我所要做的就是检查文件是否存在。我没有意识到我可以调用方法并在Gradle文件中使用if语句;

signingConfigs {
     release {
          storeFile file('.../android_keystore.keystore')
          if (storeFile.exists()) {
               storePassword RELEASE_STORE_PASSWORD
               keyAlias RELEASE_KEY_ALIAS
               keyPassword RELEASE_KEY_PASSWORD
          }
}