Thursday, August 21, 2014

Android R.Java file disappeared, not generated or other common issues

5:28 AM


What is R file?

It is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. If you create any component in the activity_main.xml file, id for the corresponding component is automatically created in this file. This id can be used in the activity source file to perform any action on the component.
Note: If you delete R.jar file, android creates it automatically.

How R.java file will be created ?

In R.java file, each resource category will be created as one class. In each resource class all respective elements will be created as static members, means constants. So, we should not change these values. All these are final members also, so we must access them with their class names, like R.drawable.ic_launcher, R.layout.main, etc. See below codes to know how classes will be created for each resource category.

public final class R {  

    public static final class drawable {  
        public static final int ic_launcher=0x7f020000;  
   }  
    public static final class id {  
        public static final int menu_settings=0x7f070000;  
    }  
    public static final class layout {  
        public static final int activity_main=0x7f030000;  
    }  
    public static final class menu {  
        public static final int activity_main=0x7f060000;  
    }  
    public static final class string {  
        public static final int app_name=0x7f040000;  
        public static final int hello_world=0x7f040001;  
        public static final int menu_settings=0x7f040002;  
    }  


How many classes existed with name R in an android application ?

There are 2 classes available in each android application with name R.
1. First class is part of android core system or android SDK, which can be accessed as android.R .We can see this class in R.class file which is available in android.jar file, which is automatically included in our project by ADT plugin.
2. Second class is part of our application, which can be accessed as yourpackagename.R (yourpackagename is like com.example.semicolonmagazine). If there is support library attached in our project then , we can see two R.java file (one is support library R.java and another is our project R.java) which is available in directory gen. This R.java file is visible, only after successful build of your project.


Fig 1                                                                       Fig 2

Problems related to  R file?
·         Errors in res folder (e.g String.xml, menu.xml, other xml files within res folder)
·         May be problem in build target
·         problem in android sdk

Solution For R file not generating?

·         Fix all errors in your XML files
·         Run Project -> Clean (This will delete and regenerate R and BuildConfig)
·         Make sure Project -> Build Automatically is ticked. If not, build it manually via
Menu -> Project -> Build  Project .
·         Wait a few seconds for the errors to disappear.
·         If it doesn't work, delete everything inside the /gen/ folder
·         If it still doesn't work, try right-clicking your project -> Android Tools -> Fix Project Properties.
·         If again doesn’t work, Right-click your project > properties > Android. Look at the Project Build Target and Library sections on the right side of the page. -Your Build Target should match the target in your AndroidManifest.xml. So if it's set to target 17 in AndroidManifest, make sure that the -Target Name is Android 4.2.

·         If it still doesn't work , Make sure all the "import android.R" was removed - Clean again (if this doesn't fix it, restart eclipse and try again)

Hey Google, Android on Eclipse needs more love ! Please fix these minor issues.

Written by

We are passionate programmers. Enjoying the rich platforms of Semicolon Family of Programming languages we are proud to call ourselves Semicolon Developers.

0 comments:

Post a Comment

 

© 2013 Echo "Semicolon Developers"; Kathmandu. All rights resevered @ Semicolon Developers Network Pvt. Ltd.. Designed by Templateism

Back To Top