如何解决如何打包我的Android库,以使客户不会遇到诸如“ AAPT:错误:属性layout_behavior”之类的错误
我制作了一个Android库,当我的客户尝试将其导入模块级build.gradle文件中的mylocusmapsapplication项目时,该库无法链接。客户的应用程序几乎没有内容,它是在Android Studio中通过转到文件>新建>新建项目...>空活动创建的。然后,我们只是将我的库添加为依赖项。但是当他们尝试编译时,出现了以下错误:
/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:错误:属性layout_behavior(aka 找不到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-locuslabs-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那里获得了深刻的见识,根本原因可能是库本身,它可能像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
块。导致此问题的原因是以下不正确块
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的客户服务就会变得无响应。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。