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.)
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>
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.
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;
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.
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.
Related
How can I change the entire package path in Android Studio, so that also the import statements within Java classes and xml files will be changed automatically.
I know there are threads already posted about this topic, but whenever I change package path, I have to adapt all import statements by hand. And it's much work when there are over 100 classes :(
If you want to move your Java classes to different package, in Android Studio you can right click on package name and then select Refactor->Move.
I have a project that is formed by a project and a library project. Both of them have their own manifest.
The library project has all the acitivities definitions (and practically all the code). The app project has a manifest where only an Application class is defined. (If you ask why i have this structure is because i have a common library and i create multiple app projects to change themes and api calls)
My question is, when i have to define my initial activity class name in platforms, i.e Facebook, should i use my library project package name as it is defined in its manifest? or gradle merges it with my app project package name?
Example:
com.app.finalproject is my application package name.
com.commonlibrary.ui.SplashActivity is my common package name of my initial activity and it is defined in my common library project manifest.
So, after gradle merges the two manifest what is the package name of my SplashActivity ? is it still com.commonlibrary.ui.SplashActivity o is it something like com.app.finalproject.ui.SplashActivity ?
Thanks in advance.
Activities do not have a package name, strictly speaking.
The Java class that implements an activity will be in a Java package. That package is denoted by the package line at the top of the class file, along with the directory in which the Java class resides. None of that is affected by the manifest merger process.
Sometimes, you need a full ComponentName of an activity or other component. In that case, the package portion of the ComponentName will be the application ID. The Java class portion of the ComponentName will still use its regular Java package.
Using your example, if I am understanding it correctly, the activity is still com.commonlibrary.ui.SplashActivity. The ComponentName for that activity would be com.app.finalproject/com.commonlibrary.ui.SplashActivity.
I have an application that supports different domains. My code is developed under the package: com.example. I would like to publish multiple application under different packages like:
com.example.domain1, com.example.domain2, etc.
In the manifest I define:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.domain1"
....
and for domain2:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.domain2"
The problem I am facing is that once I change the base package name from domain1 to domain2 I need to rename all my packages in the src folder as well as layouts. What I am looking for is to keep the same base packaging com.example and distribute the different apps under its sub-domains.
Is there a better way?
Clarification:
When changing the package name, the resources file changes from com.example.R to com.example.domain2.R. This means that I have to go into all src java classes and layouts etc. and update the generated R file location. That is not handy.
I ended up marking my main project as a library and then creating a project for each domain and linking to the library.
No need to play around with the manifest or the R.java file.
Thanks to #Tenfour04 for pointing me in the right direction!
In your manifest, each Activity, Service, Provider and Receiver has a name attribute. By default they use a shortcut like ".MainActivity" The leading . is a shortcut for the package name at the top level of the manifest.
So if you don't want to rename all those packages in the src folder, just type out explicit names for the Activities, Services, Providers, and Receivers in your manifest, for example, android:name="com.example.sharedmultiprojectdomainname.MainActivity".
If you are using Gradle as your build system, you might want to incorporate build variants into your build, which will solve package problems. More on this can be found in this Google I/O speech
Hope this helps.
i have an android project which has a lot of classes I've built.
i want to organize it to sub folders and my package in eclipse.
when i'm adding a folder and move files over there, what changes should i do in the code so the file which need this classes can reference to it?
is it something in the:
import android.app_name.folder_name.class_name
help will be appreciated, my project is beginning to be a big mass..
if its not possible i will be glad to hear about other solution
Eclipse has a rename/refactor method you can use. The only issue I'm familiar with is the fact that it seems to break the manifest.xml
Yes you can import project into Eclipse...
Then you should create a Package to manage related classes. (FYI, package is ultimately creates folder hierarchy on your drive).
Note: Make sure you provide a correct reference of the particular class in AndroidManifest.xml file.
I'm trying to run the in app billing sample code. I try to change the package name from com.example.dungeons to com.mydomain.dungeons. I change the package name by right clicking on the project name, then Android Tools ==> Rename Application Package. After I enter the new package name, I get a bunch of warnings because it adds an import to every class file like so:
import com.mydomain.dungeons.R;
What does this line mean? Also, I notice the package declaration for each class is still:
package com.example.dungeons
You just told the Android tools that your package changed. This caused the ADT to re-generate the R class with the new package declaration and changed the package in AndroidManifest.xml; it also updated all references to the R class in your code for you.
You still have to actually rename the package declarations in your code, preferably by using an Eclipse refactoring (right click on the package, etc.).
eclipse import your ID of elements.
and you have standard android elements(layout, Widgets etc)
http://developer.android.com/reference/android/R.html
eclipse create R file, when you you clean project (project>clean)