16

What is wrong with my configuration or code ?
I have this error highlighted

Cannot resolve method 'plant(timber.log.Timber.DebugTree)'

for the code

import timber.log.Timber;  
public class AppClass extends Application {  
    @Override  
    public void onCreate() {  
        super.onCreate();  
        if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); }  
    }  
}  

but it builds and it executes. Still I think it means something, no ?

Configuration infos:

Android Studio Bumblebee | 2021.1.1  
classpath 'com.android.tools.build:gradle:7.1.0'  
Gradle: com.jakewharton.timber:timber:5.0.1@aar  
ext.kotlin_version = '1.6.10'   
sourceCompatibility JavaVersion.VERSION_1_8
9
  • 1
    but it builds and it executes. Still I think it means something, no ? if it builds and executes, probably not. most likely just a bug with android studio Commented Jan 26, 2022 at 22:03
  • 1
    It means that something is out of sync. Similar things happen to me occasionally with Android Studio. Try exiting AS and restarting it. You can also clean and invalidate caches and restart. When these things happen to me and they are difficult to get rid of, restarting my whole computer works. (Sometimes they hang around and go away on their own.) As long as it doesn't stop your work, I would let it go but keep an eye on it.
    – Cheticamp
    Commented Jan 26, 2022 at 22:17
  • I restarted the computer. I cleaned and invalidated caches and restart. Twice. Still there. So may be with a new version of AS. Thanks
    – C.B.
    Commented Jan 27, 2022 at 18:58
  • 1
    I've come across the same issue, immediately after updating Android Studio from Arctic Fox (2020.3.1) to Bumblebee (2021.1.1). I've tried a number of things in an attempt to rectify the peculiar issue, to no avail. At the moment, I'm keeping track of this issue on the official Timber repo, which covers this. Commented Feb 2, 2022 at 5:24
  • 1
    not yet solved with Android Studio Bumblebee | 2021.1.1.21 Patch 1 for Windows 64-bit
    – C.B.
    Commented Feb 5, 2022 at 10:43

3 Answers 3

19

Until issue fixed (as @n8yn8 noted in question comment) I solved it with downgrade to version 4.7.1:

implementation 'com.jakewharton.timber:timber:4.7.1'

There are some further discussions on Timber's GitHub Issue #459. It seems to be an issue related to IDE. It is confirmed that no solution with Timber 5+ for pure Java code at this moment.

12

For the workaround solution without downgrade dependency version and also no need to apply with another dependency by keep applying the one from JakeWharton, we can try to config Timber in Kotlin instead of Java class since the warning message only appear on Java class.

By doing so, you can try two options bellow:

  1. Convert your custom application class from Java to Kotlin
  2. Create another class in Kotlin and create new method to config Timber configuration without converting Java class to Kotlin class to avoid any errors or code cleaning.

See my example for second options bellow:

TimberUtils.kt

import timber.log.Timber

object TimberUtils {

    @JvmStatic
    fun configTimber() {
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }
    }
}

YourCustomJavaClass.java

@Override
public void onCreate() {
    super.onCreate();
    TimberUtils.configTimber();
}

Hope it can resolve your problem.

2
  • Creative workaround. I use yours.
    – Azizi Musa
    Commented Mar 24, 2023 at 6:22
  • This is the real solution - @C.B. please accept it. Commented Aug 28, 2023 at 22:16
4

In app level build.gradle file, set the following jakewharton timber version:

implementation 'com.jakewharton.timber:timber:4.7.1'

Then in your application class onCreate() Method:

For Kotlin:

if (BuildConfig.DEBUG) {
        Timber.plant(DebugTree())
    } else {
        Timber.plant(ReleaseTree())
    }

For Java:

if (BuildConfig.DEBUG) {
        Timber.plant(new DebugTree());
    } else {
        Timber.plant(new ReleaseTree());
    }

Inner ReleaseTree() class Kotlin:

inner class ReleaseTree : Timber.Tree() {
    override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
        if (priority == Log.VERBOSE || priority == Log.DEBUG) {
            return
        }

        // log your crash to your favourite
        // Sending crash report to Firebase CrashAnalytics

        // FirebaseCrash.report(message);
        // FirebaseCrash.report(new Exception(message));
    }
}

Inner ReleaseTree() class Java:

class ReleaseTree extends Timber.Tree {
@Override
protected void log(int priority, String tag, String message, Throwable t) {
    if (priority == Log.VERBOSE || priority == Log.DEBUG) {
        return;
    }

    // log your crash to your favourite
    // Sending crash report to Firebase CrashAnalytics

    // FirebaseCrash.report(message);
    // FirebaseCrash.report(new Exception(message));
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.