Search code examples
androidkotlintimber-android

Android/Kotlin: unresolved reference: timber


I'm trying to write a kotlin library for Android and can't include timber. I always get the following error:

Error:error: unresolved reference: timber

I have this in my build.gradle:

apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven {url "https://maven.google.com"}
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    compile 'com.jakewharton.timber:timber:4.5.1'
    testCompile 'junit:junit:4.12'
}

My source file is very simple at the moment:

package net.mbonnin.test

import timber.log.Timber

class Main() {

    fun main() {
        Timber.d("hello world")
    }
}

It fails on the import statement.

I'm using Android studio 3 canary 4 and kotlin 1.1.2-4. Any idea what I'm doing wrong ? Or is timber not usable in kotlin ?


Solution

  • apply plugin: 'java-library'
    apply plugin: 'kotlin'
    

    You're not applying any android plugin and thus don't know how to handle @aar artifacts. But these are the default artifacts when using Android libraries. Sometimes you might find @jar artifacts with the dependency as well, but not that often any more. And Timber is

    A logger with a small, extensible API which provides utility on top of Android's normal Log class.

    You could teach Gradle to understand @aar files, but then you'd run into issues with the Android dependencies when using Timber.

    So basically you have to make you module an Android Kotlin library instead.

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'