Android 有没有办法从root build.gradle文件共享构建类型和签名配置?
我当前的项目是用一个“基本项目”设置的,其中包含了我应用程序的许多核心代码。然后我有三个基于“基本项目”的应用程序版本。这些应用程序版本中有两个有味道,一个没有。这是我的项目的基本结构:Android 有没有办法从root build.gradle文件共享构建类型和签名配置?,android,gradle,android-gradle-plugin,build.gradle,Android,Gradle,Android Gradle Plugin,Build.gradle,我当前的项目是用一个“基本项目”设置的,其中包含了我应用程序的许多核心代码。然后我有三个基于“基本项目”的应用程序版本。这些应用程序版本中有两个有味道,一个没有。这是我的项目的基本结构: build.gradle (root) --->build.gradle (Base project) ---> build.gradle (Version 1) ---> V1 Flavor 1 ---> V1 Fla
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
文件中都有重复的buildTypes
和signingConfigs
。我所有的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项目和要调试或发布的应用程序项目,而不仅仅是在应用程序项目上选择debug或release。如果我试图构建v1flavor1
,我必须在该应用程序、版本1上选择release
,并在构建变体中选择要构建的基础项目
我的库项目的
build.gradle
文件是否有方法继承buildType
和signingConfig
以及只需选择我正在构建的应用程序的构建类型,而不必更改库构建类型?当选择发布版本时,库项目仍然需要通过proguard运行。我还没有找到一个很好的方法来处理这个问题,但我确实找到了一个比在每个应用/版本中使用重复的构建类型和签名配置更好的方法。下面是我用来实现这一目标的基本步骤,但决不是最好或正确的方法。这是我唯一能找到的方法来完成我想要达到的目标。如果其他人对如何以更好的方式实现它有任何进一步的想法,请添加另一个答案
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)
}
这有帮助,但仍然不便,无论如何,谢谢分享。