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.

Birei
  • 35,723
  • 2
  • 77
  • 82

4 Answers4

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.

loopj
  • 1,599
  • 1
  • 15
  • 12
  • 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. . :) – Website Is Fun Sep 26 '17 at 05: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 {
    ...
}
Birei
  • 35,723
  • 2
  • 77
  • 82
  • A Thousand pluses, I don't know whether this is the right way but solved my prob. – Satheesh Nov 24 '14 at 14:06
  • Is there any way to avoid doing this for each class file? – davegallant Jul 10 '15 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 '15 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!! – Isaac Martin Otim Aug 16 '16 at 21:39
  • Just way, android studio doesn't handle it. It needs to be included. Had the same problem – Sarker Oct 02 '16 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).
stevehs17
  • 1,466
  • 2
  • 14
  • 19
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