15

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
C.B.
  • 378
  • 6
  • 19
  • 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 – a_local_nobody Jan 26 '22 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 Jan 26 '22 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. Jan 27 '22 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](https://github.com/JakeWharton/timber/issues/458) on the official Timber repo, which covers this. – Tom Larcher Feb 02 '22 at 05:24
  • 1
    not yet solved with Android Studio Bumblebee | 2021.1.1.21 Patch 1 for Windows 64-bit – C.B. Feb 05 '22 at 10:43
  • This looks to be a tracked issue: Timber reference: https://github.com/JakeWharton/timber/issues/458 Android Studio bug tracker: https://issuetracker.google.com/issues/218088925 – n8yn8 Feb 23 '22 at 19:18
  • not yet solved with `Android Studio Bumblebee | 2021.1.1 Patch 2` and `classpath 'com.android.tools.build:gradle:7.1.2'` – C.B. Mar 01 '22 at 18:37
  • my current workaround is to use a kotlin appClass : no error hghlighted, still building and working. The beginning of a migrating my dev to Kotlin... ;-) – C.B. Jun 26 '22 at 16:09
  • Same issue in Android Studio Flamingo | 2022.2.1 Patch 2, built on May 12, 2023 – Alex Bravo Aug 01 '23 at 18:59

4 Answers4

17

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.

Chester Fung
  • 182
  • 1
  • 10
Solata
  • 1,444
  • 3
  • 28
  • 42
5

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.

Chivorn
  • 2,203
  • 2
  • 21
  • 37
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));
}

}

-1

For those using sentry-timber

Just use

implementation "io.sentry:sentry-android:$sentry_version"
implementation "io.sentry:sentry-android-timber:$sentry_version"

Remove this dependency

implementation "com.jakewharton.timber:timber:$timber_version"

For me, this fix resolves the issue

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53