2015-02-12 7 views
6

में एक बूलियन बदलने के लिए गड़बड़ी का कार्य मैं एक बहुत ही सरल कार्य बनाना चाहता हूं जो मेरे ग्रेडल कॉन्फ़िगरेशन में बूलियन बदलता है।बिल्ड कॉन्फ़िगरेशन

मैं एक एंड्रॉइड एप्लिकेशन पर काम करता हूं जिसे कई प्रोफाइल के साथ चलाया जा सकता है, और प्रत्येक के लिए यह निर्दिष्ट करने की आवश्यकता है कि मेरे कोड में ऐप को ब्लूटूथ नकली होना चाहिए या नहीं।

मेरे Gradle (प्रासंगिक कोड):

if (BuildConfig.fakeBluetooth) { 
    processFictiveBluetoothService(); 
} else { 
    // other case 
} 

कमांड लाइन में उपयोग के उदाहरण::

def fakeBluetooth = "true" 

buildTypes { 
    debug { 
     minifyEnabled false 
     signingConfig android.signingConfigs.debug 
     buildConfigField "boolean", "fakeBluetooth", fakeBluetooth 
    } 
    release { 
     minifyEnabled true 
     signingConfig android.signingConfigs.release 
     buildConfigField "boolean", "fakeBluetooth", fakeBluetooth 
    } 
} 

task noFakeBluetooth { 
    fakeBluetooth = "false" 
} 

मेरी जावा कोड में उपयोग का उदाहरण

./gradlew iDebug noFakeBluetooth 

और

./gradlew iDebug 

समस्या: दोनों मामलों में नकली ब्लूटूथ का मान हमेशा "सत्य" होता है (cmd लाइन में "नोफैकब्लूटूथ" के साथ या बिना)।

उत्तर

16

आप परियोजना गुण का उपयोग कर सकते मूल्य पारित करने के लिए:

buildTypes { 
    debug { 
     minifyEnabled false 
     signingConfig android.signingConfigs.debug 
     buildConfigField "boolean", "fakeBluetooth", fakeBluetooth() 
    } 
    release { 
     minifyEnabled true 
     signingConfig android.signingConfigs.release 
     buildConfigField "boolean", "fakeBluetooth", fakeBluetooth() 
    } 
} 

def fakeBluetooth() { 
    def value = project.getProperties().get("fakeBluetooth") 
    return value != null ? value : "true" 
} 

और फिर आप के साथ संपत्ति पारित कर सकते हैं:

./gradlew iDebug -PfakeBluetooth=true 
+0

धन्यवाद यह क्या मैं – psv

0

यह काम करता है

android.defaultConfig.buildConfigField "String", "value", "1" 
+0

किया है यह भी एक समाधान है। लागू करने के आधार पर ... धन्यवाद – psv

+0

मेरे मामले में मैं बस कार्यों के भीतर कॉन्फ़िगरेशन पैरामीटर सेट करने के लिए इसका उपयोग कर रहा हूं –

0

मैं सही लगता है दृष्टिकोण बिल्ड प्रकार या उत्पाद के लिए संसाधन मूल्य को परिभाषित करना होगा:

resValue "string", "key", "value"

फिर अपने कोड के अंदर से बाहर पढ़ने के लिए की तरह: getResources().getString(R.string.key);

संबंधित मुद्दे