Access aar style \ theme resouces compileOnly - android

I build a custom aar library with some resouces, included a common and reusable theme in themes.xml.
<style name="Theme.MyThemeLibrary" parent="Theme.Material3.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/clr_primary</item>
<item name="colorPrimaryVariant">#color/clr_primary</item>
<item name="colorOnPrimary">#color/clr_on_primary</item>
...
<style name="Theme.MyThemeLibrary.TextInputLayout" parent="Widget.Material3.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/clr_osc</item>
<item name="startIconTint">#color/clr_tilc</item>
<item name="endIconDrawable">#color/clr_tilc</item>
<item name="android:textColorHint">#color/clr_tilc</item>
<item name="helperTextTextColor">#color/clr_tilc</item>
</style>
<style name="Theme.MyThemeLibrary.Button" parent="Widget.Material3.Button">
<item name="cornerRadius">15dp</item>
</style>
...
I want to use it in another library, so I can't use implementation, but only compileOnly.
compileOnly files('libs/MyThemeLibrary.aar')
I can access correctly to all R resouces like string or color, but not Styles.
Building project give me error in layout files e.g.
AAPT: error: resource style/Style.MyThemeLibrary.Button (aka
com.example.mysecondslibrary:style/Style.MyThemeLibrary.Button) not
found.

Related

error: style attribute 'attr/colorPrimary' not found

Recently I updated my target SDK to 29 as per new Android guidelines but after that I received this error:
D:\Android Apps\PatherPane\app\src\main\res\values\styles.xml:4:5-9:13: AAPT: error: style attribute 'attr/colorPrimary (aka com.patherpane.app:attr/colorPrimary)' not found.
And my style XML file is:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">
#drawable/background_splashscreen
</item>
</style>
How can I fix this?
This command followed by item names is showing the error: Theme.AppCompat.Light.DarkActionBar
Make sure you have not missed appcompat implementation in build.gradle(app)
implementation ‘com.android.support:appcompat-v7:28.0.0’
or
implementation 'androidx.appcompat:appcompat:1.2.0'
if using androidx
This error commonly shows up when
androidx.appcompat:appcompat
and
com.google.android.material:material
versions do not match. Check whether you updated one and not the other
Had this in a library module, which suddenly stopped resolving the theme.
dependencies {
implementation "com.google.android.material:material:1.3.0"
}
Go to app/src/main/res/values/colors.xml
It should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color> //This is where the colorPrimary is defined
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
Make sure that the line that I commented exists, has correct syntax, and defines the appropriate 6-digit hexcode in the middle. The hexcode should be the primary color of the theme you are designing.

android studio duplicate resources

I'm getting these duplicate resources errors.
[color/colorAccent] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\strings.xml [color/colorAccent] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\colors.xml: Error: Duplicate resources
[color/colorPrimary] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\strings.xml [color/colorPrimary] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\colors.xml: Error: Duplicate resources
[color/colorPrimaryDark] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\strings.xml [color/colorPrimaryDark] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\colors.xml: Error: Duplicate resources
[style/AppTheme] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\strings.xml [style/AppTheme] C:\Users\haide\Documents\androidProjects\SkyrimGenerator\SkyrimScenarioCharacterGenerator\app\src\main\res\values\styles.xml: Error: Duplicate resources
Here is my colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Here is my styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
and strings.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
This happened after I changed my strings.xml invalidated cache and restarted because it wouldn't update.
I tried multiple permutations of having just one apptheme and one color in a single file, but I get compilation errors. Say I delete apptheme from strings.xml and colors from color.xml, I'll get compilation error. If I have any two duplicates it'll give me duplicate error...
Does anyone know how to fix the error?
I had same issue once... I noticed that i had declared the variable colorPrimary in the app level build.gradle file and also in colors.xml. I fixed the error by removing the resource value in the build.gradle file
I had this in my app level build.gradle
defaultConfig {
...
resValue 'color', "colorPrimary", "#2196F3"
}
And this in my colors.xml
<color name="colorPrimary">#C41426</color>
I removed one of them problem solved

Unable to delete xml file from app/build folder

I tried to add a background for a theme (ActionBar),
but then everything went wrong.
code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat">
<item name="android:background">"colorPrimary"</item>
</style>
<style name="Theme.AppCompat.DayNight.DarkActionBar" parent="Theme.AppCompat"/>
<style name="Theme.AppCompat.DayNight.Dialog" parent="Theme.AppCompat.Dialog"/>
<style name="Theme.AppCompat.DayNight.Dialog.Alert" parent="Theme.AppCompat.Dialog.Alert"/>
<style name="Theme.AppCompat.DayNight.Dialog.MinWidth" parent="Theme.AppCompat.Dialog.MinWidth"/>
<style name="Theme.AppCompat.DayNight.DialogWhenLarge" parent="Theme.AppCompat.DialogWhenLarge"/>
<style name="Theme.AppCompat.DayNight.NoActionBar" parent="Theme.AppCompat.NoActionBar"/>
</resources>
and I got this message:
Error:(4, 41) String types not allowed (at 'android:background' with value '"colorPrimary"').
decided to revert my code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat"/>
<style name="Theme.AppCompat.DayNight.DarkActionBar" parent="Theme.AppCompat"/>
<style name="Theme.AppCompat.DayNight.Dialog" parent="Theme.AppCompat.Dialog"/>
<style name="Theme.AppCompat.DayNight.Dialog.Alert" parent="Theme.AppCompat.Dialog.Alert"/>
<style name="Theme.AppCompat.DayNight.Dialog.MinWidth" parent="Theme.AppCompat.Dialog.MinWidth"/>
<style name="Theme.AppCompat.DayNight.DialogWhenLarge" parent="Theme.AppCompat.DialogWhenLarge"/>
<style name="Theme.AppCompat.DayNight.NoActionBar" parent="Theme.AppCompat.NoActionBar"/>
</resources>
but then it showed the same error,and the XML file is not really edited.
tried some solutions such as:
1.clean project (didn't work)
2.re-build project (didn't work)
3.delete app/build folder and run AS again (didn't work)
4.run AS as administrator (didn't work)
5.close Android Studio and run again (didn't work)
6.delete only the res folder (didn't work)
everything went wrong.
directory: app\build\intermediates\res\merged\debug\values-night-v8\values-night-v8.xml
Any solutions?
you forgot to add the #color to your background tag :
just change your
<item name="android:background">"colorPrimary"</item>
line with this
<item name="android:background">#color/colorPrimary</item>
and it saying the truth what you have is just a string ,and android has no way to know you meant the colorPrimary value in color resources.
hope this help.

No resource found that matches the given name: attr 'windowBackground'

As it is said in this doc I am using items in values/styles.xml without "android" namespace and in values-v14/styles.xml with "android" namespace. But when building values/styles.xml it says"No resource found that matches the given name: attr 'windowBackground'."
here is the code for values/styles.xml:
<style name="MyDefaultStyle" parent="#style/Theme.AppCompat.Light">
<item name="windowBackground">#color/list_app_background</item>
</style>
here is the code for values-v14/styles.xml:
<style name="MyDefaultStyle" parent="#style/Theme.AppCompat.Light">
<item name="android:windowBackground">#color/list_app_background</item>
</style>
this is because the windowBackground is from api 21 so u should use it on
values-v21
Just recreate a new project and import the forms and units.

How to put a CardView attribute in a style?

I have:
in a file build.gradle (dependencies)
dependencies {
compile 'com.android.support:cardview-v7:21.0.+'
}
in a file styles.xml (styles definition)
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<style name="CardViewStyle">
<item name="android:layout_margin">5dip</item>
<item name="card_view:cardCornerRadius">4dp</item>
<item name="card_view:cardElevation">4dp</item>
</style>
</resources>
But compiler complains:
Error: No resource found that matches the given name: attr 'card_view:cardCornerRadius'.
How do I add some cardView attributes in a style?
Set parent attribute to CardView.
You don't even have to add xmlns:card_view="http://schemas.android.com/apk/res-auto".
Working snippet of code:
<style name="CardViewStyle" parent="CardView">
<item name="cardCornerRadius">4dp</item>
<item name="cardElevation">4dp</item>
</style>
To add to mklimek's answer, you do not even need to add the parent="CardView"
Just avoid the cardView: prefix
So instead of
<item name="cardView:cardElevation">5dp</item>
just use
<item name="cardElevation">5dp</item>
So my complete style code for this is:
<style name="categoriesCardViewButtons">
<item name="android:layout_width">80dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">5dp</item>
<item name="cardBackgroundColor">#color/grey_ultra_light</item>
<item name="android:paddingBottom">#dimen/l1LayoutPaddingB</item>
</style>
This error can occur if you do not have CardView in your gradle dependencies (in Android Studio) for the module containing your style (even though you have the dependency in the module where you use the card view). In that case, simply add
compile 'com.android.support:cardview-v7:22.2.1'
(or another version) to build.gradle for the module containing themes.xml or styles.xml.
Then, you do not need to set a parent on your style, nor do you need a namespace prefix, but can follow the example in tsik's answer, so all you need is
<style name="myStyle">
<item name="cardCornerRadius">8dp</item>
</style>
This code work well
<style name="MyCardView" parent="CardView">
<item name="cardUseCompatPadding">true</item>
<item name="cardCornerRadius">#dimen/hei_standard_8dp</item>
<item name="cardBackgroundColor">#color/bg_item_list_alphabet</item>
</style>
Note: parent ="CardView"

Categories

Resources