Unable to use the dependencies in the arr file after importing in the application

烂漫一生 提交于 2019-12-25 04:27:35

问题


I am trying to create a library, that uses recyclerview, and then importing that library ie., arr file to an application project.

Changes i did in the library's gradle file are

  • apply plugin: 'com.android.library'
  • removed the applicationId
  • And then generated aar file

The library's gradle file has a list of dependencies

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'

}

Now i imported the aar file to my app. When i try to access the activity containing the recyclerview, the app crashes the following err...

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

Please help to get rid of this error. I want to access the other libraries that i include in the library ie volley and glide.

Thanks in advance.


回答1:


It seems like your your app-build does not know your library needs com.android.support:appcompat-v7:24.2.0 so it's not packaged into your apk.

You have two options:

  1. If you want to stay with including your library as simple aar file:
    Redeclare all dependencies of your library in your app
  2. Include your library in another way, so gradle can know the transitive dependencies:
    • Gradle Module Dependency
    • Maven Dependency
    • Ivy Dependency



回答2:


If you're publishing your library to an artifactory repository, you need to configure your publish gradle commands to add the dependencies your library has. In Bintray I do it in this way

artifacts {
    archives generateSourcesJar
}

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

Then you only need to tell gradle that you're using the classes that your library contains. You can do it in this way

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


来源:https://stackoverflow.com/questions/39140591/unable-to-use-the-dependencies-in-the-arr-file-after-importing-in-the-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!