विभिन्न संसाधनों के साथ Android स्वाद।

Apr 18 2023
यदि आप एक स्वाद बना रहे हैं तो आपको प्रत्येक स्वाद के लिए एक अलग संसाधन फ़ाइल की आवश्यकता हो सकती है। आज हम इस बारे में जानेंगे कि हम अलग-अलग रिसोर्स फाइल के साथ फ्लेवर कैसे सेट कर सकते हैं।

यदि आप एक स्वाद बना रहे हैं तो आपको प्रत्येक स्वाद के लिए एक अलग संसाधन फ़ाइल की आवश्यकता हो सकती है। आज हम इस बारे में जानेंगे कि हम अलग-अलग रिसोर्स फाइल के साथ फ्लेवर कैसे सेट कर सकते हैं।

सबसे पहले, आइए परिभाषित करें कि स्वाद क्या है। Android विकास में , एक फ़्लेवर एक ही ऐप का एक रूपांतर होता है जिसमें विभिन्न विशेषताएं, कॉन्फ़िगरेशन या व्यवहार हो सकते हैं। अलग-अलग क्षेत्रों, ग्राहकों या परिवेशों के लिए ऐप के कई संस्करण बनाने के लिए फ़्लेवर का उपयोग किया जा सकता है।

इस उदाहरण में, मान लें कि हम अपने ऐप के दो प्रकार बनाना चाहते हैं: एक निःशुल्क संस्करण और एक सशुल्क संस्करण। नि: शुल्क संस्करण में विज्ञापन होंगे, जबकि सशुल्क संस्करण विज्ञापन-मुक्त होगा और इसमें कुछ अतिरिक्त सुविधाएं होंगी।

दो फ्लेवर बनाने के लिए, हमें अपने प्रोजेक्ट की बिल्ड.ग्रेड फ़ाइल में कुछ बदलाव करने होंगे। फ़ाइल खोलें और android ब्लॉक के अंदर निम्न कोड जोड़ें:

android {
    // ... 

    flavorDimensions "version"
    productFlavors {
        free {
            applicationIdSuffix ".free"
            versionCode 1
            versionName "1.0"
            resValue "string", "app_name", "Free App"
            manifestPlaceholders = [
                    appIcon     : "@mipmap/ic_launcher_free",
                    appIconRound: "@mipmap/ic_launcher_free_round"
            ]
            buildConfigField 'String', 'BASE_URL',  "\"www.freeurl.com\""
        }
        paid {
            applicationIdSuffix ".paid"
            versionCode 1
            versionName "1.0"
            resValue "string", "app_name", "Paid App"
            manifestPlaceholders = [
                    appIcon     : "@mipmap/ic_launcher_paid",
                    appIconRound: "@mipmap/ic_launcher_paid_round"
            ]
            buildConfigField 'String', 'BASE_URL', "\"www.paidurl.com\""
        }
    }

  • flavorDimensionsस्वाद के बीच अंतर करने के लिए हम जिस आयाम का उपयोग कर रहे हैं उसे परिभाषित करता है। इस मामले में, हम "संस्करण" का उपयोग कर रहे हैं।
  • productFlavorsहमारे द्वारा बनाए जा रहे दो स्वादों को परिभाषित करता है: "मुफ़्त" और "सशुल्क"।
  • dimensionप्रत्येक स्वाद को उस आयाम से जोड़ता है जिसे हमने पहले परिभाषित किया था।
  • applicationIdप्रत्येक स्वाद के लिए अद्वितीय पैकेज नाम निर्दिष्ट करता है। इस मामले में, हम दो स्वादों के बीच अंतर करने के लिए ऐप के पैकेज नाम के अंत में ".free" और ".paid" जोड़ रहे हैं।
  • versionCodeऔर versionNameप्रत्येक स्वाद के लिए संस्करण संख्या निर्दिष्ट करें।
  • buildConfigFieldBuildConfig क्लास में एक फ़ील्ड जोड़ता है जो इंगित करता है कि ऐप सशुल्क संस्करण है या नहीं।

अपने ऐप को प्रोजेक्ट स्तर पर स्विच करने के बाद, ऐप/src फ़ोल्डर के अंदर प्रत्येक स्वाद नाम वाला फ़ोल्डर बनाएं। उदाहरण के लिए - मैंने दो फ्लेवर बनाए हैं, फ्री और पेड, इसलिए मैंने दो फोल्डर बनाए हैं।

अब आप सोच रहे होंगे कि फ्री फोल्डर के सामने [मेन] लिखा हुआ है क्योंकि मैंने बिल्ड वेरिएंट में से फ्रीडिबग वेरिएंट को चुना है।

बिल्ड वेरियंट को सेलेक्ट करने के बाद अपने फ्लेवर फोल्डर के अंदर Res फोल्डर बना लें फिर Res फोल्डर के अंदर अपनी जरूरत के हिसाब से सभी फोल्डर बना लें।

अब, यदि आप विशेष लेआउट, ड्रॉएबल और रंगों को बदलना चाहते हैं तो आपको स्वाद संसाधन फ़ोल्डर के अंदर संसाधनों की प्रतिलिपि बनाने की आवश्यकता है।

याद रखें कि नाम सभी स्वाद संसाधन फ़ाइलों के समान होना चाहिए।

अब, चलिए कोड साइड पर चलते हैं।

activity_main.xml (फ्री फ़्लेवर)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Welcome to Free flavor"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@drawable/ic_app_logo"
     />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="Free Flavor Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Welcome to Paid Flavor"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        android:text="Paid Flavor Button 1"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Paid Flavor Button 2"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_marginTop="23dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@drawable/ic_app_logo" />

</androidx.constraintlayout.widget.ConstraintLayout>

      
                
while selecting build variants you will notice that the image is changing while switching via build variant.

संदर्भ के लिए:https://github.com/surajnegi8909/CreateFlavor.git