Beta Testing Your Android App With Build Variants
In this blog post, we are going to learn how to use the build variants feature of Android Studio to create a simple project which is built in two flavors. The build environment will be configured such that each flavor can be built using either a release or debug build type.
Join the DZone community and get the full member experience.
Join For FreeDevelopers are always excited about releasing their apps quickly without testing it very well and making sure that things work out fine before the final release. As a Developer, you need to ship your app to beta users or testers first and get their feedbacks and improve your app. Software release life cycle are the stages of development you software goes through. Its starts from its initial development to its eventual release.
Stages of a typical software release cycle goes like this: Pre-alpha -> Alpha -> Beta -> Release candidate -> Gold. The main goal of beta testing is to get real-world exposure and to know how your app will feel in the hands of actual users. Google Play Store has a very useful functionality for developers to release their apps in Beta and invite people to test it.
In this blog post, we are going to learn how to use the build variants feature of Android Studio to create a simple project which is built in two flavors (just difference in color background). The build environment will be configured such that each flavor can be built using either a release or debug build type.
What Are Build Variants?
With Build Variants, your app can have multiple versions using the same source code. Though inside the source code, you might want to tweak it to have different functionalities or view for a particular variant to work a particular way. With just one single app, you can have multiple builds types such as debug and release build type. A particular build variant might have a different set of device requirements such minimum API etc. Build variants are the result of Gradle (I wrote an article on Gradle, you can visit it here) using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors.
Why Would You Want to Have Multiple Build Types?
You app might want to use a different API endpoints for debugging and another API endpoints for production or due to limitation on different app stores such as Amazon and Google Play, your app needs to work differently for example Google Play Services is not available on the Amazon app store but it is on the Google Play store, so this becomes very handy in removing the service completely on the Amazon app store and leaving it on the Google Play store. Built variants help you to create different composable pieces of your application in the Android build system. These build types are combined with flavors if you want to, in which you might want to change the look and feel of the app like the background colors, layouts, etc. Another useful usage is if you want different functionalities for your app that has a free and paid version.
We are going to build a simple Lagos Zombie Apocalypse app that uses build types and build flavors using Android Studio.
Steps:
- Create a new project in Android Studio, name LagosApocalypse, enter the usual company domain and all.
- Select Empty activity and click on the Next button.
- Leave activity name as MainActivity and layout as activity_main and click Finish button.
- Open the strings.xml file and add some extra strings
<resources> <string name="app_name">LagosApocalypse</string> <string name="button_send">SEND ZOMBIES!</string> <string name="town">The town name</string> <string name="really_send_zombies">YES</string> </resources>
- Create a button.xml file inside res/drawable folder. It will draw a rectangle orange button.
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/holo_orange_dark"/> <size android:width="120dp" android:height="80dp"/> </shape>
- Create a background.xml inside the res/drawable folder, it will display a gradient blue background.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:angle="90" android:startColor="@android:color/holo_blue_light" android:endColor="@android:color/holo_blue_bright" android:type="linear" /> </shape> </item> </selector>
- Modify the activity_main.xml so that it looks like this:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/background" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:id="@+id/et_city_name" android:layout_marginTop="38dp" android:textSize="32sp" android:gravity="center" android:hint="@string/town" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/main_button_send" android:background="@drawable/button" android:text="Send Zombies" android:textColor="@android:color/white" android:layout_gravity="center" android:layout_width="200dp" android:layout_height="200dp" /> </FrameLayout>
- Add an on click listener on the MainActivity class and implement the onClick method.
package com.kolocoder.lagosapocalypse; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.main_button_send).setOnClickListener(this); } @Override public void onClick(View view) { String town = ((EditText)findViewById(R.id.et_city_name)).getText().toString(); if (getString(R.string.really_send_zombies).equals("YES")){ Toast.makeText(this, String.format( "TEST send Zombies to %s", town), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, String.format( "Send Zombies to %s", town), Toast.LENGTH_SHORT).show(); } } }
- Select the app folder, Then, click the Build menu and select the Edit flavors.
- The list only contains a defaultConfig. Click on the + button to add a new flavor. Name it blueFlavor and give it the same values as min SDK version and target SDK version as is the case with defaultConfig.
- For the application id field, use the package name + the extension .blue.
- Enter the version code and version name for this flavor and click on the OK button.
- Repeat step 10 to 12 for another flavor. Name that flavor greenFlavor.
- Now your build.gradle file should be like this:
- Go to the Project panel, select the src folder under the app folder. Create a new folder and name it blueFlavor. Add a new res folder and within that folder, create another one called drawable. Note that you can maintain the same structure as main folder.
- Also, do the same for the greenFlavor build’s flavor. The project structure will be like this:
- Copy the background.xml and button.xml files from the /main/res/drawable folder and paste them in the blueFlavor/res/drawable folder.
- Repeat this for greenFlavor and open the background.xml file in the greenFlavor/res/drawable folder. Modify its content. We will be using a gradient green color for the background.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:angle="90" android:startColor="@android:color/holo_green_light" android:endColor="@android:color/holo_green_dark" android:type="linear" /> </shape> </item> </selector>
- Inside the same folder, open the button.xml file and make the drawable appear in green as well.
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/holo_green_dark"/> <size android:width="120dp" android:height="80dp"/> </shape>
- Create a new folder and call it debug in the app/src folder.
- Inside the debug folder, create a res folder and within the folder, create a values folder.
- Copy strings.xml file from the main/res/values folder and paste it inside debug/res/values folder.
- Open the strings.xml file and modify the really_send_zombies string resource value to NO.
- Now open the Build Variants panel by going to Build->Select Build Variant.
- Let's choose the greenFlavorDebug build variant and run the app.
- Also select the blueFlavorDebug and run the app again.
References and Further Reading
https://developer.android.com/studio/build/build-variants.html
Published at DZone with permission of Chike Mgbemena. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments