Hi..! This blog is going to be pretty small and may be a little interesting, i hope, if not much.

There are many time we combine multiple modules into the main app inorder to get going a functionality, or rather we integrate several modules incase they do not have a gradle path available.

I was recently working with a custom date picker library and with it came several different modules: the calendar library and also the number picker. So the structure was as under:

  1. Main application --> has a build.gradle
    |
    |
    -----------> Datepicker library as a dependency --> has a build.gradle file
  2. Date picker library has the following dependencies:
    |
    |
    ------------> Calendar view library --> has a build.gradle
    |
    |
    ------------> Number picker library --> has a build.gradle

This makes it a total of 5 build.gradle files (including the main build.gradle which has mavencentral and jcentre lines)

The aim of this blog then is to assist you in merging all of these into just one build.gradle file, reducing the total to 2. This not only reduces redundancy but it also makes it easy to manage everything into one file.

Deleting build.gradle files from the modules and simply adding dependencies of these modules will fire an error saying that “default” is not defined in app. Thus this solution will not work.

Let’s move to something that works:

I will take up the modules that i used as an example of what should be done, you can then experiment with your modules and dependencies. Ensure that every project (module) is an android studio module or rather gradle module. If not, convert the module to the structure followed by Android Studio to remove src.dir tags and also src.manifest tags.

Starting with the datepicker library, it has a build.gradle file as under:

 

    apply plugin: 'com.android.library'

    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    }

    dependencies {
        compile project(':numberpickerlibrary')
        compile project(':calendarviewlibrary')
    }

Similarly, the dependencies numberpickerlibrary and calendarviewlibrary had the following build.gradle files:

  1. numberpickerlibrary
    apply plugin: 'com.android.library'

    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    }

    dependencies {
        compile 'com.android.support:support-v4:23.3.0'
        compile 'com.android.support:appcompat-v7:23.3.0'
    }
  1. calendarviewlibrary
    apply plugin: 'com.android.library'

    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    }

As, you notice, there are a lot of lines that are repeating.

    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    }

Let’s move over to the main build.gradle file.

The main build.gradle file (one belonging to the app) looked like this:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    }

    defaultConfig {
        applicationId "com.example.packagename"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dependencies { 
        compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
        compile 'com.android.support:cardview-v7:24.0.0-alpha2'
        compile 'com.android.support:recyclerview-v7:24.0.0-alpha2'
        compile 'com.google.code.gson:gson:2.2.4'
        compile project(':datepickerlibrary')
    }

To this file, we then add the following (rather, we would be merging the build.gradle files into one)

for all the projects you could add the following outside the android block, just the way we have dependencies:

project(':datepickerlibrary') {
    apply plugin: 'com.android.library'
    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
    }
    dependencies {
        compile project(':numberpickerlibrary')
        compile project(':calendarviewlibrary')
    }
}

project(':calendarviewlibrary'){
    apply plugin: 'com.android.library'
    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
    }
}

project(':numberpickerlibrary') {
    apply plugin: 'com.android.library'
    android{
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
    }
    dependencies {
        compile 'com.android.support:support-v4:23.3.0'
        compile 'com.android.support:appcompat-v7:23.3.0'
    }
}

And now you are free to delete all the build.gradle files from your modules. And you will be left with just 2 build.gradle files.

Happy Coding!

Advertisements