【问题标题】:How to convert a module dependency to aar dependency in android?如何在android中将模块依赖转换为aar依赖?
【发布时间】:2020-07-20 06:14:50
【问题描述】:

我有一个 android 基础项目,其中有一些可重用的模块,以便我可以在任何项目中使用它们,我有 4 个模块依赖项,例如 -

  1. 模块一
  2. 模块二
  3. 模块三
  4. 模块四

我在 moduleThree 的 gragle 中定义的地方 -

    implementation project(':moduleOne')
    implementation project(':moduleTwo')
    implementation project(':moduleFour')

这意味着我正在使用 moduleThree 中的所有 3 个模块。

在我的项目 gradle 文件中,我已经这样定义了 -

    implementation project(':moduleOne')
    implementation project(':moduleTwo')
    implementation project(':moduleThree')
    implementation project(':moduleFour')

在我的应用程序的 setting.gradle 文件中 -

include ':app', ':moduleOne', ':moduleTwo', ':moduleThree', ':moduleFour'

所以到目前为止,我在我的应用程序中将其用作模块,因此我必须复制文件夹,然后将其粘贴到我的原始项目中。我发现这是一项乏味的工作,所以我决定创建一个 aar 文件并直接在我的项目中使用它们。

但是当我将所有四个 aar 都放入我的项目 B 中时,我无法访问它的资源文件,它引发了一些异常 - Android 资源链接失败。喷射。 我试过了

  • 清理项目
  • 使缓存失效并重新启动
  • 重建项目
  • 删除构建文件夹

所以我的问题是除了提供我只想提供 aar 文件的所有模块,然后任何项目都可以将它们添加为依赖项。我已经生成了 aar 文件,但运行项目时遇到问题 -

     <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:TagsEditText="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sv_scan_description"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <com.example.moduleThree.ui.TagsEditText
                        android:id="@+id/tagsEditText"
                        style="@style/TextEditTheme"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        TagsEditText:tagsBackground="@drawable/send_to_email_bg"
    ---/>
</ScrollView>

AAPT:错误:找不到属性 tagsBackground(又名 com.example.myproject:tagsBackground)。

注意-生成的 Aar 文件是 moduleOne-debug.aar 和 moduleOne-release.aar,我将第一个版本重命名为 moduleOne.aar 并在项目中使用。还好吗?

【问题讨论】:

    标签: android gradle aar android-module


    【解决方案1】:

    所以真正的问题是,以这种方式创建的 aar 和 jar 不包含它们所依赖的库。如果以这种方式添加库,则需要将其所有依赖项添加到使用它的项目中。

    在我的示例中,它是实现 'com.github.mabbas007:TagsEditText:1.0.5',我已将其添加到 app - gradle 文件中,同步后它工作正常。

    如果项目包含大量此类依赖项,则应将其全部添加。但是您仍然可能会遇到诸如“清单合并失败”和其他问题的问题。

    或者您可以尝试将您的 lib 部署到 maven 或 jitpack,但您需要正确设置以确保生成的 pom 文件包含所有需要的依赖项,以便能够轻松使用此库。

    【讨论】:

    • 我也面临同样的问题,你的最终解决方案是什么?
    • @digiwizkid 如果您在模块中使用任何 gradle 依赖项,请确保您已将所有依赖项添加到主 gradle(应用程序级文件)中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2017-05-14
    • 2015-07-03
    相关资源
    最近更新 更多