【问题标题】:Is it possible to ignore the storeFile when building a debug version of the application?构建应用程序的调试版本时是否可以忽略 storeFile?
【发布时间】:2025-12-09 00:10:01
【问题描述】:

问题的症结在于,并非所有使用此应用程序的人都可以访问存储文件,并且必须如下注释掉这些行以进行 gradle 同步:

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

在我们的构建类型中,我们定义了调试中没有签名配置:

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

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

有没有更自动化的方法来忽略这些行?

【问题讨论】:

    标签: android gradle build.gradle


    【解决方案1】:

    你可以这样使用:

    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
    

    【讨论】:

    • 感谢您的回复!也许我不了解 Gradle 的工作原理,但我无法弄清楚这是否真的在做任何事情,或者我的 apk 是否只是没有被签名。我没有看到那些 println 语句被张贴在任何地方(而且我有点不确定它们将被打印到哪个窗口;事件日志?)。
    【解决方案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
              }
    }
    

    【讨论】:

      最近更新 更多