Android Studio AAR 文件错误:找不到类

7

当我尝试在其他项目中使用已导出的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生成的build目录下的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'
    }
}
3个回答

5
问题在于,aar文件不包含嵌套的依赖项并且没有描述库使用的依赖项的POM文件
如果您正在使用flatDir仓库导入aar文件,则必须在项目中指定依赖项。您应该使用maven仓库!例如: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文件进行依赖注入。

0

虽然这不是好的实践方式,但如果您确实需要它,您可以使用嵌套依赖项,如下所示

depenencies {
    ...

    compile (name:'somerandomlibrary-debug', ext:'aar') {
        dependencies {
            compile 'com.some:dependency:9.1.1'
            ...
        }
    }
}

-1

要使用aar文件,您必须在build.gradle中使用类似以下的内容:

repositories{
      flatDir{
              dirs 'libs'
       }
 }

这样你就可以在libs文件夹中使用aar文件。

然后你需要使用以下方式添加依赖:

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }

感谢你抽出时间。我在我的Gradle中已经有了上述内容。但是我无法访问匿名类和内部类,我可以访问其他类。 - manish
你只能访问公共类。 - Angela

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接