快速配置Android项目里版本信息

快速配置Android项目里版本信息

  • 在项目根目录创建配置文件version.gradle,加入以下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    def androidSupportVersion = '26.0.0'
    def androidConstraintLayout = '1.0.2'

    ext {
    versionCode = 1
    versionName = "1.0"
    compileSdkVersion = 26
    buildToolsVersion = "26.0.1"
    minSdkVersion = 16
    targetSdkVersion = 26
    appcompatV7 = "com.android.support:appcompat-v7:$androidSupportVersion"
    design = "com.android.support:design:$androidSupportVersion"
    constraintLayout = "com.android.support.constraint:constraint-layout:$androidConstraintLayout"
    }

    allprojects {
    repositories {
    jcenter()
    maven {
    url "https://maven.google.com"//以后的sdk都要从google自己的maven仓库中获取
    }
    }
    }
  • 在根目录的build.gradle文件中引入刚创建的配置文件,在文件顶部加入以下内容:

    1
    apply from:'version.gradle'
  • 在具体Module的build.gradle文件中通过如下方式来使用刚定义好的版本信息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    android {
    compileSdkVersion getProject().compileSdkVersion
    buildToolsVersion getProject().buildToolsVersion
    defaultConfig {
    applicationId "com.tedxiong.android"
    minSdkVersion getProject().minSdkVersion
    targetSdkVersion getProject().targetSdkVersion
    versionCode getProject().versionCode
    versionName getProject().versionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    externalNativeBuild {
    cmake {
    cppFlags "-frtti -fexceptions"
    }
    }
    ndk {
    // Specifies the ABI configurations of your native
    // libraries Gradle should build and package with your APK.
    abiFilters 'armeabi-v7a'
    }
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    externalNativeBuild {
    cmake {
    path 'CMakeLists.txt'
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile getProject().appcompatV7
    compile getProject().constraintLayout
    compile getProject().design
    testCompile 'junit:junit:4.12'
    }
0%