有没有办法从root build.gradle文件共享buildTypes和signingConfigs?

时间:2015-05-29 20:50:35

标签: android gradle android-gradle build.gradle

我目前的项目设置了一个"基础项目​​"包含我的应用程序的许多核心代码。然后我有三个版本的应用程序基于"基础项目​​"。其中两个应用程序版本有风味,一个没有。这是我的项目的基本结构:

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)

主要问题是,在调试和发布版本之间切换时,我的所有buildTypes文件中都有signingConfigsbuild.gradle。我在所有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
    }
}

这样做的两个主要问题是我需要在Android Studio的Build Variants部分中手动切换基础项目,版本1项目和app项目,以进行调试或发布,而不是仅选择调试或发布在应用程序项目上。如果我正在尝试构建V1 Flavor 1,我必须在该版本1的应用上选择release,并在Build Variants中选择基础项目来构建它。

我的图书馆项目有办法吗? build.gradle文件要继承buildTypesigningConfig,只需要选择我构建的应用的构建类型,而不是更改库构建类型?当选择发布时,库项目仍然需要通过proguard运行。

1 个答案:

答案 0 :(得分:1)

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