Our Story

Enterprise solutions to empower people.
Company was established in the year 2012. Provide high quality cost effective services to the community and businesses. Goldenmace is a company with young, energetic, curious and committed minds who are excited about solving complex business problems, ethically. We build long term relationships and provide quality products and services that will exceed the expectations of all our customers. We deliver secure, reliable and scalable applications that help businesses excel in today’s rapidly evolving economy.

Working Hours

Monday to Saturday ................. 09:00 - 18:00

Sunday ......................... CLOSED

info@goldenmace.com

IND +91 22 4004 9616 | USA +1 309 247 5034

Top
 

Avoiding cold starts And Working with Flavors

Avoiding cold starts And Working with Flavors

Cold starts

 

A cold start refers to an app’s starting from scratch: the system’s process has not, until this start, created the app’s process. Cold starts happen in cases such as your app’s being launched for the first time since the device booted, or since the system killed the app. This type of start presents the greatest challenge in terms of minimizing startup time, because the system and app have more work to do than in the other launch states.
Why we should avoid using Splash Screens on Android, arguing that they hurt the user experience, increase the size of the application, etc.
The key is creating a custom theme that overrides android:windowBackground, then replacing that custom theme with your standard theme before calling super.onCreate().

Assuming you have a theme called AppTheme, your launcher theme would be:

themes.xml

<style name=”AppTheme.ColdStart”>

<item name=”android:windowBackground”>@drawable/splash_bg</item>

</style>

 

Place “splash_bg” inside drawable folder:

splash_bg.xml

<layer-list

xmlns:android=”http://schemas.android.com/apk/res/android”

android:opacity=”opaque”>

<!– The background color, preferably the same as your normal theme –>

<item android:drawable=”@android:color/white”/>

<!– Your product logo – 144dp color version of your app icon –>

<item>

<bitmap

android:gravity=”center”

android:src=”@drawable/new_logo”/>

</item>

</layer-list>

 

The easiest way to transition back to your normal theme is to call setTheme(R.style.AppTheme) before super.onCreate() and setContentView():

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style. AppTheme);
super.onCreate(savedInstanceState);
// …
}
}

 

Flavors

Flavor is a very powerful feature available in the Android gradle plug-in that allows us to manage different “flavors” of an application.
It’s important to understand why and when you might need multiple flavors. As an app developer, you would like to release a given app using a new brand name, logo and/or theme, along with some minor feature differences. Using this to maintain app very quickly.
The other way to create separate projects for each version, which is far from ideal, and will lead to errors and projects not having the complete new changes and fetch issues.
Using multi flavors developer can easy way to build and maintain different version of app.
Another very important concept regarding building android apps, called “build types”. The default build types are debug and release.
If you have create two flavors, called free and promo, and two build types called debug and release, the Build Variants will be:

  • freeDebug
  • freeRelease
  • promoDebug
  • promoRelease
  • paidDebug
  • paidRelease

check out the Android Studio Build Variant more information.

 

Working with flavors:

productFlavors {
free {}
paid {}
promo {}
}

we will have a new separate file called variants.gradle with the different properties of each flavor:
ext {

free = [
applicationId : “com.gmits.free”,
versionCode : 1,
versionName : “1.0”,
app_name : “Flavour Free”,
buildConfigField: ‘”Hi”‘
]

paid = [
applicationId : “com.gmits.paid”,
versionCode : 1,
versionName : “1.0”,
app_name : “Flavour Paid”,
buildConfigField: ‘”Hello”‘
]

promo = [
applicationId : “com.gmits.promo”,
versionCode : 1,
versionName : “1.0”,
app_name : “Flavour Promo”,
buildConfigField: ‘”Demo”‘
]
}

we can see that productFlavors delegates on de class:NamedDomainObjectContainerthis provides a whenObjectAdded method that allows you to perform an action when an object is added, in this case our ‘ProductFlavor’.

productFlavors.whenObjectAdded {
flavor ->
def flavorData = rootProject.ext[flavor.name]
flavor.applicationId = flavorData.applicationId
flavor.versionCode = flavorData.versionCode
flavor.versionName = flavorData.versionName
flavor.resValue ‘string’, “app_name”, flavorData.app_name
flavor.buildConfigField(“String”, “DEFAULT_BASE_DOMAIN”, flavorData.buildConfigField)
}

The code and the examples shown in this article are available at Github Repository

No Comments

Leave a Comment