在其他项目中使用 .AAR 文件的问题 [英] Issue with using .AAR file in other project

查看:33
本文介绍了在其他项目中使用 .AAR 文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在另一个项目中使用我的 android 库项目(有资源).这是我到目前为止所做的:

  1. 在 Android Studio 中创建了一个库项目.构建该项目,因此生成的 .aar 文件如下所示:..\MyApplication\mylibrary\build\outputs\aar\mylibrary-debug.aar

  2. 在 Android Studio 中创建了一个新的 Android 项目.然后,我(归功于 Dwayne)似乎对我来说效果很好.总结:

    • 声明您的平面文件存储库.此存储库声明位于项目模块中的 gradle.build 文件中而不是父目录中的顶级 gradle.build 文件:

      存储库{平面目录{目录'库'}}

    • 将您的 mylibrary-release.aar 或 mylibrary-debug.aar 从 ..\MyLibraryProject\mylibrary\build\outputs\aar\ 复制到应用程序模块中的 libs 文件夹 ..\MyApplicationProject\app\libs.

    • 声明您对库的依赖.

      依赖项{编译'com.example.library:mylibrary:release@aar'}

    注意依赖声明的命名空间部分 (com.example.library) 被平面文件"存储库忽略,而依赖声明中的命名空间不正确不会影响任何事情,您应该确保命名空间正确,以防万一您稍后会转移到图书馆存储库.

    <小时>

    另一种选择可能是创建本地 Maven 存储库,如这里,但我没试过这个选项.

    I have to use my android library project (which has resources), in another project. Here is what I did so far:

    1. Created a library project in Android Studio. Build that project, so .aar file was generated like: ..\MyApplication\mylibrary\build\outputs\aar\mylibrary-debug.aar

    2. Created a new Android project in Android Studio. Then, I followed this:

      File >> New Module >> Import .JAR or .AAR package

    This gives me following structure in my project:

    There was no any error in gradle sync or while I make the project.

    But now, if I try to use class name of my Library project, it doesn't get shown in auto suggest. There is one class MyClass.java in my library project. In IDE, if I try to write MyC, I am not able to see MyClass. It shows No suggestions instead.

    Am I missing some configuration?

    Let me know in case you want other information related to folder structure or build.gradle file.

    解决方案

    This alternative (credits go to Dwayne) seems to work fine for me. Summarizing:

    • Declare your flat file repository. This repository declaration goes in the gradle.build file in your project module and not the top level gradle.build file in the parent directory:

      repositories{
         flatDir {
           dirs 'libs'
         }
      }
      

    • Copy your mylibrary-release.aar or mylibrary-debug.aar from ..\MyLibraryProject\mylibrary\build\outputs\aar\ to the libs folder in your application module ..\MyApplicationProject\app\libs.

    • Declare your dependency on the library.

      dependencies {
         compile 'com.example.library:mylibrary:release@aar'
      }
      

    Note the namespace section (com.example.library) of the dependency declaration is ignored by the ‘flat file’ repository, while having an incorrect namespace in the dependency declaration doesn’t effect anything you should ensure the namespace is correct incase you move to a library repository at a later date.


    Another alternative may be creating a local Maven repository as explained here, but I haven't tried this option.

    这篇关于在其他项目中使用 .AAR 文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆