簡體   English   中英

如何打包我的 Android 庫,以便我的客戶不會遇到諸如“AAPT:錯誤:屬性 layout_behavior”之類的錯誤

[英]How can I package my Android library so that my customer does not experience errors like "AAPT: error: attribute layout_behavior"

我制作了一個 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_xml:9:錯誤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_bottomxml:9:coordinator. (又名 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 那里得到了一個很好的見解,根本原因可能是庫本身可能被錯誤地打包,就像POM with Gradle 中缺少的 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 的客戶服務就變得沒有響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM