导入应用程序后,无法在arr文件中使用依赖项

时间:2016-08-25 08:44:07

标签: android android-library aar

我正在尝试创建一个使用recyclerview的库,然后将该库,即。,arr文件导入应用程序项目。

我在库的gradle文件中所做的更改是

  • apply plugin:'com.android.library'
  • 删除了 applicationId
  • 然后生成 aar 文件

库的gradle文件有一个依赖项列表

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.volley:volley:1.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:24.2.0'

}

现在我将aar文件导入我的应用程序。当我尝试访问包含recyclerview的活动时,应用程序崩溃了以下错误...

FATAL EXCEPTION: main 
            Process: com.example.alertlibtest, PID: 32032
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alertlibtest/com.example.example.tejlib.Jaamoon}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:148)
                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                    Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                    at com.example.example.tejlib.Jaamoon.onCreate(Jaamoon.java:34)
                    at android.app.Activity.performCreate(Activity.java:6237)
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                    at com.example.example.tejlib.Jaamoon.onCreate(Jaamoon.java:34) 
                    at android.app.Activity.performCreate(Activity.java:6237) 
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.alertlibtest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.alertlibtest-1/lib/x86, /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 android.view.LayoutInflater.createView(LayoutInflater.java:583)
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                    at com.example.example.tejlib.Jaamoon.onCreate(Jaamoon.java:34) 
                    at android.app.Activity.performCreate(Activity.java:6237) 
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[dex file "/data/data/com.example.alertlibtest/files/instant-run/dex/slice-support-annotations-24.2.0_28ab

请帮助摆脱这个错误。我想访问我在库中包含的其他库,即 排球 滑行

提前致谢。

2 个答案:

答案 0 :(得分:0)

您的app-build似乎不知道您的库需要com.android.support:appcompat-v7:24.2.0所以它没有打包到您的apk中。

您有两种选择:

  1. 如果您希望继续将您的图书馆作为简单的aar文件保留:
    在您的应用中重新声明您库的所有依赖项
  2. 以其他方式包含您的库,因此gradle可以知道传递依赖项:
    • Gradle Module Dependency
    • Maven Dependency
    • 常春藤依赖

答案 1 :(得分:0)

如果要将库发布到工件库,则需要配置publish gradle命令以添加库所具有的依赖项。在Bintray,我这样做了

artifacts {
    archives generateSourcesJar
}

task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}

然后你只需要告诉gradle你正在使用你的库包含的类。你可以这样做

compile('com.yourpage:yourlibrary:1.0.0'){
    transitive=true
}