在aar找不到的类

Shr*_*der 8 android gradle google-play-services android-gradle-plugin aar

当调用GoogleApiAvailability时,我的Android库(aar)中的类未找到异常.

    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]]
Run Code Online (Sandbox Code Playgroud)

错误是不言自明的,但我不确定我做错了什么.我很擅长gradle和AndroidStudio.

我的gradle.build中有以下依赖项:

    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
Run Code Online (Sandbox Code Playgroud)

我认为问题是播放服务库需要包含在aar中,而编译不会这样做.我尝试使用'运行时',但我得到"找不到gradle dsl运行时".

问题:

  1. 这是一个我需要在我的网站中包含播放服务库的问题,我该如何实现?(如果没有,我该如何解决这个问题)
  2. 我是否需要指定播放服务或播放服务-gcm是否足够?

错误信息/设置

导致错误的行

GoogleApiAvailability ServiceAvailability = GoogleApiAvailability.getInstance();
Run Code Online (Sandbox Code Playgroud)

例外:

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
Run Code Online (Sandbox Code Playgroud)

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')
}
Run Code Online (Sandbox Code Playgroud)

项目级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
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 16

AAR文件不包含嵌套(或传递)的依赖关系,并且不具有其描述库所使用的依赖关系的POM文件.

这意味着,如果要使用flatDirrepo 导入aar文件,则还必须在项目中指定依赖项.

您应该使用maven存储库(您必须在私有或公共maven存储库中发布库),您将不会遇到相同的问题.
在这种情况下,gradle使用包含依赖项列表的pom文件下载依赖项.