有没有办法从根构建共享buildTypes和signingConfigs ?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)

主要问题是,当在调试和发布构建之间切换时,我在所有build.gradle文件中都有重复的buildTypessigningConfigs。我在所有的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
    }
}

这两个主要问题是,我需要手动切换到基础项目,版本1项目和应用程序项目要么调试或发布在Android Studio的构建变体部分,而不是只是选择调试或发布在应用程序项目。如果我试图构建V1 Flavor 1,我必须在该应用程序上选择release,在版本1上,并在build variables中为其构建基础项目。

是否有办法让我的库项目的build.gradle文件继承buildTypesigningConfig,以及只需要选择我正在构建的应用程序的构建类型,而不是改变库构建类型?当选择发行版时,库项目仍然需要通过proguard运行。

我还没有找到一个很好的方法来处理这个问题,但我确实找到了一个比在每个应用程序/风味中重复buildTypes和signingConfigs更好的方法。以下是我用来完成这一任务的基本步骤,但这绝不是最好或正确的方法。这是我能找到的实现我想要达到的目标的唯一方法。如果其他人对如何更好地完成它有任何进一步的想法,请添加另一个答案!

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

最新更新