我的图书馆如何在Android Studio中导入另一个图书馆?

时间:2016-03-28 20:02:42

标签: android exception import aar

我从Android中的库开始,我已经设法以这种方式在我的应用程序中导入库:

  1. 将.aar文件添加到/ libs文件夹
  2. 添加build.gradle:

    repositories{
      flatDir {
        dirs'libs'
    }
    dependencies {
        ...
        compile(name:'mylibrary1',ext:'aar')
    } 
    
  3. 这样做很好,但我做了mylibrary2并重复mylibrary1中的第1步和第2步,现在mylibrary1可以使用mylibrary2

    总之,我希望我的应用导入mylibrary1mylibrary1以导入mylibrary2

    当我从mylibrary2调用某个方法时问题出在我的应用中我得到一个例外:java.lang.NoClassDefFoundError,我希望有人可以帮助我

    如果有帮助,这是stackstrace:

    E/AndroidRuntime: FATAL EXCEPTION: main
     Process: ar.com.tally.myapplication, PID: 30197
     java.lang.NoClassDefFoundError: Failed resolution of: Lar/com/tally/mylibrary2/Class2;
         at ar.com.tally.mylibrary1.Class1.get1(Class1.java:10)
         at ar.com.tally.myapplication.MainActivity$1.onClick(MainActivity.java:28)
         at android.view.View.performClick(View.java:5076)
         at android.view.View$PerformClick.run(View.java:20279)
         at android.os.Handler.handleCallback(Handler.java:739)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:135)
         at android.app.ActivityThread.main(ActivityThread.java:5910)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
      Caused by: java.lang.ClassNotFoundException: Didn't find class "ar.com.tally.mylibrary2.Class2" on path: DexPathList[[dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-support-annotations-23.2.1_d668123607e57c7aa4406ba98a4276e21606428b-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-internal_impl-23.2.1_eca880fd3aaa2f093d820f9d7b8841aa1fa00b4f-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-intermediates-exploded-aar-mylibrary1_7763b008eb2396101d055fb29b3e03d5ee273add-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.2.1_5e6bcbbe4699b2a562a7ba7b6349f8bbfbef9fb4-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-support-v4-23.2.1_c564c185a6a1edef108674e96bf4712114e0c95a-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-recyclerview-v7-23.2.1_a1ec4cea5854a30b896e5f639d2fd95cd214b2b1-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-design-23.2.1_2c4b9902e86719e2bd0652ddfba695894bed0a47-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.2.1_01059d3db947a24736ebcedd233fd40b734ccf3e-classes.dex", dex file "/data/data/ar.com.tally.myapplication/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.2.1_69d3f470bac3bccd2f54179c6363fce8b9091976-classes.dex"],nativeLibraryDirectories=[/vendor/lib, /system/lib, /vendor/lib, /system/lib]]
         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
         at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
         at ar.com.tally.mylibrary1.Class1.get1(Class1.java:10) 
         at ar.com.tally.myapplication.MainActivity$1.onClick(MainActivity.java:28) 
         at android.view.View.performClick(View.java:5076) 
         at android.view.View$PerformClick.run(View.java:20279) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5910) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
         Suppressed: java.lang.ClassNotFoundException: Didn't find class "ar.com.tally.mylibrary2.Class2" on path: DexPathList[[zip file "/data/app/ar.com.tally.myapplication-1
    

1 个答案:

答案 0 :(得分:1)

所以当你制作mylibrary2时,你也改变了mylibrary1,所以你需要用新的aars替换两个aars并重新构建。

但是,如果您在与模块相同的项目中拥有这些库,我建议您将它们作为项目导入而不是手动处理。

[(1, 2), (3, 4), (5, 6)] [(1, 2), (3, 6), (4, 5)] [(1, 4), (2, 3), (5, 6)] [(1, 4), (2, 5), (3, 6)] [(1, 6), (2, 3), (4, 5)] [(1, 6), (2, 5), (3, 4)]

或者如果在不同的项目中,就像在这种情况下你可以在存储库中添加mavenLocal():

compile project(':mylibrary1')
compile project(':mylibrary2')

在您的lib项目中,您可以添加maven插件:

    repositories {
     mavenLocal()
...
}

然后你需要打电话:

  

gradle install

将在您的本地maven仓库中安装lib。所以你不需要手动复制它们