2015-02-06 77 views
0

我有一個需要使用自定義軟件包和自定義配置文件構建的應用程序。我在網上看了一下,發現有關gradle flavors,但我無法弄清楚如何使用它們。使用自定義設置輕鬆構建相同的應用程序Android Studio

基本上我有一個應用程序包com.example.apps.myapp。我需要使用不同的包裝com.example.apps.myapp2和一個自定義配置類像com.example.config.myapp2.Configuration

我現在有這對構建相同的應用程序我build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "21.0.1" 

    defaultConfig { 
     applicationId "com.example.apps.myapp" 
     minSdkVersion 15 
     targetSdkVersion 20 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      runProguard false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    packagingOptions { 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/NOTICE.txt' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/LICENSE.txt' 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile project(':api') 
    compile project(':view-pager-indicator') 
    compile project(':MaterialEditText') 
    compile 'com.pkmmte.view:circularimageview:1.1' 
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3' 
    compile 'com.google.android.gms:play-services:6.5.87' 
    compile 'com.android.support:support-v4:21.0.3' 
    compile 'com.android.support:appcompat-v7:21+' 
    compile 'com.android.support:cardview-v7:21.+' 
    compile 'com.facebook.android:facebook-android-sdk:3.21.1' 
    compile 'com.mauriciogiordano:easydb:0.1.0' 
} 

多麼容易創建gradle這個任務或一件讓這些更改並構建應用程序? PS:我對gradle沒有任何認識。

謝謝。

回答

1

在build.gradle中聲明風味很容易。
只是從官方文檔Gradle Plugin User Guide。 對於不同的配置包括buildConfigField。您可以在代碼的任何部分從BuildConfig.FieldName中讀取此字段。

productFlavors { 
      flavor1 { 
       applicationId "com.example.apps.myapp" 
       buildConfigField "int", "CONFIG_INT", "123" 
       buildConfigField "String", "CONFIG_STRING", "\"ABC\"" 
      } 

      flavor2 { 
       applicationId "com.example.apps.myapp2" 
       buildConfigField "int", "CONFIG_INT", "456" 
       buildConfigField "String", "CONFIG_STRING", "\"QWE\"" 
      } 
     } 

並獲得場

BuildConfig.CONFIG_INT 
BuildConfig.CONFIG_STRING 

不要忘記按(右從保存)同步按鈕重建你BuildConfig新的領域。在最新版本的gradle中,可以聲明Android資源Watch there。從構建

開關flavoirs異體Android Studio中

Build Variants

+0

謝謝!那麼我該如何切換從一種味道到另一種? – 2015-02-06 22:43:29

+0

包名稱的小增加。使用** applicationId **。 – AndreyICE 2015-02-06 23:06:47

+0

謝謝。但是,如何選擇要構建的味道? – 2015-02-06 23:15:41

相關問題