Is there a way to share buildTypes and signConfigs with the root build.gradle file?

My current project is set up with a "base project" containing the main code for my applications. Then I have three versions of the applications based on this "base project". Two of these app versions have flavors and the other does not. This is the main structure of my project:

build.gradle (root)
   --->build.gradle (Base project)
       ---> build.gradle (Version 1)
            ---> V1 Flavor 1
            ---> V1 Flavor 2
       ---> build.gradle (Version 2)
            ---> V2 Flavor 1
            ---> V2 Flavor 2
       ---> build.gradle (Version 3, no flavors)

      

The main problem is that when you switch between debug and release assembly I have duplicates buildTypes

and signingConfigs

all my files build.gradle

. I have this in all my files build.gradle

:

signingConfigs {
    debug {
        storeFile debug.keystore
    }
    release {
        storeFile -REMOVED-
        storePassword -REMOVED-
        keyAlias -REMOVED-
        keyPassword -REMOVED-
    }
}

buildTypes {
    debug {
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
    release {
        minifyEnabled true
        proguardFiles 'proguard-project.txt'
        signingConfig signingConfigs.release
    }
}

      

There are two main problems with this: I need to manually switch base project, version 1 project and application project, either debug or release under Build Options in Android Studio, instead of just selecting the debug version or release in the application project. If I were trying to build V1 Flavor 1

, I would have to select release

in this application, in Version 1, and a base project in Build Variants to build it.

Is there a way for my library files to build.gradle

inherit buildType

and signingConfig

and also only select the build type of the application I'm building and not change the library build type too? Library projects still need to be run through proguard when a release is also selected.

+3


source to share


1 answer


I haven't found a great way to deal with this, but I found a somewhat better way than duplicating buildTypes and signConfigs in every app / taste. Below are the basic steps of what I've used to achieve this, but by no means the best or correct way to do it. This was the only way to find what I was trying to achieve (sort of). If anyone else has any further ideas on how to do this better please add another answer!



buildscript {
    // Buildscript items here
}

allprojects {
    repositories {
        // Maven repos, jcenter, etc go here
    }

    // Common build attributes for every build type
    ext.ANDROID = { project ->    
        project.android {
            compileSdkVersion 'Google Inc.:Google APIs:22'
            buildToolsVersion '22.0.1'

            defaultConfig {
                targetSdkVersion 22
                minSdkVersion 10
            }

            signingConfigs {
                release {
                    storeFile STORE_FILE
                    storePassword STORE_PASSWORD
                }
            }

            buildTypes {
                release {
                    minifyEnabled true
                    proguardFiles getDefaultProguardFile('proguard-android.txt')
                    signingConfig signingConfigs.release
                }
            }
        }
    }

    // Common flavor attributes
    ext.ANDROID_PRODUCT_FLAVORS = { project ->
        android {
            productFlavors {
                // Flavor configs in here
            }
        }
    }
}

project(':library1') {
    apply plugin: 'com.android.library'

    dependencies {
        // Library dependencies here
    }

    // Only need to include the base 'android' items for this library
    project.ANDROID(project)
}

// App with no flavors
project(':app1') {
    apply plugin: 'com.android.application'

    dependencies {
        compile project(':library1')
    }

    // Only need to include the base 'android' items for this app
    project.ANDROID(project)
}

// App with many types of flavors
project(':app2') {
    apply plugin: 'com.android.application'

    dependencies {
        compile project(':library1')
    }

    // Need to include the base 'android' items AND 'product_flavors' items for this app
    project.ANDROID(project)
    project.ANDROID_PRODUCT_FLAVORS(project)
}

      

+1


source







All Articles