【问题标题】:constraintlayout attributes in aar not recognised while buildling构建时无法识别 aar 中的约束布局属性
【发布时间】:2019-04-08 11:04:06
【问题描述】:

我创建了一个依赖于约束布局的库。该库通过 aar 文件包含在新项目中。该项目对其使用的所有 appNS 属性都给出以下错误。

home/vishal/.gradle/caches/transforms-1/files-1.1/my_library.aar/9012247ff26b45ffb7af7d608db342c5/res/layout/activity_main.xml:70: AAPT:错误:属性 layout_constraintBottom_toTopOf(又名 com.example.sampleapp:layout_constraintBottom_toTopOf) 未找到。

库编译 sdk : 28 支持版本:28.0.0

ConstraintLayout 作为实现添加到库中。项目中包含的 aar 库如下:

dependencies {
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    implementation files('libs/my_library.aar')
}

我已尝试将库和项目转换为 androidX,但问题仍然存在。



注意:

为了使用现有库测试场景 [更新]: 我在刮刮卡库 [https://github.com/Veeshal/scratchCardLayout] 中添加了约束布局以及实现约束布局的活动及其布局。将其作为 aar 文件导入。我在构建时没有收到任何错误。 验证创建一个示例项目并添加从上面共享的 github 存储库中的模块 scratchcardlayout 生成的 AAR 文件。


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .....

    <!-- Optional Design -->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:visibility="gone">

        ....

    </android.support.constraint.ConstraintLayout>

    <!-- Selected Design -->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_cropping_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        ....

    </android.support.constraint.ConstraintLayout>

    .....

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 传递依赖不包含在 aar 中,如果您希望它们自动包含在您的应用程序中,您可以将 aar 作为 maven 工件发布
  • 该库不应该是公开可用的,所以不能把它放在 maven 中。只能共享 aar。
  • 我觉得你不需要公开发布,你应该可以在本地“发布”它,查看this article
  • 传递依赖项不包含在 aar 中,并且有意如此,您要么在使用 aar 的应用程序中明确声明传递依赖项,要么将 aar 作为 maven 工件发布,以便传递依赖在 pom 文件中声明。您发布的项目生成的 aar 不包括 ConstraintLayout,如果它与其他示例应用程序一起使用,则意味着示例应用程序已经具有该依赖项(如项目本身中的演示应用程序)。
  • 在您使用 aar 的应用程序中运行 ./gradlew dependencies,您应该能够看到哪个依赖项正在导入 ConstraintLayout。

标签: android android-gradle-plugin android-constraintlayout android-library


【解决方案1】:

不能像 JAR 依赖一样添加本地 AAR 依赖。

需要在根项目的build.gradle中设置一个本地的flatDir仓库:

allprojects {
    repositories {
        ...
        flatDir {
            dirs "libs"
        }
    }
}

然后可以像引用任何其他依赖项一样引用AAR;注意@aar:

implementation "com.acme:somelibrary:1.0.0@aar"

而且很可能,您还必须使用 api 而不是 implementation(在库模块中)

...为了将ConstraintLayout 暴露给“库使用者”(即应用模块)。

【讨论】:

  • 对不起,我没有在问题中提到 flatDir 已经完全按照您提到的方式添加到我的根 gradle 文件中。 'api' 也对我不起作用。
  • @Veeshal 你仍然可以从implementation 中的excludeConstraintLayout 明确地将其直接添加到应用模块的dependencies 中。您在上面发布的依赖项在每种情况下都是错误的,并且可能几乎没有影响......我的答案的下半部分仍然适用。
  • App 模块中的 ConstraintLayout 依赖项肯定有效,但我不希望它被明确包含在内。与任何人共享时,应自动解决 Aar 库依赖关系。
  • @Veeshal 除非声明它,否则它可能不会被添加。这个answer 显示了另一种方法;但总的来说,它必须被声明(它可能不仅不知道ConstraintLayout,还知道整个AAR)。只需尝试使用./gradlew app:dependencies 即可查看包含的内容。
  • 如果我包含来自这个库 github.com/Veeshal/scratchCardLayout [随机选择] 的 aar 文件,那么我不会收到任何错误。
猜你喜欢
  • 2016-02-09
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多