Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AAPT: error: attribute android:forceDarkAllowed not found

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

like image 202
6155031 Avatar asked May 16 '19 07:05

6155031


4 Answers

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!

like image 156
Tash Pemhiwa Avatar answered Nov 18 '22 21:11

Tash Pemhiwa


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.

like image 44
svkaka Avatar answered Nov 18 '22 22:11

svkaka


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!

like image 2
Marci Avatar answered Nov 18 '22 22:11

Marci


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>
like image 2
Diljeet Avatar answered Nov 18 '22 22:11

Diljeet