android - 如何打包我的 Android 库,以便我的客户不会遇到像 "AAPT: error: attribute layout_behavior"这样的错误

标签 android gradle android-gradle-plugin android-coordinatorlayout

我创建了一个 Android 库,当我的客户尝试将其导入到名为 mylocusmapsapplication 的项目的模块级 build.gradle 文件中时,该库无法链接。客户的应用程序中几乎没有任何内容,它是在 Android Studio 中通过转到 File > New > New Project... > Empty Activity 创建的。然后我们简单地将我的库添加为依赖项。但是当他们尝试编译时,它给出了以下错误:

/Users/michaelosofsky/.gradle/caches/transforms-2/files-2.1/9f0df0bf18c337fe1883aef0ec2959c5/jetified-locuslabs-android-sdk-3.0.1/res/layout/ll_levels_selector_coordinator.xml:9: AAPT: error: attribute layout_behavior (aka com.example.mylocusmapsapplication:layout_behavior) not found.


我们还可以通过将以下内容添加到他们的模块级 build.gradle 文件来解决此错误:implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"但是,随后我们收到了一个新错误:

/Users/michaelosofsky/.gradle/caches/transforms-2/files-2.1/9f0df0bf18c337fe1883aef0ec2959c5/jetified-locuslabs-android-sdk-3.0.1/res/layout/ll_levels_selector_coordinator.xml:9: AAPT: error: resource string/bottom_sheet_behavior (aka com.example.mylocusmapsapplication:string/bottom_sheet_behavior) not found.


我们再次通过向他们的模块级 build.gradle 文件添加另一个依赖项来解决这个问题:implementation "com.google.android.material:material:1.3.0-alpha02"然后客户能够编译、链接和运行他们的项目。
但我对这种解决方法并不满意。所以我尝试改变我的库如何包含这两个依赖项。
在我的库的模块级 build.gradle 文件中,我尝试从以下内容进行更改:
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
implementation "com.google.android.material:material:1.3.0-alpha02"
对此:
api "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
api "com.google.android.material:material:1.3.0-alpha02"
我重新发布了我的库,但是当我的客户在没有上述解决方法的情况下自行尝试我的库的新版本时,他们看到了相同的错误消息。
我的库使用 :coordinatorlayout: 和 :material: 库,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:background="@color/ll_darkDarkTranslucent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/llLevelSheetLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        app:layout_behavior="@string/bottom_sheet_behavior">
我相信app:layout_behavior属性在 :coordinatorlayout: 库中定义,其值 @string/bottom_sheet_behavior在 :material: 库中定义。
更新 1:当我更多地使用解决方法解决这个问题时,我发现了一堆其他有类似问题的依赖项。以下是我的 SDK 所依赖的库导致 java.lang.NoClassDefFoundError在运行时。
    implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
    implementation "com.google.android.material:material:1.3.0-alpha02"
    implementation "com.mapbox.mapboxsdk:mapbox-android-sdk:9.0.0"
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "org.kamranzafar:jtar:2.2"
    implementation "org.tukaani:xz:1.5"
    implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"
    implementation "me.xdrop:fuzzywuzzy:1.2.0"
    implementation "androidx.fragment:fragment-ktx:1.3.0-alpha07"
我能够找到一些关于 retrofit 的类似问题的引用资料。图书馆,有人使用与我相同的解决方法:https://github.com/square/retrofit/issues/3266#issuecomment-632362066
更新 2:我从@megasoft78 那里得到了一个深刻的见解,根本原因可能是库本身,它可能像 Android library dependencies missing from POM with Gradle 中那样打包不正确。 .
如何打包我的库以使我的客户不会遇到这些错误?

最佳答案

感谢@pavneet-singh,我们找到了解决这个问题的方法。 @megasoft78 是正确的,根本原因是我的库被错误地打包。这是更正后的 module-level build.gradle file对于我的图书馆:

// This must go at the top of build.gradle
plugins {
    id 'maven-publish'
}
// This can go anywhere in the build.gradle
// I have removed my values wherever it says <MY_XXX> below
project.afterEvaluate {
    publishing {

        // How to package the library
        publications {
            library(MavenPublication) {
                from components.release
                groupId = '<MY_GROUP_ID>'
                artifactId = '<MY_ARTIFACT_ID>'
                version = '<MY_ARTIFACT_VERSION_NUMBER>'
            }
        }

        // Where to publish the library (GitLab Package Registry in my case)
        repositories {
            maven {
                url "https://gitlab.com/api/v4/projects/<MY_GITLAB_GROUP_ID>/packages/maven"
                name "GitLab"
                credentials(HttpHeaderCredentials) {
                    name = 'Job-Token'
                    value = System.getenv("CI_JOB_TOKEN")
                }
                authentication {
                    header(HttpHeaderAuthentication)
                }
            }
        }
    }
}
关键变化是 publications堵塞。以下不正确的 block 是导致问题的原因:
        publications {
            library(MavenPublication) {
                groupId = '<MY_GROUP_ID>'
                artifactId = '<MY_ARTIFACT_ID>'
                version = '<MY_ARTIFACT_VERSION_NUMBER>'
                artifact bundleReleaseAar
            }
        }
注意我正在使用 GitLab's Package Registry而不是 JitPack.io因为当我成为付费客户后,JitPack.io 的客户服务就没有响应了。

关于android - 如何打包我的 Android 库,以便我的客户不会遇到像 "AAPT: error: attribute layout_behavior"这样的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63961378/

相关文章:

java - GooglePlayService 需要更新但 PlayStore 没有更新

android - 通过处理程序获取 ToggleButton 的状态

java - android onTouchEvent "Click and Hold"

java - Gradle 项目同步失败 - Android Studio 0.8.14

android - 找到多个具有操作系统独立路径 'META-INF/INDEX.LIST' 的文件

android - 更新到android gradle插件3.5后出现proguard问题

android - AppExtension 类型对象的未知属性 dynamicFeatures

java - 如何检查 Android 中的互联网访问是否可用

java - Gradle 从同一个项目创建 war 和 jar

java - 如何在Intellij中使用Gradle构建OpenFire插件?