AAPT: error: attribute android:forceDarkAllowed not found
Asked Answered
S

5

21

Im trying to use android dark theme. I install android studio 3.5 preview.

compileSdkVersion 28 targetSdkVersion 28

but still getting this error. Is this dark theme bug or Im doing something wrong?

build.gradle{
dependencies {
    // ...
    implementation 'com.google.android.material:material:1.1.0-alpha06'
    // ...
  }}

styles.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <item name="android:forceDarkAllowed">true</item>
</style>

project link

https://github.com/googlesamples/android-DarkTheme

Smasher answered 16/5, 2019 at 7:34 Comment(1)
Have you seen this issue? github.com/googlesamples/android-DarkTheme/issues/2Holocaine
L
20

You need to up your compiledSdkVersion to 29, in you app build.gradle:

android {
    compileSdkVersion 29
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        ...
}

Then you should be good to go!

Lehet answered 4/9, 2019 at 7:32 Comment(2)
What if I can't / don't want to use version 29 but stay at 28?Laughlin
Then you must not use android:forceDarkAllowed because below 29 dark mode is unknown feature. And how you app behaves when installed on 29 and up? Check yourself.Natural
A
8

I was searching for an answer and as @Ruben pointed out)

Changing compiledSdkVersion 28 to compileSdkVersion 'android-Q' should solve the issue. android:forceDarkAllowed attribute was only added in Android Q.

Unrelated to this issue, but it seems like you need to also update 'androidx.appcompat:appcompat:1.1.0-alpha04' to 'androidx.appcompat:appcompat:1.1.0-alpha05' or the theme switching doesn't work properly.

Afra answered 4/6, 2019 at 9:29 Comment(1)
Yes, this is correct - thanks for pointing it out. If you don't update to appcompat version, it won't switch themes immediately. You have to restart the app to make the switch.Dahomey
B
3

I found the answer in android studio suggestions. for clarification the tag android:forceDarkAllowed is only found in api level 29+ so what we should do is create a folder values-v29 and then write the tag in it like below

in file values/____.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    //removed from here
</style>

in file values-v29/____.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <item name="android:forceDarkAllowed">true</item>
</style>
Biamonte answered 3/12, 2021 at 12:12 Comment(1)
I guess that this answer is too new to attract people googling for this issue (the question is two years old), however this was the solution I needed. I need compatibility with API level 26 and increasing it to 29 just to fix an issue with Xiaomi phones is just a no go for me. For others looking to apply it, the names of both files where "styles.xml" in my case, and worked like a charm both in Android 8 and the stupid Xiaomi phone that was messing the dark mode.Distilled
W
2

I encountered the same error message when I tried to run my previously untouched Angular based NativeScript "Hello World" android application. (In command prompt: tns create, tns run android --bundle)

For me the solution was: 1. Open SDK Manager in Android Studio 2. Install Android 10.0 (Q) (API Level: 29) SDK Platform.

After these steps I could start running my app without any errors!

Willingham answered 8/11, 2019 at 15:12 Comment(2)
I have the same setting still getting the error with nativescript 6.2Lundquist
I've done all of the above and still get this error. Surely it cant be this hard. What other gotchas do i need to be aware of?Endsley
I
-6

See the Nativescript docs.

Remove below line:

<item name="android:forceDarkAllowed">true</item>

from file

<project>/app/App_Recoures/Android/src/main/res/values/styles.xml
Isomerize answered 16/8, 2020 at 8:41 Comment(1)
It is a question about native development and people put the line there with an intention.Phoneme

© 2022 - 2024 — McMap. All rights reserved.