java – 导入Android库AAR – 无类定义

我正在使用Android Studio.我正在写一个名为detector的Android库.它在创建它的项目中完美运行.

compile project(':detector')

我想将它生成的AAR文件导入另一个项目.我这样做:

File > New > New Module > Import .JAR/.AAR Package

然后我在新项目app build.gradle中添加相同的行:

compile project(':detector')

清理和构建后,我运行应用程序并收到此错误:

11-18 06:32:14.151 21283-21283/testing.com.test D/ResourcesManager: For user 0 new overlays fetched Null
11-18 06:32:14.161 21283-21283/testing.com.test W/System: ClassLoader referenced unknown path: /data/app/testing.com.test-1/lib/arm
11-18 06:32:15.131 21283-21283/testing.com.test W/System: ClassLoader referenced unknown path: /data/app/testing.com.test-1/lib/arm
11-18 06:32:15.131 21283-21283/testing.com.test D/ContextRelationManager: ContextRelationManager() : FEATURE_ENABLED=true
11-18 06:32:15.131 21283-21283/testing.com.test D/RelationGraph: garbageCollect()
11-18 06:32:15.301 21283-21283/testing.com.test D/RelationGraph: garbageCollect()
11-18 06:32:15.501 21283-21283/testing.com.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test D/AndroidRuntime: Shutting down VM
11-18 06:32:15.631 21283-21283/testing.com.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: testing.com.test, PID: 21283
                                                                         java.lang.NoClassDefFoundError: detector.Manager
                                                                             at detector.Detector.<clinit>(Detector.java:39)
                                                                             at testing.com.test.MainActivity.onCreate(MainActivity.java:17)
                                                                             at android.app.Activity.performCreate(Activity.java:6904)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我解压缩AAR并反编译Classes.jar文件以查找Manager文件是否存在:

Manager.class
Manager$3.class
Manager$2.class
Manager$1.class

这是库build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName '1'
    }
    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 ('org.altbeacon:android-beacon-library:2.8.1')
    compile ('com.google.android.gms:play-services-ads:9.6.1')
    compile 'com.android.support:appcompat-v7:23.4.0'
}

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
    }
}

此外,我能够将库部署到jCenter,并且能够在链接到jCenter安装时在其他项目中成功运行库.只是导致此问题的直接AAR导入.

请建议构建Android库AAR并将其导入另一个Android项目.

====== UPDATE ======

我发现,由于@bwt的帮助,如果我在引导AAR的应用程序build.gradle中包含库依赖项,问题就解决了.如何确保依赖项(google-play和altbeacon)包含在AAR中?在构建AAR之前将它们设置为传递依赖关系并没有帮助.

最佳答案:

我不确定问题是该类是不是导入的(否则你无法编译)但是为了防止在没有附加模块的情况下导入本地aar会发生什么?

>将aar放在要使用它的模块内的目录中(例如app模块中的aarlibs)
>将此目录声明为存储库(在应用程序的build.gradle中):

repositories{
    flatDir{
        dirs 'aarlibs'
    }
}

android {
...

>将其引用为依赖项(也在应用程序的build.gradle中):

dependencies {
    compile(name:'aar_file_name', ext:'aar')
...
上一篇:在Android Studio中导入Urban Airship会产生编译问题吗?


下一篇:AndroidStudio项目打包成jar