Dong Wang
Dong Wang

Reputation: 344

Android Studio library modules cant generate R.java

I write a library module that other module can depend on. I create a library module and write some code in it. This is a Custom View. I create attr.xml like this.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="top_icon" format="reference" />
    <attr name="bottom_icon" format="reference" />

    <declare-styleable name="GradientIconView">
        <attr name="top_icon" />
        <attr name="bottom_icon" />
    </declare-styleable>

    <attr name="text" format="string" />
    <attr name="text_size" format="dimension" />
    <attr name="top_text_color" format="color" />
    <attr name="bottom_text_color" format="color" />

    <declare-styleable name="GradientTextView">
        <attr name="text" />
        <attr name="text_size" />
        <attr name="top_text_color" />
        <attr name="bottom_text_color" />
    </declare-styleable>
</resources>

I write some code like this.

TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.GradientIconView);

    BitmapDrawable drawable;

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {

        int attr = a.getIndex(i);
        switch (attr) {
            case R.styleable.GradientIconView_top_icon:
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setTopIconView(drawable);
                break;
            case R.styleable.GradientIconView_bottom_icon:
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setBottomIconView(drawable);
                break;

        }
    }

It says cannot resolve symbol 'R', i rebuild the project and make the library module. It doesnt work. Here is the project structure. Structure about project

Upvotes: 1

Views: 1202

Answers (2)

Dong Wang
Dong Wang

Reputation: 344

I find what the problem is. It's not the R.java not generated. It just the R.styleable.*** cannot be used in a switch statement in Android library modules.

As of ADT 14, resource constants in library projects are no longer final. So i change my code like this. It work well.

TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.GradientIconView);

    BitmapDrawable drawable;

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {

        int attr = a.getIndex(i);
        if (attr == R.styleable.GradientIconView_top_icon) {
            drawable = (BitmapDrawable) a.getDrawable(attr);
            setTopIconView(drawable);
            break;
        }
        else if(attr == R.styleable.GradientIconView_bottom_icon) {
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setBottomIconView(drawable);
                break;
        }
    }

    a.recycle();

Finally, i solve it by myself :)

Upvotes: 1

Sinh Phan
Sinh Phan

Reputation: 1266

Try this: -Delete folder YourApp/build, YourApp/app/build and all file .iml -Restart android studio and clean-rebuild Hope It help you

Upvotes: 0

Related Questions