7

When I try use an exported android library project (aar format) in other project. I get the following errors.

> "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'"

Inside the aar file there is one file "classes.jar", which contains all the class files but I dont understand the cause of error.

I am using the aar file generated inside build directory by android studio. I also have added apply plugin: 'com.android.library' in gradle file.

All these errors are coming only for anonymous and static class.

My gradle file:

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'
    }
}
manish
  • 944
  • 2
  • 14
  • 27

3 Answers3

5

The problem is that, the aar file doesn't contain the nested dependencies and doesn't have a POM file which describes the dependencies used by the library.

If you are importing the aar file using a flatDir repo you have to specify the dependencies also in your project. You should use a maven repository! For example: maven-publish

A easier solution is, to add this to the following lines to the build.gradle in the "aar"-project:

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

You can use the generated POM file in your aar based app for dependency injection.

Community
  • 1
  • 1
Manuel Schmitzberger
  • 5,162
  • 3
  • 36
  • 45
0

This isn't good practice but if you really need it you can use nested dependencies, like this

depenencies {
    ...

    compile (name:'somerandomlibrary-debug', ext:'aar') {
        dependencies {
            compile 'com.some:dependency:9.1.1'
            ...
        }
    }
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
-1

To use an aar file,you have to use inside the build.gradle somenthing like:

repositories{
      flatDir{
              dirs 'libs'
       }
 }

In this way you can use aar files inside the libs folder.

Then you have to add the dependency using:

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }
Angela
  • 316
  • 2
  • 10
  • Thanks Angela for your time, I already have the above in my gradle. I am able not able to access anonymous and inner classes. I can access other classes – manish Sep 20 '15 at 16:43
  • You can access only the public classes. – Angela Sep 20 '15 at 16:55