多人协作时gradle配置优化

gradle

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

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

  • 第一个是项目根目录下的脚本配置文件,用行话来说就是rootProject的build.gradle。它的内容如下:
// 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版本不一致,而导致在使用持续集成工具打包时需要频繁提交对该文件的修改,这样会造成提交日志的冗余,因为这些提交并没有包含多少有效信息,同时每个开发者在同步版本库的配置时都要重新修改一次这个文件也比较繁琐。

  • 第二个是项目中一个模块的脚本配置文件,用行话来说就是subProject的build.gradle。它的内容如下:

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等。

  • 第三个为gradle工具自身的一些配置,如位置,发布地址等。内容如下:

#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

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

  • 第四个为项目混淆配置文件,一般在对外发布app时为了防止应用代码被破解会使用proguard工具将项目代码进行混淆处理。

  • 第五个为项目的属性配置文件,主要是对一些内置属性进行配置,如jvm参数等。内容如下:

# 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
  • 第六个为项目的脚本配置文件,主要是在gradle构建的初始化阶段告诉gradle该项目包含多少个module。内容如下:
include ':app'
  • 第七个为项目的属性配置文件,一般为android sdk的路径,ndk的路径等。内容如下:
## 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文件中一些配置在多人协作时会被频繁修改,所以我们需要将这些配置提取出来使得每个开发者的修改不影响到版本库中的配置,有以下两种方法:

  • 将这些配置,如android插件版本(com.android.tools.build:gradle:3.1.2),compileSdkVersion等,放到gradle.properties中。为什么要放到gradle.properties中,因为该文件中的配置在项目构建时会被自动加载,而且我们并不会将gradle.properties放到版本管理中,可以随意修改而不影响其他开发者。具体做法参考如下:

项目根目录的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
  • 将这些配置放到一个单独的配置文件中,如me.properties中,该文件不放到版本管理中,然后在项目根目录的build.gradle中加载该属性文件从里面取出配置。该做法和第一种做法的区别是修改me.properites之后android studio工具不会强制要求你做一次同步操作(sync project with gradle files)。具体参考做法如下:
    项目根目录的build.gradle文件
// 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

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

参考文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值