Part 2. Advanced Android Tutorial - Setup Application Class

Getting Started

The first step should be to clone the sample app repository and build the application in your own Android Studio. I would also suggest that you should simultaneously code your own app which is very similar to it. Like, for your entry screen you can use Github API and display most popular Android repositories! You will find Jake Wharton several times there. That guy is a genius! Now coming back to the point. Start by creating your own project and with the sample app code downloaded to your computer so that you can refer it, whenever required.

Manage Gradle Dependencies

Generally, in an Android project, we end up with 10 or more libraries (support and third-party libraries like Glide). It is a good practice to manage the versions of the libraries using variables names. Use ext variables in your Gradle so that all your version numbers are in one place.

ext {
    minSDKVersion = 21
    targetSDKVersion = 26
    supportLibraryVersion = "26.1.0"
    constraintLayoutVersion = "1.0.2"
    junitVersion = "4.12"
    testRunnerVersion = "1.0.1"
    espressoVersion = "3.0.1"
    daggerVersion = "2.11"
    guavaVersion = "18.0"
    roomVersion = "1.0.0"
    retrofitVersion = "2.3.0"
    googlePlayServiceVersion = "11.6.0"
    rxVersion = "2.2.1"
    timberVersion = "4.6.0"
    roomVersion = "1.0.0"
    stethoVersion = "1.5.0"
}

Then in your app module build.gradle file you can declare dependencies like this:

implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"

Timber Logging Library 

Timber is an abstraction over the native logging interface. It is a lightweight library to write logs in different places and control how it's done in a centralized manner. It has a very small and extensible API. Yes, this library belongs to Jake Wharton!

Setting up Timber is pretty straightforward.  The first step is to just add a dependency to your app's module-level Gradle file.

implementation "com.jakewharton.timber:timber: $rootProject.ext.timberVersion"

In Application class plant a "Tree" which would create logs in the debug mode only. You can refer to the Application Class of the accompanying application to see how logging with Timber is set up. Here a custom Debug tree has been defined to print line numbers in the log which helps a lot in debugging. Similar to Debug Tree, you can define Release Tree as well. This 6 minutes YouTube video explains it really well. Why we went through all this trouble to set up timber? Because it is very handy when you are debugging your code. I recommend using Timber in all Android applications.

Advantages of Timber
  1. No tags: The library automatically assigns a TAG to log statements. By default, the TAG is the filename, but Timber can provide a custom tag if required.
  2. Integration with Crashlytics: Timber can be used with Crashlytics to report crashes in production. Refer to this tutorial for a simple example. 
  3. Extensible: It is easy to configure logging with timber so that we have complete information of an application crash or its erroneous behavior. We can have the tag which gives context, its stack trace and any additional information that we may want, both in development and production.

Stetho

Stetho is an open source debugging platform developed by Facebook. It allows you to use Google Chrome’s developer tools to perform various debugging activities, such as view hierarchy inspection, network inspection, SQLite database management, and more. Refer to this tutorial for a more detailed explanation. The accompanying application also integrates Stetho in the debug mode. It takes only one line of code to integrate it into the application after you have added it to your dependencies. Stetho is very useful when you want to debug your app database. Just type chrome://inspect in your browser and you can see everything inside your app database. We all know how reading application DB otherwise is a time taking process. So, I recommend it for every project. If you want you can remove it later when you no longer need to debug your DB.


if(BuildConfig.DEBUG) {
     Stetho.initializeWithDefaults(this);
}

Strict Mode

StrictMode is used to set up virtual machine and thread policies for the application and report such violations. You can configure Android to crash your application with the alert or you can just log the alert and let your application carry on. It is most commonly used to catch accidental disk or network access on the application's main thread. I recommend developing all applications in StrictMode so that you adhere to correct structure and coding practices right from the beginning. The sample application enforces strict mode and you can see the code in the Application Class to find how the strict mode is set up for your application.


private void setupStrictMode() {
        if (BuildConfig.DEBUG) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectAll()
                    .penaltyLog()
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects()
                    .penaltyLog()
                    .build());
        }
}

In the next lesson, we will discuss Dependency Injection.








2 comments:

  1. Hi, I have cloned your repository. Really liked the way you maintained MVP pattern for RecyclerView.

    ReplyDelete
  2. Thanks. Let me know if you want some material on a particular topic.

    ReplyDelete

Powered by Blogger.