我有一个 android 基础项目,其中有一些可重用的模块,以便我可以在任何项目中使用它们,我有 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 文件并直接在我的项目中使用它们。但是当我将所有四个 aars 放入我的项目 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 并在项目中使用。可以吗?
最佳答案
所以真正的问题是,以这种方式创建的 aar 和 jar 不包含它们所依赖的库。如果以这种方式添加库,则需要将其所有依赖项添加到使用它的项目中。
在我的示例中,它是实现 'com.github.mabbas007:TagsEditText:1.0.5',我已将其添加到 app - gradle 文件中,同步后它工作正常。
如果项目包含大量此类依赖项,则应将其全部添加。但是您仍然可以遇到诸如“ list 合并失败”之类的问题和其他问题。
或者您可以尝试将您的 lib 部署到 maven 或 jitpack,但您需要正确设置以确保生成的 pom 文件包含所有需要的依赖项,以便能够轻松使用此库。
关于android - 如何在android中将模块依赖转换为aar依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989590/