木材未登录Kotlin Android

Eth*_*_AI 6 android kotlin timber-android

Timber是一个用于登录Android的绝佳库.但是在Kotlin课程中,不输出任何内容.我怎样才能解决这个问题?

MainActivity.kt代码:

Timber.e("Timber Log 1")
Log.e("MainActivity", "Log 1")
Run Code Online (Sandbox Code Playgroud)

Gradle:我尝试过普通的Java Timber:

implementation 'com.jakewharton.timber:timber:4.7.1'
Run Code Online (Sandbox Code Playgroud)

而这个Kotlin特定包装:

implementation 'com.github.ajalt:timberkt:1.5.1'
Run Code Online (Sandbox Code Playgroud)

结果相同.两者都没有输出.只来自Log.e()

Pav*_*ngh 13

木材的第一步是种植文件中提到的树

通过Tree实例添加行为.您可以通过调用Timber.plant来安装实例.树木的安装应尽早完成.应用程序的onCreate是最合理的选择.

并使用 debugTree

DebugTree implementation will automatically figure out from which class it's being called and use that class name as its tag.由于标签不同

如果你不这样做那么你将没有日志条目并尽快oncreate这样做,比如在应用程序类中或更好的内部,所以这样做

Timber.plant(Timber.DebugTree());
Run Code Online (Sandbox Code Playgroud)


Fua*_*mad 7

我遇到了同样的问题,使用 Kotlin 和 Android studio 3.6 请按照以下步骤操作:

  1. 在 build.gradle(Module: App) 中添加以下内容

    implementation 'com.jakewharton.timber:timber:4.7.1'
    
    Run Code Online (Sandbox Code Playgroud)
  2. Timber在应用程序类中初始化:

    class MyApp : Application() {
    
        override fun onCreate() {
            super.onCreate()
    
            if(BuildConfig.DEBUG){
                Timber.plant(Timber.DebugTree())
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将 Application class( MyApp)添加到 Manifest (AndroidManifest.xml)

    <application
        android:name=".MyApp"
    
    Run Code Online (Sandbox Code Playgroud)

现在你可以使用木材: Timber.i("Timber logs")

如果您愿意,也可以使用自定义标签: Timber.tag("Yo").I("used custom tag for logs")


小智 5

就我而言,BuildConfig 导入是错误的

import org.koin.android.BuildConfig
Run Code Online (Sandbox Code Playgroud)

但我的应用程序有

import com.company.example.BuildConfig
Run Code Online (Sandbox Code Playgroud)