0

我在 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 的编辑器中,我可以看到LoggerLoggerFactory类的方法。

为了将 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

我错过了什么?

提前谢谢了。

史蒂夫

4

2 回答 2

2

在这里找到了答案。

添加库后,您需要./gradlew clean在构建之前在项目根目录中运行。

于 2013-06-04T20:28:27.810 回答
0

更新

您可能仍然有某种错字

我获取了 android SDK 并尝试使用您的构建文件编译一个简单的 Hello world 项目并且它可以工作。这意味着您可能有错字、缺少 jar 文件或类似的东西。

确保 jars 文件也存在正确的名称!

下面是 build.gradle(编译文件只使用一次,但这没关系,我通常不使用没有远程存储库的文件系统):

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
    }
}

它可以是slf4j-api-1.7.5.jar而不是slt4j-api-1.7.5.jar吗?

我认为您的依赖项部分有错字。

于 2013-06-02T14:30:04.607 回答