Wear OS 上 XML 文件中的 Android 资源链接失败问题

问题描述

我正在 Wear OS 上开发表盘主题我有一个用于许多不同项目的核心模块,其中包含所有通用代码

特别是,我有这个复杂样式的 xml 文件

<drawable
    xmlns:app="http://schemas.android.com/apk/res-auto"
    class="android.support.wearable.complications.rendering.ComplicationDrawable"
    app:backgroundColor="@android:color/transparent"
    app:borderColor="@android:color/transparent"
    app:borderDashGap="0dp"
    app:borderDashWidth="1dp"
    app:borderRadius="52dp"
    app:borderStyle="solid"
    app:borderWidth="3dp"
    app:highlightColor="@android:color/transparent"
    app:iconColor="@color/white"
    app:rangedValuePrimaryColor="@android:color/transparent"
    app:rangedValueRingWidth="2.5dp"
    app:rangedValueSecondaryColor="@android:color/transparent"
    app:textColor="@color/white"
    app:textSize="12sp"
    app:textTypeface="sans-serif-condensed"
    app:titleColor="@color/white"
    app:titleSize="12sp"
    app:titleTypeface="sans-serif">

    <ambient
        app:highlightColor="@color/white"
        app:iconColor="@color/white"
        app:titleColor="@color/white"
        app:borderColor="@android:color/transparent"
        app:rangedValuePrimaryColor="@android:color/transparent"
        app:rangedValueRingWidth="1dp"
        app:rangedValueSecondaryColor="@android:color/transparent"
        app:textColor="@color/white"
    />
</drawable>

文件是这样调用的:

val drawable = ContextCompat.getDrawable(context,R.drawable.custom_complication_styles) as ComplicationDrawable

最初,一切顺利(在外观上),我的项目编译得很好(在调试模式下)。但是后来我注意到 Android Studio 抱怨我的核心模块的 gradle 依赖项。他想让我代替

    api "com.google.android.wearable:wearable:2.8.1"
    api "com.google.android.support:wearable:2.8.1"

    compileOnly "com.google.android.wearable:wearable:2.8.1"
    compileOnly "com.google.android.support:wearable:2.8.1"

问题在于,这样做会触发以下错误

AAPT:错误:未找到属性 borderColor(又名 com.myapp:borderColor)。

对于上述 xml 文件中的几乎每个属性都会触发相同的错误

此外,错误自动生成的中间文件有关,而不是原始文件。不知道是不是正常。在那个文件上,我提到了这个错误

enter image description here

如果相关,这里是我的核心模块的完整 gradle 文件

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-parcelize'
}

android {
    compileSdkVersion COMPILE_SDK_VERSION
    buildToolsversion BUILD_TOOL_VERSION

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 24
        targetSdkVersion TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    signingConfigs {
        release {
            keyAlias 'bs23'
            keyPassword '123456'
            storeFile file("$rootDir/keystore/android.jks")
            storePassword '123456'
        }
        debug {
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storeFile file("$rootDir/keystore/debug.keystore")
            storePassword 'android'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
            signingConfig signingConfigs.release
            debuggable false
        }
        debug {
            signingConfig signingConfigs.debug
        }
    }


    kotlinoptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    api 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.gms:play-services-location:17.1.0'
    api 'com.google.android.gms:play-services-wearable:17.0.0'
    api 'androidx.fragment:fragment-ktx:1.3.1'
    compileOnly 'com.google.android.gms:play-services-fitness:20.0.0'
    compileOnly 'com.google.android.gms:play-services-auth:19.0.0'
    compileOnly "com.google.android.wearable:wearable:$wearable_version"
    compileOnly "com.google.android.support:wearable:$wearable_version"
    api 'androidx.wear:wear:1.1.0'
    api "androidx.core:core-ktx:1.3.2"
    api 'com.android.billingclient:billing:2.1.0'
    api 'com.google.android.material:material:1.3.0'
    implementation 'com.google.android.libraries.places:places:2.4.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'

    //RX
    api 'io.reactivex.rxjava2:rxjava:2.2.19'
    api 'io.reactivex.rxjava2:rxandroid:2.1.1'

    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

因此在 'api' 和 'compileOnly' 之间来回移动使我的项目编译与否。我不知道我做错了什么。

解决方法

听起来您的项目正在通过您的核心模块访问属于 com.google.android.wearable:wearable:2.8.1 和/或 com.google.android.support:wearable:2.8.1 的类/值。我不认为这直接违反了任何最佳做法,因此您的选择是:

  1. 继续使用 api 向使用您的核心模块的项目公开 Wearable:wearable 和 support:wearable 功能。并禁止/忽略警告。
  2. 在您的核心模块中切换到 compileOnly 并在需要它的项目中添加一个依赖项(到 wearable:wearable 和/或 support:wearable)。您可能希望将版本号提取到一个中心位置,以确保它们始终使用相同的版本号。