简体   繁体   中英

Android resource linking failed when trying to use custom font in styles.xml

I'm trying to use custom fonts with new "android:fontFamily" attribute, added in Android O. While this works fin in xml:

android:fontFamily="@font/sf_pro_text_semibold"

using same construction in styles.xml like this:

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>

leads to following build error:

Execution failed for task ':app:processDebugResources'.

Android resource linking failed C:\\Users\\anro.gradle\\caches\\transforms-2\\files-2.1\\4125ed89f50d00591f7f19265a14b826\\res\\values\\values.xml: AAPT: error: resource font/sf_pro_text_semibold" (aka com.example.fonttesting:font/sf_pro_text_semibold") not found.

Lots of tutorials say that it should work, but it doesn't. I suppose the problem is in build.gradle file, but I don't even know what to try to make it work (Downgrading to older support library version is not a good solution). Here is my build.gradle file:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.example.fonttesting"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

So, does anybody know how to make custom fonts in styles.xml work with androidx?

AAPT: error: resource font/sf_pro_text_semibold" (aka com.example.fonttesting:font/sf_pro_text_semibold") not found.

There is a typo:

Remove the " char:

<item name="android:fontFamily">@font/sf_pro_text_semibold</item>

instead of

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>

I had similar issue, my issue was given extension in style

<item name="android:fontFamily">@font/muli.ttf</item>
<item name="fontFamily">@font/muli.ttf</item>

After removing the extension it worked for me.

<item name="android:fontFamily">@font/muli</item>
<item name="fontFamily">@font/muli</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM