【问题标题】:Android studio aar file error class not foundAndroid studio aar 文件错误类未找到
【发布时间】:2015-09-20 13:45:57
【问题描述】:

当我尝试在其他项目中使用导出的 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'
    }
}

【问题讨论】:

    标签: android android-studio aar


    【解决方案1】:

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

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

    一个更简单的解决方案是将其添加到“aar”-project 中的 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 文件进行依赖注入。

    【讨论】:

      【解决方案2】:

      这不是一个好习惯,但如果你真的需要它,你可以使用nested dependencies,就像这样

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

      【讨论】:

        【解决方案3】:

        要使用 aar 文件,您必须在 build.gradle 内部使用类似:

        repositories{
              flatDir{
                      dirs 'libs'
               }
         }
        

        这样就可以使用libs文件夹内的aar文件了。

        然后您必须使用以下方法添加依赖项:

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

        【讨论】:

        • 感谢 Angela 抽出宝贵时间,我的 gradle 中已经有了以上内容。我无法访问匿名类和内部类。我可以访问其他课程
        • 您只能访问公共课程。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 2014-08-09
        相关资源
        最近更新 更多