依赖项内的库类的Android NoClassDefFoundError [英] Android NoClassDefFoundError for library's class inside a dependency

查看:165
本文介绍了依赖项内的库类的Android NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用记录器库的名为核心"的Android库( https://github.com/orhanobut/logger ).

I created an Android library named "core" that use the Logger library (https://github.com/orhanobut/logger).

此处是其build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.orhanobut:logger:1.15'

    compile 'com.android.support:appcompat-v7:25.0.1'

    testCompile 'junit:junit:4.12'
}

然后我构建核心库的.aar.

通过将.aar core文件放在libs文件夹中,将该库作为依赖项添加到我的应用程序中.

I add this library as a dependency into my application, by putting the .aarcore files in the libs folder.

那是我的应用程序的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.package.test"
        minSdkVersion 16
        targetSdkVersion 25
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    repositories {
    mavenCentral()
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

dependencies {

    compile 'com.mypackage:core:1.0@aar'

    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:multidex:1.0.1'

}

如您所见,我已激活multidex.

它编译得很好,但是在运行时,在第一次调用Logger时出现了异常:

It compile just fine but at runtime, at the first call to Logger I get an exception:

stack=java.lang.NoClassDefFoundError: Failed resolution of: Lcom/orhanobut/logger/Logger;

即使我设置了transitive = true

compile (com.mypackage:core:1.0@aar) {
        transitive=true
}

它不起作用.

谢谢!

推荐答案

我必须共同做一些事情才能使其正常工作.

I had to do a couple of things in concert to get it to work.

1)在您的图书馆项目中

1) In your library project

将以下内容添加到您的项目级 gradle文件中:

Add the following to your project-level gradle file:

buildscript {
    dependencies {
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}

请确保为gradle版本使用正确的插件版本,请检查

Be sure to use the right version of the plugin for your gradle version, check here.

将以下内容添加到您的模块级 gradle文件中:

Add the following to your module-level gradle file:

apply plugin: 'com.github.dcendents.android-maven'
group='com.github.YourPackage'// Doesn't have to be github, just an example

此外,在此文件中,如果您使用的是

Also in this file, make sure your dependencies (the ones you want to be transitive) use api instead of implementation if you are using Gradle 3.4+. If you are using a version of Gradle <3.4, compile is the way to go. Example:

dependencies {
    api 'com.google.android.gms:play-services-location:15.0.1'
}


2)在您的应用程序项目(使用库)中


2) In your app project (which uses the library)

将以下内容添加到您的模块级 gradle文件中:

Add the following to your module-level gradle file:

dependencies {
    implementation('com.github.YourPackage:yourRepo:version@aar') {
        transitive=true
    }
}

在您的项目级 gradle文件中添加以下内容(但这取决于您的库的发布位置,对我来说是jitpack):

Add the following to you project-level gradle file (but this will depend on where your library is served from, for me it's jitpack):

allprojects {
    repositories {
        ...
        maven {
            url 'https://jitpack.io'
            credentials { username 'yourAuthKey' }// Only for private repositories
        }
    }
}

注意:您不应将auth密钥作为字符串添加到build.gradle中,而应将其放入gradle.properties文件中的属性中.

Note: You shouldn't add the auth key as a string in build.gradle, put it in a property in your gradle.properties file.

注2:JitPack允许您使用例如development-SNAPSHOT作为gradle依赖项的版本号. Android Studio会缓存这些依赖项,并且当您推送到开发分支时,不会重新下载它.要解决此问题,请在开发过程中使用提交哈希作为版本号,或清除(删除)Windows上位于~/.gradle/caches/modules-2/metadata-x.xx/descriptors/com.github.YourPackage/yourRepo的缓存文件. (针,我是很难学到的.)

Note 2: JitPack allows you to use for example development-SNAPSHOT as a version number for a gradle dependency. Android Studio caches these dependencies and will not re-download it when you push to your development branch. To overcome this, use commit hashes as version numbers during development or clear (delete) the cache files, located on Windows at ~/.gradle/caches/modules-2/metadata-x.xx/descriptors/com.github.YourPackage/yourRepo. (Needles to say, I learned this the hard way).

这篇关于依赖项内的库类的Android NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆