Skip to content

Commit

Permalink
Setup AndroidX startup for Timber
Browse files Browse the repository at this point in the history
Signed-off-by: Mohsen <[email protected]>
  • Loading branch information
mohsenoid committed Jan 23, 2022
1 parent 8a0d7da commit 6de6e4f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ dependencies {

// leakcanary
debugImplementation("com.squareup.leakcanary:leakcanary-android:2.7")

// Startup
implementation("androidx.startup:startup-runtime:1.1.0")
}
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mohsenoid.rickandmorty">

<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -28,6 +29,16 @@
</activity-alias>

<activity android:name=".view.MainActivity" />

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.mohsenoid.rickandmorty.startup.TimberInitializer"
android:value="androidx.startup" />
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.component.KoinComponent
import org.koin.core.context.startKoin
import timber.log.Timber

class RickAndMortyApplication : MultiDexApplication(), KoinComponent {

Expand All @@ -16,8 +15,6 @@ class RickAndMortyApplication : MultiDexApplication(), KoinComponent {
override fun onCreate() {
super.onCreate()

if (isDebug) setupTimber()

startKoin {
val appProperties: Map<String, String> = mapOf(
KoinQualifiersNames.BASE_URL to BuildConfig.BASE_URL,
Expand All @@ -30,15 +27,4 @@ class RickAndMortyApplication : MultiDexApplication(), KoinComponent {
modules(appModule + dataModule)
}
}

private fun setupTimber() {
Timber.plant(
object : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
// adding file name and line number link to logs
return "${super.createStackElementTag(element)}(${element.fileName}:${element.lineNumber})"
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mohsenoid.rickandmorty.startup

import android.content.Context
import androidx.startup.Initializer
import com.mohsenoid.rickandmorty.BuildConfig
import timber.log.Timber

class TimberInitializer : Initializer<Unit> {

private val isDebug: Boolean = BuildConfig.DEBUG

override fun create(context: Context) {
if (isDebug) setupTimber()
}

private fun setupTimber() {
Timber.plant(
object : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
// adding file name and line number link to logs
return "${super.createStackElementTag(element)}(${element.fileName}:${element.lineNumber})"
}
},
)
}

override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()
}

0 comments on commit 6de6e4f

Please sign in to comment.