我正在使用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之前将它们设置为传递依赖关系并没有帮助。
答案 0 :(得分:2)
我不确定问题是该类是否未导入(否则您无法编译)但是,如果您在没有其他模块的情况下导入本地aar
会发生什么?
将aar
放在您要使用它的模块内的目录中(例如,应用模块中的aarlibs
)
将此目录声明为存储库(在应用程序的build.gradle
中):
repositories{
flatDir{
dirs 'aarlibs'
}
}
android {
...
将其引用为依赖项(也在应用程序的build.gradle
中):
dependencies {
compile(name:'aar_file_name', ext:'aar')
...
答案 1 :(得分:2)
aar文件没有传递依赖项,并且没有描述库使用的依赖项的pom文件。
表示,如果要导入aar文件,则还必须在项目中指定依赖项。
您应该使用maven存储库(您必须在私有或公共maven存储库中发布该库)。
答案 2 :(得分:2)
上面的Android Studio v-4.2中.AAR/JAR的导入方式有变化,所以用这个https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency
答案 3 :(得分:1)
假设您有两个项目:
在您的工作项目(2)中,您必须为您的aar文件添加一个新模块(1)。其过程如下,在您现有的某个模块上按下鼠标右键,选择“打开模块设置”,然后按“+”符号添加新模块,选择导入jar / aar并选择相关文件是在第1节中生成的。
您会注意到使用以下代码生成了新模块的新gradle文件:
configurations.maybeCreate("default")
artifacts.add("default", file('name_of_you_aar_file.aar'))
创建模块后,您必须使用您在与模块相关的build.gradle中编写的内容将新模块链接到您的应用模块:compile project(':name_of_your_module')
让我知道它是否适合你。
答案 4 :(得分:1)
使用上述步骤将使Android Studio生成新模块并自动编辑 .gradle 文件。或者,您可以通过编辑特定模块的build.gradle文件直接在应用程序模块中包含.AAR。