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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With