Each version of the build variant represents each version that you can build. Although you are not directly configuring build variants, you can configure build type and product flavor.
For example, a demo product flavor can declare different features and device requirements, such as custom source code, resources, and minimal APIs. However, the build type of debug has different build and package settings, such as debug options and signature files.
Configuration Build Types
You can create and configure build types in the android{} chunks in the module's Build.gradle file. When you create a new module, Android Studio automatically creates debug and release for you. Although the debug build type does not appear in the build configuration file, Android Studio is configured with Debuggable true. This will allow you to debug the app on a secure Android device and sign the APK with a generic debug keystore.
If you want to add or higher determined settings, you can add the debug build type to your configuration. The next example declares a applicationidsuffix for the debug build type. and configure a Jnidebug build type, set it to initialize according to the debug build type.
Android {...Defaultconfig {...} buildtypes {release {minifyenabled true proguardfiles getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '} Debug {Applicationidsuffix". Debug"}/** * the' Initwith 'property allows-copy configurations from the other build types, * so you don' t has to configure one from the beginning. You can then configure * just the settings your want to change. The following line initializes * 'Jnidebug' Using the Debug build type, and changes only the * Applicationidsuffix and Versionnamesuffix settings. */Jnidebug {//This copies the Debuggable attribute and debug signing configurations. Initwith Debug Applicationidsuffix ". Jnidebug" Jnidebuggable True}}}
Configuring Product Flavor
Creating the product flavor is the same as creating the build type: add it to the productflavors{} chunk and configure the settings you want. Product Flavors supports defaultconfit because defaultconfig belongs to the Profuctflavor class. This means that you can provide a basic configuration for all flavors in defaultconfig{}. Each flavor can replicate any of these default values. Like ApplicationID.
Note: You still need to declare the package name in the packages attribute in the manifest file.
Here is a code example that creates a demo and full product flavor, and sets their own ApplicationID and Versionname.
android { ... defaultConfig {...} buildTypes {...} productFlavors { demo { "com.example.myapp.demo" "1.0-demo" } full { "com.example.myapp.full" "1.0-full" } }}
After you have created and configured your product flavor, click the Sync Now button that appears in the message prompt bar. After the synchronization is complete, Gradle automatically creates build variants based on your build type and product flavor, and uses . For example, if you create a demo and full two product flavor, and you keep debug and release as the default, Gradle creates the following build variants:
1, Demodebug
2, Demorelease
3, Fulldebug
4, Fullrelease
Create a source sets for build variants
By default, Android Studio creates Main/source set and directories that are shared across all build variants. However, you can create a new source sets to control Gradle compilation and a specific build types,product flavors and build variants. For example, you can define basic functionality in Main/source set and use the product flavor source sets to change things in your app, including permissions and logs.
Gradle wants you to organize the source set files and directories in a certain way.
The Android Gradle plugin provides a very useful gradle task to show you how to organize your files for the type,product flavor and build variants. For example, the following report section describes the specific files that Gradle would like to find the build type of debug:
----------------------------------- ------------------------- project:appdebug Compile configuration: Compilebuild.gradle name:android.sourceSets. debug Java sources: [App/src/debug /java]manifest file:app/src/ debug /androidmanifest.xmlandroid resources: [app/src/ debug /res]assets: [App/src/debug /assets] Aidl sources: [App/src/debug /aidl]renderscript sources: [App/src/debug /rs]jni sources: [App/src/debug /jni]jni Libraries: [ App/src/debug /jnilibs]java-style resources: [app/src/ debug /resources]
To generate and view a report of your build configuration, follow these steps:
1. Click the IDE window on the right.
2. Find MyApplication > Tasks > Android, double-click sourcesets
3. To view the report, click Gradle Console at the bottom of the IDE window
When you create a new build Variant,android studio will not create a source set directory for you, but will give you some options to help you. For example, create a java/directory for your debug build type:
1. Open the Project panel and select Project View
2. Find MYPROJECT/APP/SRC
3. Right-click the src directory and select New > Folder > Java folder
4. Locate the target Source Set in the drop-down and select Debug
5. Click Finish
Android Studio creates a source set directory for your debug build type, and then creates a java/directory inside. You can also have Android studio create the appropriate directory for a new file created for a specific build variant. For example, create a values XML file for your debug build type:
1. In the project panel, right-click the src directory and select New > XML > Values XML File
2. Enter the name of the XML file, or leave the default.
3. Locate the target Source Set in the drop-down and select Debug
4. Click Finish
Because the debug build type is specific to the source Set,android Studio creates the necessary directories automatically when the XML file is created.
This article Song Zhihui
Personal Weibo: Click to enter
4.1. Android Stuido Configure your build Variant