Android Studio - Adding SDK versions and dependencies to all module

Viewed 745

In my currently working project contains lot of modules and dependency. So is there is any way to keep all this versions and dependency common and reuse in all modules. I know that we can define common dependency in root build.gradle file but whats about things like compileSdkVersions and exclude groups. My dependency some times include exclude group like.

  androidTestCompile ('com.android.support.test:rules:1.0.1'){
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

How can we handle this scenario? Once we have add in root build.gradle, Is there any way to add them all in a app module with out specifying individual ones like below.

compile deps.cardview
compile deps.design
compile deps.supportv4
compile deps.animation
compile deps.pagination
compile deps.shimmerlayout
compile deps.enhanced_card
compile deps.swipeanim
compile deps.appcompact
2 Answers

For things like compileSdkVersion, buildTypes and compileOptions, I define sth like this in root gradle file :

ext.android_settings_for_module = {
    compileSdkVersion COMPIlE_SDK_VERSION.toInteger()
    buildToolsVersion BUILD_TOOLS_VERSION

    defaultConfig {
        minSdkVersion 
        targetSdkVersion 
        versionCode 
        versionName 
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }
    compileOptions compile_options
    lintOptions lint_options
    testOptions test_options
}

ext.lint_options = {
    //butterKnife
    disable 'InvalidPackage'
}

ext.compile_options = {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

And then, in your module file, you can use :

android android_settings_for_module

Quite Similar for dependencies. Define a field in root gradle file :

ext.common_libs = [

]

And then use in module level gradle file :

dependencies {
    compile common_libs
}

For sharing common SDK version and Dependencies. You can define shared gradle dependencies in the library module, and if the app module has the library as a dependency, you won't need to specify everything twice. Taking this further, you could create a 'common' module that requires the shared gradle dependencies, and have both the app & library module require the common module.

have look :

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
}
}

// Load dependencies
apply from: 'dependencies.gradle'

here is dependencies.gradle: for all module and sdk version in app.

ext {
//Version
supportLibrary = '22.2.1'

//Support Libraries dependencies
supportDependencies = [
        design           :         "com.android.support:design:${supportLibrary}",
        recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
        cardView         :         "com.android.support:cardview-v7:${supportLibrary}",
        appCompat        :         "com.android.support:appcompat-v7:${supportLibrary}",
        supportAnnotation:         "com.android.support:support-annotations:${supportLibrary}",
]
}

Happy Coding!!

Related