1

Github link: https://github.com/iamdangerous/Test-Library

I am creating a library(name: myLibrary) in which I have added a style item for cardView like below

<resources
xmlns:app="http://schemas.android.com/apk/res-auto">

<style name="CardLayout">
    <item name="cardElevation">@dimen/test_number</item>
    <item name="cardBackgroundColor">@color/red</item>
    <item name="cardCornerRadius">@dimen/test_number</item>
</style >

And I am integrating this library as an AAR file. The library is building perfectly. But when I integrate this library as a module I am getting error like this Style issue

I am again repeating you will get this error on integrating as an AAR. Any help is appreciated.

CardLayout is not any class or view it is just a name that I am using to refer this style. Please don't get confused. And I am using this CardLayout style as below in my the library module

<android.support.v7.widget.CardView
    style="@style/CardLayout"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

enter image description here

Rahul Lohra
  • 824
  • 1
  • 10
  • 21
  • Possible duplicate of [Can't find resource identifiers from AAR in XML](https://stackoverflow.com/questions/28533067/cant-find-resource-identifiers-from-aar-in-xml) – Marcos Vasconcelos Oct 11 '17 at 14:12

1 Answers1

1

The issue is that you have defined the values for the attributes but haven't defined the attributes itself.

You need to add attrs.xml under your res folder with below code

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <attr name="myCardElevation" format="reference|dimension" />
  <attr name="myCardBackgroundColor" format="reference|color" />
  <attr name="myCardCornerRadius" format="reference|dimension" />
</resources>

You can see I updated the attribute keys because the ones you used were conflicting with the ones provided by CardView

Your styles.xml would look like

 <style name="CardLayout">
    <item name="myCardElevation">@dimen/test_number</item>
    <item name="myCardBackgroundColor">@color/red</item>
    <item name="myCardCornerRadius">@dimen/test_number</item>
 </style>

I would also fix the project, because it is doing a bunch of weird stuff such as packaging an Activity in a library and depending on Butterknife and Constraint Layout deps.

If however you are trying to override the attributes provided by CardView itself, then you wouldn't need to define attrs.xml but rather just add a parent attribute to your style tag

 <style name="CardLayout" parent="CardView">
     <item name="cardElevation">@dimen/test_number</item>
     <item name="cardBackgroundColor">@color/red</item>
     <item name="cardCornerRadius">@dimen/test_number</item>
 </style>

Update: with more discussion with OP it is more clear as to why the overriding doesnot work for him. Since the AAR doesnot bundle build.gradle file, the dependency of cardview isnt available when OP includes an AAR in his app.

2 solutions are possible here,

  1. app module could include cardview in its build.gradle so that its available when the library compiles from AAR

    repositories{ flatDir{ dirs 'libs' } }

    dependencies { // This is required because aar doesnot bundle build.gradle i.e the cardview lib needs to be present to be overriden by mylib compile 'com.android.support:cardview-v7:26.+' compile(name:'mylibrary-debug', ext:'aar') }

  2. OP could publish it to some maven repository hosting service such as JitPack, JCenter or Maven central which would include a POM.xml file along with the aar file and which defines the dependencies of the aar file. That way app module doesnot need to declare the cardview dependencies, instead that would be downloaded transitively.

Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35