Android: ClassNotFoundException when using library project with gradle dependencies

I have a custom view library that compiles itself and works correctly (through another action created only for testing purposes inside the library project). However, when I create a library and then import aar into another project (open module settings β†’ new module β†’ existing aar ..), I get a ClassNotFoundException runtime - the exception applies to the only gradle dependency that the library is used. Why is this happening?

Library gradle file:

apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile 'com.googlecode.libphonenumber:libphonenumber:7.2.1' } 

The error I am getting is:

 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.i18n.phonenumbers.PhoneNumberUtil" on path: DexPathList[[zip file.. 
+6
source share
1 answer

The aar dependency is not like the maven / ivy dependency in that there is no transitive dependency in the pom or xml file associated with it. When you add aar dependency, gradle has no way to know which transitive dependencies are retrieved.

Common practice in the Android world seems to be to explicitly add transitive dependencies to your application that uses aar. This can be a cumbersome and heterogeneous defeat of a point in a dependency management system.

There are several ways:

1. The android-maven plugin

There is a third-party gradle plugin that allows you to publish the aar file to the local maven repository along with a valid pom file.

2. The maven-publish plugin

You use the standard maven-publish plugin to publish aar for the maven repo, but you need to build the pom dependencies yourself. For instance:

 publications { maven(MavenPublication) { groupId 'com.example' //You can either define these here or get them from project conf elsewhere artifactId 'example' version '0.0.1-SNAPSHOT' artifact "$buildDir/outputs/aar/app-release.aar" //aar artifact you want to publish //generate pom nodes for dependencies pom.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) } } } } 

In both cases, when aar + pom is available in the maven repository, you can use it in your application as follows:

 compile ('com.example:example: 0.0.1-SNAPSHOT@aar '){transitive=true} 

(I'm not quite sure how transients work if you add the dependency as compile project(:mylib) . I will update this answer soon for this case)

0
source

All Articles