4

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 ?

air cooled
  • 343
  • 1
  • 5
  • 18
mbonnin
  • 6,893
  • 3
  • 39
  • 55
  • 1
    Mmmmkay, Timber seems to be Android-only. I was under the impression that it could be used without android but I must have been wrong... – mbonnin Jun 17 '17 at 21:47
  • That shouldn't be the issue here.. Also you shouldn't compile the Gradle plugin like that. What is the rest of your gradle script? – nhaarman Jun 17 '17 at 21:53
  • Yes, I find it strange too. If there's an unmet dependency on Android.log, I would expect the error message to say so instead of just `unresolved reference` – mbonnin Jun 17 '17 at 21:54
  • have you refresh the gradle project to update your dependencies? – holi-java Jun 17 '17 at 22:28
  • You have two `dependencies` sections. Perhaps the first one overrides the second one? Then Timber would never be included, and that would explain why you can't reference it in your code. – nhaarman Jun 17 '17 at 22:46

1 Answers1

7
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'
tynn
  • 38,113
  • 8
  • 108
  • 143