2016-03-16 25 views
10

Desidero passare una variabile test che ho impostato in modo diverso per flavor come definito per NDK. Ma per qualche ragione passa sempre il valore dell'ultimo sapore.Come impostare la variabile in base ai sapori gradle

Ecco la build.gradle:

apply plugin: 'com.android.library' 

def test 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 
    defaultPublishConfig "flavorARelease" 
    publishNonDefault true 

    defaultConfig { 
     minSdkVersion 15 
     targetSdkVersion 17 

     ndk { 
      moduleName "test" 
      ldLibs "log" 
     } 
    } 

    productFlavors {  
     flavorA { 
      test = 1 
     } 

     flavorB { 
      test = 2 
     }  
    } 

    buildTypes {  
     debug { 
      ndk { 
       if (cFlags == null) { cFlags = "" } 
       cFlags = cFlags + " -DLOGGING=1 -DTEST="+test+" " 
      } 
      minifyEnabled false 
     } 
     release { 
      ndk { 
       if (cFlags == null) { cFlags = "" } 
       cFlags = cFlags + " -DLOGGING=0 -DTEST="+test+" " 
      } 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
} 

E qui sono le linee CFLAG dalla Android.mk generato

build/intermedi/NDK/flavorA/debug/Android.mk:

LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2 

mi aspettavo -DTEST=1 qui

build/intermedi/NDK/flavorB/debug/Android.mk:

LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2 

Allora, dove è il mio errore? O come posso raggiungere il mio obiettivo? Si prega inoltre di considerare che il vero problema è più complesso e, se possibile, voglio renderlo definito nel segmento "buildTypes".

risposta

8

ho trovato la soluzione:

Prima invece di def test specificare un nuovo campo per tutti i productFlavors

productFlavors.all { 
    ext.dTest = null 
} 

Allora questo campo è impostato in ogni sapore (codice invariato)

productFlavors {  
    flavorA { 
     dTest = 1 
    } 

    flavorB { 
     dTest = 2 
    }  
} 

E finalmente puoi farlo in buildTypes

buildTypes {  
    all { 
     productFlavors { 
      all { 
       ndk { 
        if (cFlags == null) { cFlags = "" } 
        cFlags = cFlags + " -DTEST="+dTest+" " 
       } 
      } 
     } 
    } 
    debug {   
     minifyEnabled false 
     ndk { 
      if (cFlags == null) { cFlags = "" } 
      cFlags = cFlags + " -DLOGGING=1 " 
     } 
    } 
    release { 
     minifyEnabled true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

     ndk { 
      if (cFlags == null) { cFlags = "" } 
      cFlags = cFlags + " -DLOGGING=0 " 
     } 
    } 
} 

Qui il file completo:

apply plugin: 'com.android.library' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 
    defaultPublishConfig "flavorARelease" 
    publishNonDefault true 

    defaultConfig { 
     minSdkVersion 15 
     targetSdkVersion 17 

     ndk { 
      moduleName "dTest" 
      ldLibs "log" 
     } 
    } 

    productFlavors.all { 
     ext.dTest = null 
    } 

    productFlavors {  
     flavorA { 
      dTest = 1 
     } 

     flavorB { 
      dTest = 2 
     }  
    } 


    buildTypes {  
     all { 
      productFlavors { 
       all { 
        ndk { 
         if (cFlags == null) { cFlags = "" } 
         cFlags = cFlags + " -DTEST="+dTest+" " 
        } 
       } 
      } 
     } 
     debug {   
      minifyEnabled false 
      ndk { 
       if (cFlags == null) { cFlags = "" } 
       cFlags = cFlags + " -DLOGGING=1 " 
      } 
     } 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

      ndk { 
       if (cFlags == null) { cFlags = "" } 
       cFlags = cFlags + " -DLOGGING=0 " 
      } 
     } 
    } 

} 
+0

** test si estende ConventionTask **. Pertanto, i prodotti productFlavors non verranno creati poiché si utilizza una parola chiave riservata come variabile. –

+0

Non sono sicuro che tu faccia riferimento alla domanda o alla soluzione. Nel mio vero progetto non è 'test', ma un insieme di variabili diverse. Se questo esempio è rotto a causa dell'uso di 'test', puoi ripararlo? Come sono davvero arrugginito di gradle di nuovo da quando ho lottato con questo problema molto tempo fa. - Cambiare tutte le occorrenze di 'test' su' dTest' è già un trucco? – Torge

+0

Mi riferivo al ** productFlavors **, dove stavi dichiarando 'Test'. Sono andato avanti e ho rinominato la variabile. Grazie! –

3

È possibile utilizzare buildConfigField

productFlavors { 
    demo { 
     buildConfigField "int", "FOO", "1" 
     buildConfigField "String", "FOO_STRING", "\"foo1\"" 
    } 
    full { 
     buildConfigField "int", "FOO", "2" 
     buildConfigField "String", "FOO_STRING", "\"foo2\"" 
    } 
} 
+1

E come accetteresti questo da C in #ifdef? Funziona solo per Java AFAIK. – Torge