10

I have several projects which I build to create an .aar. I then import this .aar into into Android Studio under /libs. The build.gradle file for this dependency looks as follows:

repositories{
    flatDir{
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    compile 'com.android.support:multidex:+'
    compile(name: 'customApi-debug', ext:'aar')
}

Since the library is quite large I have set multiDexEnabled = true. Android Studio finds the library and autocomplete works. Building works fine too but running the app gives the following error:

java.lang.NoClassDefFoundError: com.companyx.android.api.ui.vision.metaio.MetaIoView
            at com.companyx.android.api.ui.vision.metaio.MetaIoView$$InjectAdapter.<init>(MetaIoView$$InjectAdapter.java:29)

I uncompressed and disassembled the .aar and dex files, respectively, and verified that the classes its complaining about actually exist. I've tried existing approaches for dealing with this problem but none of them worked.

Anyone else experienced this? Thanks in advance.

Przemek Lach
  • 1,348
  • 2
  • 19
  • 42
  • There might be a verification error with the class. Look at the logs in logcat just after you install the application, while it is being odexed/oated, and see if there are any verification errors for that class. – JesusFreke Apr 24 '15 at 23:29
  • There appear to be no errors during installation. What I did notice is that when I tap to go to the Activity that uses this source I get I/art﹕ Rejecting re-init on previously-failed class java.lang.Class several times right before I get my exception. – Przemek Lach Apr 24 '15 at 23:55
  • Yes, that means that there were some verification errors at install time. Take another look at logcat during installation :) – JesusFreke Apr 25 '15 at 03:17
  • I can't seem to find any verification errors as they relate to this specific class. I found a couple of things that looked suspicious but I'm not sure if they are relevant to this problem: W/PackageParser﹕ Unknown element under : meta-data at /data/local/tmp/com.companyx.pilot.noclassdefffoundtest.app Binary XML file line #22 and Unknown element under : meta-data at /data/app/vmdl58152644.tmp/base.apk Binary XML file line #22. What keyword should I be looking for in logcat? – Przemek Lach Apr 26 '15 at 19:16
  • I just run into the same problem now. Sad that there's still no answer. – Thuy Trinh Jul 06 '15 at 05:39
  • Possible duplicate: http://stackoverflow.com/questions/28686721/noclassdeffounderror-at-runtime-but-class-is-in-classes-dex-what-givies – tir38 Mar 31 '17 at 18:25

2 Answers2

8

I run into the same issue. The fix is firstly to deploy the AAR file to a local maven (I utilized the plugin at https://github.com/dcendents/android-maven-gradle-plugin). Then I referenced to the local maven as described at https://stackoverflow.com/a/23045791/2563009. And eventually I declared the dependencies with a transitive option, like this:

dependencies {
  compile('com.myapp.awesomelib:awesomelib:0.0.1@aar') {
    transitive = true
  }
}

The error would be gone then.

Community
  • 1
  • 1
Thuy Trinh
  • 2,914
  • 1
  • 22
  • 35
-1

Just fyi, you can use a simpler syntax, which is valid too

 compile 'com.myapp.awesomelib:awesomelib:0.0.1'

Don't forget to omit the @aar thing at the end of the library name

RexSplode
  • 1,475
  • 1
  • 16
  • 24