8

I have created a simple Android Library application with just 1 activity containing a TextView. It works fine, but when I mark it as a library and reference in another application, it is giving errors when I am trying to get the Text View using findViewById(R.id.welcome_textview).

It generated R.java, but in the second app where I am referencing the library, it does not contain the id field. Here are the both R.java files that are getting generated: -

Library Application

    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.mylibrary;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int welcome_textview=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

Same file in the other Application

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.mylibrary;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

Where am I going wrong?

2
  • in second project you have not declare welcome_textview in xml file Commented Sep 12, 2011 at 6:54
  • that's the whole point that I should not have to declare it again... I am try to do something like this github.com/donnfelker/FullAndLiteVersionSharedLibrary Commented Sep 12, 2011 at 7:32

9 Answers 9

10

After hours of trying a lot of stuff, finally found the problem. In the referenced project, there was the Layout folder that was created automatically which was empty. Deleting that folder fixed the issue. Hope this helps someone.

Sign up to request clarification or add additional context in comments.

3 Comments

My situation was a little different, but this pointed me in the right direction. For me, the referencing project had a file in its layout directory with the same name as one in the referenced library. Strangely, this had the effect of causing some, but not all, of the IDs in the referenced library to be unavailable. Removing the file and rebuilding fixed the issue.
@BrianRak - my hero! I had the same issue, and ProGuard's output was totally unhelpful.
How crazy! Saved me a ton!
8

And one more possible cause based on my own painful experience: having XML layout files with the same name in the library res/layout and the app res/layout folders.

2 Comments

Wow, I was trying every option and finally after searching for 1 day got your solution and its working now..Many thanks and god bless you..
Pheww!! Spent hours to resolve this, finally did it after your answer! Thanks a bunch & God bless you!
3

The Main Reason exists behind not formation of R file is because of Xml rendering, the number of dependencies you used in library make that much number of R File in build, So

Open your each xml file and view in preview tab whether it is rendering properly or not

if not resolve it would automatically generate All the R Files.

Comments

2

It seems that having the same style defined twice can cause this error.

In the styles.xml, I had

   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

I just changed the name to AppThemMyLibrary and the R file got generated. Not sure if it was really the cause of the issue.

Comments

1

First make sure your Application package is different from Android Library package, because from your example they are identical: com.example.mylibrary.

One more thing to be aware about is that both Application's R.java file and Android Library R.java files are identical when library is compiled in context of application. In other words, when you compile application, all referenced libraries are compiled as if they were physically included application. The only difference is that library code has different package name for its R.java class.

3 Comments

Tks for the reply. The application packages are actually different - com.example.mylibrary and com.example.myapplication. After adding the application library, it created a pkg - com.example.mylibrary inside gen and generated the R.java file there.
Yes, that's an expected behavior. It should've generated two identical files as the union of application resources and library resources.
I had a library package with the same package name as my main package name. And ant didn't generate R.id.* for the main package resources. I simply changed the package name of the library package (and referencing R.id in it) and ant compiled fine.
1

Troubleshoot#1

Mistakenly I wrote

compile project(':myLibProject')

in my application build.gradle file.

Whereas the correct code is,

compile project(':library:myLibProject')

because the library project myLibProject is inside library directory.

However, because of the wrong code, an empty directory has been created in my application's root folder named myLibProject (empty folder)

Please check for such empty folder in your application's directory manually (using folder navigation software of your OS, not from Android Studio).

Troubleshoot#2

if you are using gradle wrapper in your project level build.gradle file, like this

task wrapper(type: Wrapper) {
    gradleVersion = '2.4'
}

try to comment it out.

Troubleshoot#3

comment out the erroneous line for which the build is failing.

Now make a successful build.

un-comment the line.

try to build again.

Finally

after any troubleshooting, follow these steps.

  1. Clean project (Build -> Clean Project)
  2. Rebuild project (Build -> Rebuild Project)
  3. Invalidate Caches and Restart (File -> Invalidate Caches and Restart)

Comments

0

I also got the problem, that the R file was not generated properly. In my case the problem had been the "icon.png" files located in the drawable folders of the library project. After removing all drawable folders everything compiled.

1 Comment

do you know why having the icon.png files caused a problem?
0

So I also added in Eclipse reference to my own Library Project, and after this, R.java in main project stopped generating, so errors arrived (I could see them in Console tab).

they were like this:

[2013-01-13 00:39:18 - AppName] [Path]\res\values-v11\styles.xml:7: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light'.

In my case it was enough to delete in Library Project folders like

  • "res/values-v11"
  • "res/values-v14"

specially, that my Library Project didn't use them, and everything came back to normal: immediately in main project R.Java auto regenerated itself both for com.example.app and com.example.lib, so there were 2 different R.java and they didn't disturb each other.

Comments

0

If your android library can not resolve R class, this means that maybe somewhere inside your library XML files you have some duplications with the main app module. Could be color names, string names, style names, layout names and so on. Just check all you library xml resource files and folders and make sure that nothing has same name like the main app module. After that maybe project will need one clean and everything should be fine with R import.

Note: Check also the library Manifest file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.