仅在生成签名APK时自动增加版本代码

时间:2016-07-04 09:50:03

标签: android android-gradle version auto-increment

我已成功增加版本代码。但我只想在菜单Build - >生成签名apk时增加它。生成签名的APK。以下是我的gradle代码。

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
def versionPropsFile = file('version.properties')

if (versionPropsFile.canRead()) {
    def Properties versionProps = new Properties()

    versionProps.load(new FileInputStream(versionPropsFile))

    def code = versionProps['build.version'].toInteger() + 1

    versionProps['build.version']=code.toString()
    versionProps.store(versionPropsFile.newWriter(), null)

    defaultConfig {
        applicationId "com.test"
        multiDexEnabled true
        versionCode code
        versionName "1.1"
        minSdkVersion 18
        targetSdkVersion 23
    }
}
else {
    throw new GradleException("Could not read version.properties!")
}

buildTypes {
    release {
        //minifyEnabled false
        //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
    debug {
        debuggable true
    }
}
dexOptions {
    incremental true
    javaMaxHeapSize "4g"
    preDexLibraries = false
}}

我的version.properties文件包含以下信息: 次要= 7 分支= 4 主要= 10 build.version = 73

2 个答案:

答案 0 :(得分:1)

我可以通过在assembleRelease gradle任务上连接我的代码来完成它。得到了蒂姆的blog的帮助。以下是代码 -

apply plugin: 'com.android.application' 
android {
         compileSdkVersion 23
         buildToolsVersion "23.0.3"

         def Properties versionProps = loadVersionFile()
         defaultConfig {
                      applicationId "com.test"
                      multiDexEnabled true
                      versionCode getCode(versionProps)
                      versionName getName(versionProps)
                      minSdkVersion 18
                      targetSdkVersion 23
        }
        signingConfigs {
                       release {
                                storeFile file(".../keys.jks")
                                storePassword "..."
                                keyAlias "..."
                                keyPassword "..."
                       }
       }


       buildTypes {
                   release {
                            signingConfig signingConfigs.release

                           //minifyEnabled false
                           //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                  }
                  debug {
                            debuggable true
                  }
       }
       dexOptions {
            ...
       }
  }

def loadVersionFile() {
                    def versionPropsFile = file('version.properties')
                    def Properties versionProps
                    if (versionPropsFile.canRead()) {

                    versionProps = new Properties()
                    versionProps.load(new FileInputStream(versionPropsFile))

                    } else {
                          throw new GradleException("Could not read                                                 version.properties!")
                    }
                  return versionProps}

def getCode(Properties versionProps) {
         return versionProps['build.version'].toInteger()}

def getName(Properties versionProps) {
         return  versionProps['product.version']}


assembleRelease << {
         def versionPropsFile = file('version.properties')
         def code
         def Properties versionProps
         if (versionPropsFile.canRead()) {
                versionProps = new Properties()
                versionProps.load(new FileInputStream(versionPropsFile))
                code = versionProps['build.version'].toInteger() + 1
         } else {
             throw new GradleException("Could not read version.properties!")
         }

         versionProps['build.version'] = code.toString()
         versionProps.store(versionPropsFile.newWriter(), null)
         project.android.defaultConfig.versionCode code}

allprojects {
repositories {
    jcenter()
    mavenCentral()
    flatDir {
        dirs 'libs'
    }

    maven {
        ...
    }
}}

dependencies {...}

apply plugin: 'com.google.gms.google-services'

答案 1 :(得分:1)

找出你必须放置gradle.properties的位置

提取整个签名配置 第一种选择是将整个签名配置提取到单独的用户文件。在gradle.properties中创建一个新属性:

<强> MyProject.signing = /家庭/用户名/ .signing / myproject的

MyProject不需要匹配任何应用程序名称左右,事实上,您可以随意命名该属性。此外,如果你在Windows上使用\而不是/.

将您的密钥库放在/home/username/.signing/myproject.keystore。

现在在/home/username/.signing中创建一个文件名myproject.gradle(如有必要,创建该文件夹)。此文件将包含您的签名配置,该配置应用于对包进行签名。这可能如下所示:

android {
  signingConfigs {
    release {
      storeFile file(project.property("MyProject.signing") + ".keystore")
      storePassword "mypassword"
      keyAlias "KeyAlias"
      keyPassword "mypassword"
    }
  }

  buildTypes {
    release {
      signingConfig signingConfigs.release
    }
  }
}

现在是时候在要使用此签名配置的项目中配置实际的build.gradle文件了。只需添加以下行:

if(project.hasProperty("MyProject.signing")
    && new File(project.property("MyProject.signing") + ".gradle").exists()) {
  apply from: project.property("MyProject.signing") + ".gradle";
}

仅从文件中提取一些变量

project.ext {
  uploadRepo = 'http://....'
  uploadUser = 'myusername'
  uploadPass = 'mypass'
}

现在确保只有在加载文件后将配置放在if中才能使用变量。所以你的build.gradle看起来像这样:

if(project.hasProperty("MyProject.signing")
    && new File(project.property("MyProject.signing") + ".gradle").exists()) {
  apply from: project.property("MyProject.signing") + ".gradle";

  // Configure stuff that relies on these variables
  uploadArchives {
    repositories.mavenDeployer {
      repository(url: uploadRepo) {
        authentication(userName: uploadUser, password: uploadPass)
      }
    }
    // .. whatever else you need ...
  }

}

仅从文件中提取一些字符串 再次添加一个到gradle.properties的路径(让我们这次做一个完整的路径):

<强> MyProject.properties = /家庭/用户名/ .signing / myproject.properties

if(project.hasProperty("MyProject.properties")
    && new File(project.property("MyProject.properties")).exists()) {

  Properties props = new Properties()
  props.load(new FileInputStream(file(project.property("MyProject.properties"))))

  android {
    signingConfigs {
      release {
        storeFile file(props['keystore'])
        storePassword props['keystore.password']
        // ...
      }
    }
  }
}

只需在 /home/username/.signing/myproject.properties:

生成普通属性文件

<强>密钥库= /路径/到/我/密钥库 keystore.password =输入mypassword