33

The app I am developing has many activities organized into seven java packages. Originally I wrote all the coding and stuff for each group of activities in a java package as different projects.

Now I'm at the point where I want to put all the packages together into one project. When I add a new package to the src folder, I get an error for all my R.id.* values ("R cannot be resolved").

My instinct tells me that there is something fancy I have to put in the project manifest, but I can't find any resource online to tell me how.

(Note: I have read this and this and I still couldn't figure out how to add additional packages to my project.)

Community
  • 1
  • 1
HappyGeisha
  • 565
  • 1
  • 5
  • 11
  • Did you try doing a clean build? And update the include path for your R file in the class you added? If you previously had the activity in a different project, then the R file was probably in a different location as well. – Cheryl Simon Oct 14 '10 at 16:39
  • 2
    I have gone to Project > Clean... half a dozen times hoping it would just work... no luck. – HappyGeisha Oct 14 '10 at 16:47
  • What about the include path for R? Is it correct? – Cheryl Simon Oct 14 '10 at 16:56
  • How would I update the include path for the R file? I have tried disabling "Build Automatically", copy/pasting the contents of the gen folder from the original project into the new one which makes the error go away, but once I do another clean or enable "build automatically" it disappears? Also, how do I add new java packages to the manifest? – HappyGeisha Oct 14 '10 at 16:57
  • Don't do that! You should never edit the R file by hand. By include path I meant the import statement that appears at the top of your activity file. Does it reference the correct R file? Also, are the references you are referencing included in the new project? – Cheryl Simon Oct 14 '10 at 17:12
  • To answer your 2nd question, you don't need to. There is a single package reference which is just a convinience so that you don't have to put the com.example.my.path in every Activity line. Just make that point to whatever the common root of all of your packages is, and then have the android:name of the Activity be the relative path from that point. – Cheryl Simon Oct 14 '10 at 17:13
  • Oh I see. So I imported the R file that already existed with this project and it works now (Thank you !!! [: ) Just to clarify though, it doesn't matter that my activities are separated into different java packages, they can all reference to the same R file? – HappyGeisha Oct 14 '10 at 17:19
  • yes, the R file is unique per project, not per package. – Cheryl Simon Oct 14 '10 at 17:21

5 Answers5

41

Make sure that the import statement at the top of the Activity references the correct R file. Each project has its own R file, so if you copy an Activity from one project to another it will still be trying to reference the R file from the old project.

You do not need any explicit inclusion of different packages in the manifest. To include activities from two different packages, say:

com.example.package1.Activity1
com.example.package2.Activity2

you can do the following:

<manifest package="com.example" . . . >
  <application . . .>
    <activity android:name=".package1.Activity1" . . . />
    <activity android:name=".package2.Activity2" . . . />
  </application>
</manifest>
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • Oh Mayra you have been so helpful!! Thanks again! – HappyGeisha Oct 14 '10 at 17:33
  • Yes the code works but android still gives red error marks (yet will compile and run) – StarWind0 Jul 17 '16 at 02:00
  • I am using APK Builder and all xml resources are declared in an auto generated java file by aapt. Its package name is you guess, the android manifest package name. –  Jul 20 '19 at 17:09
14

Android automatically creates the class named "R" in the package declared in your App's manifest. When all of your classes are inside that package, you'll never have to explicitly import "R". However, if you have classes in other packages, they won't see it by default and you will have to include

 import <app-package>.R;

or

 import <app-package>.*;

(substituting the actual name for <app-package> of course).

If you include library projects in your App, then they can reference their own "R" classes, which will be generated within their home packages. If you have several independent activities which need to be bundled together into one final App, you should seriously consider using library projects instead of manually merging things. It could make life much easier for you.

beekeeper
  • 2,516
  • 17
  • 13
  • 1
    I have tried to use library projects in the past, and it didn't work for me. This is because all my Activities cross-reference each other and once I made a project a library then the referencing became one directional.. I'm a programming noob so this probably didn't make any sense but thanks for your help anyways :) – HappyGeisha Oct 14 '10 at 17:37
  • No, this is a common problem with libraries -- they must be pretty much self-contained, which limits their usefulness. I just thought it might be useful in this case since you said you had developed each activity in isolation, and thus I figured they'd already be self-contained. If they don't make your life easier, there's no percentage in using them. – beekeeper Oct 14 '10 at 18:47
1

The problem may persist even if we change the manifest file. To avoid it, we must add an import com.example.R; in all our classes.

Example:

MainActivity.java in package2

package com.example.package2.Activity2
import com.example.R;

TestActivity.java in package1

package com.example.package1.Activity1
import com.example.R;
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
Omar.LKS
  • 61
  • 4
0

I am using APK Builder and all xml resources are declared in an auto generated java file by aapt. Its package name is you guess, the android manifest package name.

Then it dawned on me that xml files dont have package names. All xml files are just xml files. Then aapt generates an R class with all XML resources in one R class. The package name of this class is the one in the manifest.

So either import package-name.R or just use one package for your entire project.

0

Check that the layout -> main.xml file is correct and includes the android:id="@+id/whateverIdHasCausedYouTheError"

The R.java file will then be updated to include the id.. and bam, your error should disappear.

EdChum
  • 376,765
  • 198
  • 813
  • 562