gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:59 25 4
gpt4 key购买 nike

我正在使用 Android Studio。我正在编写一个名为检测器的 Android 库。它在创建它的项目中完美运行。

compile project(':detector')

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

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

然后我在新项目应用程序 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.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 项目。

======更新======

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

最佳答案

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

  1. aar 放在您要使用它的模块内的目录中(例如,应用模块中的 aarlibs)

  2. 将此目录声明为存储库(在应用程序的 build.gradle 中):

    repositories{
    flatDir{
    dirs 'aarlibs'
    }
    }

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

    dependencies {
    compile(name:'aar_file_name', ext:'aar')
    ...

关于java - 导入 Android 库 AAR - 无类定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40493210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com