简体   繁体   中英

Is there a way to share buildTypes and signingConfigs from root build.gradle file?

My current project is setup with a "base project" containing a lot of the core code of my apps. Then I have three versions of apps based on that "base project". Two of these app versions have flavors and one doesn't. This is the basic structure that my project has:

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 switching between a debug and release build, I have duplicate buildTypes and signingConfigs in all of my build.gradle files. I have this in all of my build.gradle files:

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
    }
}

The two main problems with this is that I need to manually switch over the base project, version 1 project and the app project to be either debug or release in the Build Variants section of Android Studio, instead of just selecting debug or release on the app project. If I were trying to build V1 Flavor 1 , I'd have to select release on that app, on Version 1, and the base project in Build Variants for it to build.

Is there a way for my library projects' build.gradle files to inherit the buildType and signingConfig as well as only having to select the build type of the app I'm building, not change the library build type too? The library projects still need to be run through proguard when the release is selected too.

I haven't found a great way of handling this but I did find a somewhat better way than having duplicate buildTypes and signingConfigs in every single app/flavor. Below are the basic steps of what I used to accomplish this but is in no means the best or proper way of doing it. It was simply the only way I could find to accomplish what I was trying to achieve (kind of). If anyone else has any further ideas on how to accomplish it a better way 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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM