Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Including multiple Java Packages to Manifest

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.)

like image 461
HappyGeisha Avatar asked Oct 14 '10 16:10

HappyGeisha


People also ask

Can Java have multiple packages?

No, it cannot be. But however there can be classes with same name in different packages, but no two similarly named classes in the same package.

How many manifest file will be there in an Android application?

Your APK or Android App Bundle file can contain just one AndroidManifest. xml file, but your Android Studio project may contain several—provided by the main source set, build variants, and imported libraries.

How many manifest file can be in an application?

A manifest can contain only one application node. It uses attributes to specify the metadata for your application (including its title, icon, and theme).


2 Answers

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> 
like image 64
Cheryl Simon Avatar answered Sep 23 '22 16:09

Cheryl Simon


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.

like image 30
beekeeper Avatar answered Sep 21 '22 16:09

beekeeper