Android studio aar 文件报错类未找到

标签 android android-studio aar

当我尝试在其他项目中使用导出的 android 库项目(aar 格式)时。我收到以下错误。

> "Could not find class 'com.manish.core.helper.RegistrationHelper$1'" 
> "Could not find class 'com.manish.core.helper.RegistrationHelper$2'"
> "Could not find class 'com.manish.core.helper.RegistrationHelper$3'"
> "Could not find class 'com.manish.core.helper.RegistrationHelper$4'"

在aar文件里面有一个文件“classes.jar”,里面包含了所有的类文件,但是我不明白错误的原因。

我正在使用 android studio 在构建目录中生成的 aar 文件。 我还在 gradle 文件中添加了 apply plugin: 'com.android.library'

所有这些错误仅针对匿名类和静态类。

我的gradle文件:

apply plugin: 'com.manish.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.manish.test"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'junit:junit:4.12'
    compile(name:'somerandomlibrary-debug', ext:'aar')
}

repositories{
    flatDir{
        dirs 'libs'
    }
}

最佳答案

问题是,aar 文件 不包含嵌套的依赖关系,也没有描述库使用的依赖关系的POM 文件。

如果您使用 flatDir 存储库导入 aar 文件,您还必须在您的项目中指定依赖项。您应该使用 ma​​ven 存储库!例如:maven-publish

一个更简单的解决方案是,将其添加到“aar”项目中的 build.gradle 的以下行中:

task createPom {
    apply plugin: 'maven'
    description "Generates pom.xml"
    pom {
        project {
            groupId 'com.example'
            artifactId 'example'
            version '0.0.1-SNAPSHOT'
            packaging 'aar'
        }
    }.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')

        configurations.compile.allDependencies.each { dependency ->
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', dependency.group)
            dependencyNode.appendNode('artifactId', dependency.name)
            dependencyNode.appendNode('version', dependency.version)
        }
    }.writeTo("$buildDir/pom.xml")
}

您可以在基于aar 的应用程序中使用生成的POM 文件进行依赖项注入(inject)。

关于Android studio aar 文件报错类未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680094/

相关文章:

android - 只想在运行单元测试时运行自定义 gradle 任务

android - 如何使接口(interface)的具体实现在库范围之外不可访问?

android - Android 库 JAR 可以依赖于 Android 库 AAR 吗?

java - Android运行时NoClassDefFoundError

Android:如何将滚动布局转换为位图

android - 获取所有已注册地理围栏的列表

java - 从广播接收器以编程方式连接 Android 中的蓝牙设备

android - 如何在我的 Chrome/Firefox 浏览器中查看 SQLite 数据库?

android - 如何使用 New Build System 在项目中添加 AAR 库?

android - 为任何移动设备编译时不推荐使用 TList