Timber library is a extended library of android Log's. While developing android applications, most of developers prefer Android Logs. But here problem is about clean logs while deploy android project. To avoid this process using Timber library.
This example demonstrate about how to integrate Timber in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the Timber library in build.gradle as shown below
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.andy.myapplication" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' implementation 'com.jakewharton.timber:timber:4.7.1' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
Step 3 − Timber has to be initialize in onCreate method in MainActivity as shown below.
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import timber.log.Timber; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); } } }
Step 4 − Timber has different error and warning methods as shown below.
Timber.v("Some Text");- It indicates about verbose error Timber.d("Some Text ");- It indicates about debug error Timber.i("Some Text ");- It indicates about information error Timber.w("Some Text ");- It indicates about warning error Timber.e("Some Text ");- It indicates about error
Step 5 − A simple example of Timber is as shown below.
package com.example.andy.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import timber.log.Timber; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); } else { Timber.plant(new ReleaseTree()); } Timber.v("Some Text"); Timber.d("Some Text "); Timber.i("Some Text "); Timber.w("Some Text "); Timber.e("Some Text "); } }
Step6 − In the above example we are not at all changing anything to view and manifest.
The sample output of the above code is as shown below −