12

I'm getting class not found exception in my Android library (aar) when GoogleApiAvailability is called.

    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.GoogleApiAvailability" on path: DexPathList[[zip file "/data/app/com.myunityplugin.PushNotifications-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myunityplugin.PushNotifications-1/lib/arm, /vendor/lib, /system/lib]]

The error is self-explanatory, but I'm not sure what I'm doing wrong. I'm pretty new to gradle and AndroidStudio.

I have the following under dependencies within my gradle.build:

    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'

I think the problem is that the play-services library needs to be included in the aar, and compile doesn't do that. I tried using 'runtime' but I get "gradle dsl runtime not found".

Questions:

  1. Is this an issue where I need to include the play-services library in my aar, and how do I accomplish that? (if not, how do I address this problem)
  2. Do I need to specify play-services or does play-services-gcm suffice?

Error Info / Setup

Line causing the error

GoogleApiAvailability ServiceAvailability = GoogleApiAvailability.getInstance();

Exception:

05-23 05:38:48.221 28055-28055/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myunityplugin.PushNotifications, PID: 28055
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/GoogleApiAvailability;
   at com.myplugin.unitynotification.GCM.GCM.checkPlayServices(GCM.java:33)
   at com.myplugin.unitynotification.GCM.MainActivity.onCreate(MainActivity.java:80)
   at android.app.Activity.performCreate(Activity.java:6550)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
   at android.app.ActivityThread.access$1000(ActivityThread.java:197)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6897)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.GoogleApiAvailability" on path: DexPathList[[zip file "/data/app/com.myunityplugin.PushNotifications-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myunityplugin.PushNotifications-1/lib/arm, /vendor/lib, /system/lib]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
   at com.myplugin.unitynotification.GCM.GCM.checkPlayServices(GCM.java:33) 
   at com.myplugin.unitynotification.GCM.MainActivity.onCreate(MainActivity.java:80) 
   at android.app.Activity.performCreate(Activity.java:6550) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263) 
   at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:145) 
   at android.app.ActivityThread.main(ActivityThread.java:6897) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
    Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.common.GoogleApiAvailability
   at java.lang.Class.classForName(Native Method)
   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

App Level build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    provided files('lib-not-include/classes.jar')
}

Project level gradle.build

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Shroder
  • 573
  • 2
  • 5
  • 23

1 Answers1

22

The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.

It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.

You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • So that's why my lib doesn't work. Do you know if a .jar library can have an AndroidManifest.xml file? – luiscosta Jun 20 '16 at 15:02
  • 1
    @luiscosta No. A jar file can't contain resources ile. – Gabriele Mariotti Jun 20 '16 at 15:18
  • So **GoogleApiAvailability** cannot be gotten via a Gradle dependency? – IgorGanapolsky Dec 20 '16 at 14:30
  • So what is a good way to get around this issue? I have an aar file with manifest and classes.jar containing all the classes. – Akshay Shah Jun 09 '17 at 13:35
  • You are right, I'm deploying an aar lib that contains two native libs and two jar wrappers, to "hide" the wrappers I imported this jars to my local maven repository with "mvn install:install-file -Dfile=myjar1.jar -DgroupId=com.my.package -DartifactId=engine -Dversion=1.1.1 -Dpackaging=jar" and reference it inside the lib project. But if I don't reference this jars in the application project I just found NoClassDefFoundError when try to use any class of this jars. Do you know how can I do solve that avoiding to populate and reference this jars in the app project? – ariel Aug 09 '17 at 00:16
  • @GabrieleMariotti that really helped me and save alot of my time, is there anyway I dont have to publish my aar to maven and not have to provide the dependency as well in the project using my aar,? – riksof-zeeshan Sep 14 '17 at 06:38