25

I'm attemping to build an Android project with Gradle from command line, but found a problem when I want to change the directory structure.

Currently is like this:

.
└── main
    ├── AndroidManifest.xml
    ├── ic_launcher-web.png
    ├── java
    │   └── com 
    │       └── myproject
    │           └── MainActivity.java
    └── res 
        ├── ... 
        ├── layout
        │   ├── activity_main.xml
        │   └── fragment_main.xml
        ├── ... 
        ...

Then I execute:

./gradlew clean build

That ends with:

BUILD SUCCESSFUL

Ok. All fine. But now I want to create a new directory, so:

I create an ui directory and move MainActivity.java there:

.
└── main
    ├── AndroidManifest.xml
    ├── ic_launcher-web.png
    ├── java
    │   └── com
    │       └── myproject
    │           └── ui
    │               └── MainActivity.java
    └── res
        ├── ...
        ├── layout
        │   ├── activity_main.xml
        │   └── fragment_main.xml
        ├── ...
        ...

Modify its package:

package com.myproject.ui;

// imports

public class MainActivity extends ActionBarActivity {
    ...
}

Modify its android:name attribute in AndroidManifest.xml:

<activity 
    android:name=".ui.MainActivity" 
    android:label="@string/app_name" >
    ...
</activity>

And try to compile it again:

./gradlew clean build

With following errors:

/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:19: error: package R does not exist
        setContentView(R.layout.activity_main);
                        ^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:23: error: package R does not exist
                    .add(R.id.container, new PlaceholderFragment())
                          ^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:33: error: package R does not exist
        getMenuInflater().inflate(R.menu.main, menu);
                                   ^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:43: error: package R does not exist
        if (id == R.id.action_settings) {
                   ^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:60: error: package R does not exist
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

BUILD FAILED

What am I doing wrong? Any ideas?

Thank you.

1
  • 1
    Press Alt+Enter on Class name if using Android Studio. It just imports the relevant R class itself. May 4, 2015 at 11:51

4 Answers 4

56

This usually happens when you are declaring the wrong package in your Activity.

Make sure the package com.example.blah; declaration in your Activity matches the package declaration in your AndroidManifest.xml.

1
  • This is indeed sorted out my issue regarding R.layout and R.id . . My AndroidManifest package is named as com.test.app and my java packages are named as com.native.testing.app . . to sort it out i changed my java packages to com.test.app and it worked. . :) Sep 26, 2017 at 5:27
30

I got it, so I will answer myself.

I had to declare the R class in those activities, fragments or whatever classes that use any resource defined there.

So, it would be like:

package com.myproject.ui;

// lots of imports...
// ...
import com.myproject.R;

public class MainActivity extends ActionBarActivity {
    ...
}
5
  • A Thousand pluses, I don't know whether this is the right way but solved my prob.
    – Satheesh
    Nov 24, 2014 at 14:06
  • Is there any way to avoid doing this for each class file? Jul 10, 2015 at 15:28
  • 1
    @dave_gerard: I don't know of any way, but now I use Android Studio that handle those imports by itself, so I don't worry about it.
    – Birei
    Jul 10, 2015 at 16:07
  • This worked for me but I actually didn't have to type the import statements out..just place the cursor at the end of the last import and hit enter and it sort of refreshes the "auto import" functionality and auto imports it for you..definitely plus one!! Aug 16, 2016 at 21:39
  • Just way, android studio doesn't handle it. It needs to be included. Had the same problem
    – Sarker
    Oct 2, 2016 at 21:44
10

I got this error because I changed the package name in the .java files and renamed the directories but didn't update AndroidManifest.xml. Changing the package name in AndroidManifest.xml fixed the problem. This is consistent with loopj's answer above.

The complete procedure for changing the Java package name in an Android project that worked for me is the following:

  1. Renamed package in .java files.
  2. Renamed directories containing .java files.
  3. Renamed package in AndroidManifest.xml, as well as other items in that file as appropriate.
  4. Renamed applicationId in build.gradle (the one for the app).
2
  • Thank you, I did refactor but for some reason it left the manifest unaltered Jul 17, 2016 at 18:25
  • :-) thought I addressed this, but got the capitalization wrong
    – Ben
    Jul 25, 2019 at 23:37
3

I had this same problem. The solution is to either highlight and right click the R and 'import class' or highlight R and press alt+Enter and then click import class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.