在aar中找不到类 [英] Class not found in aar

查看:2304
本文介绍了在aar中找不到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导致:我的Android库中没有发现类(aar) java.lang.ClassNotFoundException:未在路径中找到类com.google.android.gms.common.GoogleApiAvailability:DexPathList [[zip文件/data/app/com.myunityplugin.PushNotifications-1/base.apk ],nativeLibraryDirectories = [/ data / app / com.myunityplugin.PushNotifications-1 / lib / arm,/ vendor / lib,/ system / lib]] 

这个错误是不言自明的,但我不确定我做错了什么。



我在gradle.build的依赖项下有以下内容:

  compile'c​​om.google.android.gms:play-services:8.4.0'
compile'c​​om.google.android.gms:play-services-gcm:8.4 .0'

我认为问题在于play-services库需要包含在aar ,编译不会这样做。我尝试使用'运行时',但我得到未找到gradle dsl运行时。



问题:


  1. 这是一个问题,我需要在我的aar中包含play-services库,并且我该如何实现? (如果没有,我该如何解决这个问题)

  2. 我需要指定play-services还是play-services-gcm足够?



错误信息/设置



导致错误的行



  GoogleApiAvailability ServiceAvailability = GoogleApiAvailability.getInstance(); 

例外情况

  05-23 05:38:48.221 28055-28055 /? E / AndroidRuntime:FATAL EXCEPTION:main 
过程:com.myunityplugin.PushNotifications,PID:28055
java.lang.NoClassDefFoundError失败: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 )
在android.app.ActivityThread.main(ActivityThread.java:6897)
在java.lang.reflect.Method.invoke(本地方法)
在java.lang.reflect.Method .invoke(Method.java:372)
at com.android.internal.os.ZygoteInit $ Me thodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
引起:java.lang.ClassNotFoundException:未找到在路径上的类com.google.android.gms.common.GoogleApiAvailability:DexPathList [[zip文件/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)$ b $在java.lang.ClassLoader.loadClass(ClassLoader.java:469)
在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和roid.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 $ b at java.lang。 Class.classForName(本地方法)$ b $在java.lang.BootClassLoader。 $ java.util.ClassLoader.loadClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)$ b $ at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
导致:java.lang.NoClassDefFoundError:未找到使用引导类加载器的类;没有可用的堆栈

应用程序级别build.gradle

  apply plugin:'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion 23.0.2

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

依赖关系{
编译fileTree(dir:'libs',include:['* .jar'])
testCompile'junit:junit:4.12'
compile'c​​om.android.support:appcompat-v7:23.2.1'
compile'c​​om.google.android.gms:play-services:8.4.0'
compile'c​​om.google.android。 gms:play-services-gcm:8.4.0'
提供的文件('lib-不包含/ classes.jar')
}

项目级gradle。 build

  //顶级构建文件,您可以在其中添加对所有子项目/模块通用的配置选项。 
$ b buildscript {
repositories {
jcenter()
}
依赖关系{
classpath'com.android.tools.build:gradle: 1.5.0'

//注意:不要在这里放置您的应用程序依赖关系;它们属于单个模块build.gradle文件中的
//
}
}

allprojects {
存储库{
jcenter()



任务清理(类型:删除){
删除rootProject.buildDir
}
aar 文件不包含嵌套(或 href =https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies =noreferrer>传递性)相关性
,而不是有一个描述库使用的依赖关系的pom文件。



这意味着,如果您使用 flatDir 导入aar文件, code> repo,你必须在你的项目中指定依赖项。



你应该使用 maven仓库图书馆在私人或公共maven回购),你不会有同样的问题。

在这种情况下,gradle会使用包含依赖项列表的pom文件下载依赖项。


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
}

解决方案

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.

这篇关于在aar中找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆