Android uses Gradle unified configuration dependent version

Source: Internet
Author: User

Preface

The current Android development in order to reduce compilation time, development efficiency, most of the adoption of modular, component-based development. In this way, it is inevitable that more than one library will be used. So how do we deal with everyone's version unification when we develop it together?

I think most people should be using gradle to rely on management, has not been used to wall detention, gradle use is simply too good, for example, we want to rely on a SUPPORT-V4 package, a direct sentence:

Compile ' com.android.support:support-v4:25.1.0 '
but I don't know if you've found any problems? For example, after the V4 version number if the upgrade? You may say that it is better to manually change the version number, without much effort. Yes, only this place is of course very simple, assuming that your project refers to the three or four module, and these module also refers to the V4 package, it is necessary to all dependent V4 package All manually changed, this is a project, and if you are more than one project? This is just a V4 package, assuming that there are many places to rely on okhttp? And the next time all the Compilesdkversion and buildtoolsversion to follow the upgrade? It's too troublesome to manually change every version update, and it's easy to miss out on some places.

Let's look at the usual way:


This project has the main project app and a module modulebase, for the build.gradle file in the module directory:

Apply plugin: ' Com.android.application ' android {    compilesdkversion    buildtoolsversion "25.0.0"    defaultconfig {        ApplicationID "com.liyunlong.gradleconfig"        minsdkversion        targetsdkversion        Versioncode 1        versionname "1.0"    }    buildtypes {        release {            minifyenabled false            Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '        }    }}dependencies {    Compile Filetree (dir: ' Libs ', include: [' *.jar '])    compile ' com.android.support:appcompat-v7:25.1.0 '    compile ' com.android.support:design:25.1.0 '    compile ' com.android.support:recyclerview-v7:25.1.0 '}

This repeated configuration, do we need to manually configure each new libs to be the same? Is there a better way? So today we will provide a solution to solve this problem completely. Our fundamental solution to this problem is to manage all gradle dependencies through a single configuration file. So how do we do that?

    • The first step: Create a config.gradle file under the root of the main project
    • Step Two: Write the configuration information we need to the config.gradle file with the following configuration information:
Note: The version number we have here corresponds to the previous master project as Libs.
    • Step Three: add a line of code to the build.gradle file in the main project root directory:
Apply from: "Config.gradle"
Specific Example:

    • Fourth Step: reference Our path configuration in all module of the project. Such as:

At this point, we have finished, all of our libs can be used in this configuration, one configuration, lifelong enjoyment. Isn't it very simple and convenient? After all the version number or configuration changes to change the file directly, and can be used in all projects or may be used in the third-party libraries are plugged here to manage, with the use of reliance on, not to put inside.

Sample Download

Android uses Gradle unified configuration dependent version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.