0

I've created a simple project in Android Studio to try it out. I'll accept that for the moment I have to had code my build.gradle file to match my IDE configuration, but as I am new to both IntelliJ and Gradle I am struggling.

The project was created with a default blank activity. I created a simple, very noddy, View class given below:

package com.example.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by steve on 02/06/13.
 */
public class GraphView extends View {

    /** The system logger. */
    protected transient final Logger log;

    public GraphView(Context context) {
        super(context);

        // Instantiate the system logger.
        log = LoggerFactory.getLogger(getClass());
    }

    public GraphView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Instantiate the system logger.
        log = LoggerFactory.getLogger(getClass());
    }

    public GraphView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // Instantiate the system logger.
        log = LoggerFactory.getLogger(getClass());
    }
}

In order to get it to compile/parse within the IDE I added the Logback JAR libraries to the same libs directory the android-support-v4.jar file is located. A quick modify of the Project Structure and all appears to work fine. Within the IDE's editor I can see the methods on the Logger and LoggerFactory classes.

In order to deploy the noddy app to my Nexus 4 I modified build.gradle file to also reference the libraries:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/logback-android-1.0.10.2.jar')
    compile files('libs/slt4j-api-1.7.5.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

It builds just fine, it deploys it on the phone, but when the default activity in started I get a runtime error:

Caused by: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
        at com.example.view.GraphView.<init>(GraphView.java:29)
        ... 25 more

What have I missed?

Many thanks in advance.

Steve

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Dobbo
  • 1,188
  • 3
  • 16
  • 27
  • I see the following : compile files('libs/slt4j-api-1.7.5.jar') Shouldn't it be libs/slf4j ? – TiGer Mar 03 '14 at 15:30

2 Answers2

2

Found the answer here.

After adding a library you need to run ./gradlew clean in the project root before building.

Community
  • 1
  • 1
Dobbo
  • 1,188
  • 3
  • 16
  • 27
0

UPDATE

You likely still have some kind of typo in there.

I fetched the android SDK and attempted to compile a simple Hello world project with your build file and it works. That implies that you may have a typo, missing jar file or something like that.

Make sure that the jars file are present with the correct names too!

Below is the build.gradle (compile files is used only once, but that shouldn't matter, I typically don't use the filesystem without a remote repository):

buildscript {
        repositories {
                mavenCentral()
        }
        dependencies {
                classpath 'com.android.tools.build:gradle:0.4'
        }
}

apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar', 'libs/logback-android-1.0.10.2.jar', 'libs/slf4j-api-1.7.5.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Could it be slf4j-api-1.7.5.jar instead of slt4j-api-1.7.5.jar?

I think that you've got a typo in your dependencies section.

rimero
  • 2,383
  • 1
  • 14
  • 8
  • I would have thought that was the problem. But it was the JAR file name that had the typo. I've corrected that and the problem remains. If I comment out the libraries from the `build.gradle` file then the code doesn't compile with Gradle, so I conclude that for some reason the gradle build is not pulling in those JAR files a package time only at compile time. – Dobbo Jun 02 '13 at 17:46
  • Thanks for looking in to this. Did you try running the hello world app? The problem I'm seeing is that it fails at RUNTIME! And I've seen it with two projects, using a different location for the library so I'm sure that there isn't a typo any more. – Dobbo Jun 04 '13 at 20:15
  • In that case I don't know. I don't have any Android devices... Good luck. – rimero Jun 04 '13 at 20:44