0

I know this question has been asked before but I am at my witts end with this. I have just created a new project and getting this error.

Ofcourse the R.java file is not being generated.

I am using JRE7 android 4.2.2

    package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

As we can see no R import in the mainactivity class

Lets take a look at the untouched manifest file:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myfirstapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

nothing there that looks out of place. I have tried multiple close project open project. clean and builds.

None of the res are in uppercase and I have experimented with the project properties java build path order and export. Under libaries I have 3 different android libaries:

android 4.2.2 android Private Libaries android dependencies

THis is making me start to long for C++ :)

1
  • Its not necessary to have an R file import if your package name and folder are same. Make sure gen folder contains a folder with your package name and R.java in it. Commented Aug 2, 2013 at 20:54

6 Answers 6

3

This just worked for me.

Added a character to the manifest and saved. Deleted said character cleaned projected and package created under gen with buildconfig and R java in it.

Why is worked I have no idea.

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

2 Comments

Wow. That sure sounds like an Eclipse problem. Glad you found a fix; I'll keep this in mind for the next time I see your problem posted on SO. :)
Yeah defo some eclipse build issue. I'd love to know what it is doing under the hood that meant touching the file cause it to generate the classes
2

If you have any errors in your resource files, the tools will not generate an R.java file. Look for and fix all errors in the .xml files before worrying about class com.example.myfirstapp.R not being defined. If you have no errors in the .xml files, then clean and rebuild your project.

Also, just to rule out a programming environment problem if you're using Eclipse, right-click on the project name in the Package Explorer, navigate to Android Tools and select "Fix Project Properties".

5 Comments

in a brand new project does that automatically have this error, I have yet to write a line of code or touch the xml. I have created a new froyo project and not touched it
@Will - Sounds like you might have a configuration problem. Are you using Eclipse? Also, open the Problems view (Window -> Show View -> Problems) to see if there are any clues there. Make sure that you are using the latest tools: check for software updates and run the Android SDK Manager.
Yeah using eclipse SDK 4.2.2 Android ADT & Android Development Toolkit Version: 22.0.1.v201305230001--685705. The problem view just tells me the two references to R in the main cannot be resolved
Description Resource Path Location Type R cannot be resolved to a variable MainActivity.java /MyFirstApp/src/com/example/myfirstapp line 12 Java Problem
@Will - Weird. Try recreating the project from scratch. Is this the first Android project you've tried to create? If so, then I suspect some kind of configuration problem. (A side note: you're not totally up to date, though. The SDK Tools are up to 22.0.5. That shouldn't be causing this, though.)
0

Had this happen a while back, my sdk/build-tools folder was missing a build target for some reason. Check in there to see if you have any folders.

Comments

0

make sure there is no error in resource file, then as soon as you build your project you will get R.java generated, point to note here is no matter whether your java code does not compile, R.java will generate if there is no problem with resource file.

If this step succeed you can import all necessary R.java file. Note that there may be multiple R.java file if you have included library project in your project. All R.java files will be in respective packages. for eg. I have "com.actionsherlock.R.java" & "com.myproject.R.java"

in this case above case 1st import is must com.actionsherlock.R. However, I you no library project included, you need not import R.java as this file would be generate in the same package.

1 Comment

You don't need an import statement for classes in the same package.
0

In my case I am using java 1.6 and it is not supporting android 5.0 so removed those library files in android sdk, then clean the project it is working fine.

Comments

-1

Follow simple following steps

  • Open Android SDK Manager
  • Install Android SDK build-tool (In Tools)
  • Restart Eclipse

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.