Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Package R does not exist" error when building with Gradle from command line

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.

like image 440
Birei Avatar asked Sep 10 '25 02:09

Birei


2 Answers

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.

like image 91
loopj Avatar answered Sep 12 '25 16:09

loopj


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 {
    ...
}
like image 35
Birei Avatar answered Sep 12 '25 18:09

Birei