2016年2月4日 星期四

為 Android Studio 加入編譯計數器


在 iOS 的開發過程中,我會加入編譯自動計數器;現在開發 Android 程式,我也希望有相同的做法。在 Android Studio 中不難做到,只要在 app 目錄下加入 version.properties,內容:

然後在 app/build.grade 加入如下的代碼:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.1"

    def major = "1"
    def minor = "00"
    def versionFile = file('version.properties')
    if (versionFile.canRead())  {

        //  Load properties file
        def Properties properties = new Properties()
        properties.load(new FileInputStream(versionFile))

        //  Get build count and update it
        def buildCountString = properties['BUILD_COUNT']
        def buildCount = buildCountString.toInteger()+1

        //  Save new value back to properties file
        properties['BUILD_COUNT'] = buildCount.toString()
        properties.store(versionFile.newWriter(), null)

        defaultConfig {
            applicationId "com.pacess.amigocontroller"
            minSdkVersion 17
            targetSdkVersion 21
            versionCode 1
            versionName major+"."+minor+"."+buildCount.toString()
        }

    }  else  {
        throw new GradleException("### Unable to read version.properties...")
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'info.hoang8f:android-segmented:1.0.6'
}

沒有留言: