2015-05-29 28 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

相關問題