解惑AndroidAndroid开发经验谈Android开发

多人协作时gradle配置优化

2018-05-19  本文已影响68人  codeKeeper

我们都知道android项目采用的是一套gradle构建机制通过android studio,我们可以很方便的对gradle进行配置从而灵活高效的完成项目的编译和打包。一般android的项目构建配置如下:


gradle配置

从图中我们可以看到,主要是.gradle文件和.properties文件。我们分别来看些配置文件的内容和用途。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这里主要定义了android插件的版本号,以及你项目中一些依赖库的仓库地址。这里android插件的版本号一般跟开发者本机所安装的android studio插件版本号一致。所以在多人协作的时候,由于每个开发人员所使用的android studio版本不一致,而导致在使用持续集成工具打包时需要频繁提交对该文件的修改,这样会造成提交日志的冗余,因为这些提交并没有包含多少有效信息,同时每个开发者在同步版本库的配置时都要重新修改一次这个文件也比较繁琐。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xiaofei.gradleconfigtest"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这里主要定义了每个module的编译和打包配置,比如compileSdkVersion定义了编译项目使用的android版本,targetSdkVersion定义了项目可以兼容的最高android系统版本,implementation 'com.android.support:appcompat-v7:27.1.1'定义了该模块依赖的support包的版本号。这里同样会存在多人协作时配置不一致的情况,比如compileSdkVersion,targetSdkVersion等。

#Sat May 19 19:44:18 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

该文件我们一般不会将它放到版本管理中

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

include ':app'

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk

由于build.gradle文件中一些配置在多人协作时会被频繁修改,所以我们需要将这些配置提取出来使得每个开发者的修改不影响到版本库中的配置,有以下两种方法:

项目根目录的build.gradle文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        def ver = '3.1.2'//版本库中的默认配置,一般只有管理员才能修改
        if (hasProperty('androidPluginVer')) {//如果配置文件中定义了该属性,则使用,否则使用默认配置
            ver = getProperty('androidPluginVer')
        }
        classpath "com.android.tools.build:gradle:$ver"
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块的build.gradle文件

apply plugin: 'com.android.application'

android {
    def sdkVer = 26 //版本库中的默认配置,一般只有管理员才能修改
    if (hasProperty('sdkVersion')) {//如果配置文件中定义了该属性,则使用,否则使用默认配置
        sdkVer = getProperty('sdkVersion')
    }
    compileSdkVersion sdkVer
    defaultConfig {
        applicationId "com.xiaofei.activityhide"
        minSdkVersion 15
        targetSdkVersion sdkVer
        versionCode 1
        versionName "1.0"
        if (rootProject.hasProperty("developer")) {
            versionName = developer
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

gradle.properties文件

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#android插件版本号,一般与android studio版本号一致
androidPluginVer=3.1.2
#编译sdk版本号
sdkVersion=26

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$androidPluginVer"
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    loadPropertyFile = { name ->
        Properties properties = new Properties()
        InputStream is = null
        try {
            is = new FileInputStream(name)
            properties.load(is)
        } catch (Throwable tr) {
            tr.printStackTrace()
            println tr.getMessage()
        } finally {
            if (is != null) {
                try {
                    is.close()
                } catch (Throwable tr) {
                    tr.printStackTrace()
                }
            }
        }

        return properties
    }

    Properties myProperties = loadPropertyFile('me.properties')
    androidPluginVer = '3.1.2'//版本库中的默认配置,一般只有管理员才能修改
    if (myProperties.hasProperty('androidPluginVer')) {//如果配置文件中定义了该属性,则使用,否则使用默认配置
        androidPluginVer = myProperties.getProperty('androidPluginVer')
    }
}

me.properties文件,放在项目根目录下。

#android插件版本号,一般与android studio版本号一致
androidPluginVer=3.1.2

通过以上两种方法就可以灵活的管理自己的个人配置,而不再担心与版本库的配置发生冲突啦。

参考文档

上一篇下一篇

猜你喜欢

热点阅读