Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio aar file error class not found

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'
    }
}
like image 251
manish Avatar asked Sep 20 '15 13:09

manish


1 Answers

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.

like image 193
Manuel Schmitzberger Avatar answered Sep 17 '22 11:09

Manuel Schmitzberger