我如何打包我的Android库,使我的客户不会遇到类似";AAPT:错误:属性layout_behavior&



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

/Users/michaelosofsky/.gradle/caches/transforms-2/files-2.1/9f0df0bf18c337fe1883aef0ec2959c5/jetified-locoslabs-android-sdk-3.0.1/res/layout/ll_levels_selector_coordinator.xml:9:AAPT:错误:属性layout_behavior(又名com.example.mylocusmapsapplication:layout_behavior(未找到。

我们还通过在模块级build.gradle文件中添加以下内容来解决此错误:

implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

然而,随后我们收到了一个新错误:

/Users/michaelosofsky/.gradle/caches/transforms-2/files-2.1/9f0df0bf18c337fe1883aef0ec2959c5/jetified-locoslabs-android-sdk-3.0.1/res/layout/ll_levels_selector_coordinator.xml:9:AAPT:错误:资源字符串/bottom_sheet_behavior(又名com.example.mylocusmapsapplication:string/bottom_sheet_behavior(不是建立

我们再次通过在模块级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那里得到了一个很好的见解,即根本原因可能是库本身的打包不正确,就像Gradle的POM中缺少的Android库依赖项一样。

如何打包我的库,使我的客户不会遇到这些错误?

感谢@pavneet-singh,我们找到了这个问题的解决方案@megasoft78是正确的,根本原因是我的库打包不正确。以下是我的库的修正模块级build.gradle文件:

// 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块。以下错误的块导致了问题:

publications {
library(MavenPublication) {
groupId = '<MY_GROUP_ID>'
artifactId = '<MY_ARTIFACT_ID>'
version = '<MY_ARTIFACT_VERSION_NUMBER>'
artifact bundleReleaseAar
}
}

注意,我使用的是GitLab的软件包注册表,而不是JitPack.io,因为我一成为付费客户,JitPack.io的客户服务就没有响应。

相关内容

最新更新