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.
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
.
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 {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With