2015-05-29 30 views
3

我目前的项目是建立一个“基础项目”,其中包含我的应用程序的很多核心代码。然后我有三个基于这个“基础项目”的应用程序版本。这些应用程序版本中有两个具有风格,一个不具有风格。这是我的项目具有的基本结构:有没有办法从root build.gradle文件共享buildTypes和signingConfigs?

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.gradle文件的方式来继承buildTypesigningConfig以及仅不必选择我正在创建的应用程序的构建类型,没有太多更改库构建类型?当选择发行版时,库项目仍然需要通过proguard运行。

回答

1

我还没有找到处理这个问题的好方法,但是我发现在每个应用程序/风格中找到了比重复的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) 
} 
+0

它的帮助,但仍然不便,无论如何,感谢分享。 – VinceStyling

相关问题