skip to Main Content

OK, so I have a module called "Gridbox" which is a layout widget I wrote a very long time ago.

Within Gridbox is the file res/values/gridboxAttrs.xml which contains (abridged):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="Gridbox_Layout">
    <attr name="layout_weightx" format="float" />
    <attr name="layout_weighty" format="float" />
 </declare-styleable>
</resources>

In my application module I have res/values/styles.xml which references resources defined above:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- Style for a normal button -->
  <style name="baseButton">
    <item name="android:gravity">center</item>
    <item name="layout_weightx">1</item>
    <item name="layout_weighty">1</item>
    <item name="android:layout_margin">1dip</item>
  </style>
</resources>

This built just fine under Eclipse, but Android Studio gives me

ERROR: /Users/falk/MyProject/MyApp/build/intermediates/packaged_res/debug/packageDebugResources/values/values.xml:372:5-384:11: AAPT: error: style attribute 'attr/layout_weightx (aka org.efalk.myapp.test:attr/layout_weightx)' not found.

I’m guessing I need to do something to get Android Studio to know that the module "MyApp" depends on module "Gridbox". My MyApp/build.gradle file does end with

dependencies {
    implementation project(':Gridbox')
}

For completeness, here is settings.gradle:

pluginManagement {
    repositories {
        google {
            content {
                includeGroupByRegex("com\.android.*")
                includeGroupByRegex("com\.google.*")
                includeGroupByRegex("androidx.*")
            }
        }
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "MyProject"
include ':Gridbox'
include ':MyApp'

I have no idea what else I should be doing.

3

Answers


  1. Not sure about your SDK version, but probably adding a module-info.java file in MyApp help? i.e. to specify the dependency on Gridbox:

    something like this:

    module MyApp {
       requires Gridbox;
    }
    
    Login or Signup to reply.
  2. You are probably using AGP 7+ which requires "namespace" defined in build.gradle of Gridbox. In older versions of AGP namespace would be taken from the package defined in AndroidManifest.xml of the sdk.

    Solution 1

    android {
        namespace 'org.efalk.gridbox' // new line
        compileSdk 31
        ..
    }
    

    Add above line in build.gradle

    Solution2

    Lower AGP version in main project.

    Login or Signup to reply.
  3. The error message you’re encountering in Android Studio indicates that it can’t find the layout_weightx attribute defined in your Gridbox module. There are a couple of things to check:

    1. Namespace Mismatch:

    The error message shows aka org.efalk.myapp.test:attr/layout_weightx. This suggests a namespace issue. Double-check the following:

    • In gridboxAttrs.xml, ensure the declare-styleable element has the attribute xmlns:android="http://schemas.android.com/apk/res/android" declared at the top level. This ensures the attributes are defined within the standard Android namespace.
    • In styles.xml, remove the prefix org.efalk.myapp.test: from layout_weightx. You shouldn’t need to specify the application or test package when referencing attributes defined within your own modules.
    1. Gradle Sync:

    While your build.gradle seems correct, ensure Gradle has properly synced the dependencies. Sometimes, a clean build can help resolve issues:

    • In Android Studio, go to Build -> Clean Project.
    • Then, rebuild your project.
    1. Circular Dependencies:

    Make sure there aren’t any circular dependencies between Gridbox and MyApp. If both modules depend on each other in a circular fashion, it can cause build issues. Review your dependencies in both build.gradle files.

    Additional Tips:

    • Check if you have any typos in the attribute name (layout_weightx) between gridboxAttrs.xml and styles.xml.
    • Try invalidating caches and restarting Android Studio.

    If the issue persists after trying these suggestions, consider providing the complete gridboxAttrs.xml and styles.xml files for further analysis.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search