我在 Android Studio 中创建了一个简单的项目来试用它。我会接受,目前我必须编写 build.gradle 文件以匹配我的 IDE 配置,但由于我对 IntelliJ 和 Gradle 都是新手,所以我很挣扎。
该项目是使用默认的空白活动创建的。我创建了一个简单的,非常点头的 View 类,如下所示:
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());
}
}
为了让它在 IDE 中编译/解析,我将 Logback JAR 库添加到 android-support-v4.jar 文件所在的同一 libs 目录中。快速修改项目结构,一切似乎都可以正常工作。在 IDE 的编辑器中,我可以看到Logger
和LoggerFactory
类的方法。
为了将 noddy 应用程序部署到我的 Nexus 4,我修改了 build.gradle 文件以引用这些库:
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
}
}
它构建得很好,它部署在手机上,但是当默认活动启动时,我得到一个运行时错误:
Caused by: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
at com.example.view.GraphView.<init>(GraphView.java:29)
... 25 more
我错过了什么?
提前谢谢了。
史蒂夫