How to fix package names in IntelliJ? - java

Is it possible to fix all the package names in a given folder in IntelliJ?
If files are manually imported into IntelliJ, and their package names are all wrong (including the sub-folders), how can I mass fix the package names?
This has caused me a lot of grief so far....
moving to a package or renaming a package
This has to be a bug.
Example:
If I am on a package in the project explorer, and rename the package, say it is:
com.example.xml.xml
Now I want to change this (and all the files under it) to:
com.example.xml
I can't seem to do it!
When it says "rename package com.example.xml.xml to":
If I enter "com.example.xml" it won't work. I'm not sure what it is going on, I have spent a over an hour trying this as I have more files to import/fix.

It seems you can't change a full package name like this one: "com.example.xml.xml", but you can change each fragment, for that you can go to "View Options" (a little wheel) in the project view, disable "compact empty middle packages" options, and do something like renaming the second "xml" from the end, so the final package could get into something like:
com
-example
-bar
-xml
Then you can drag and drop all contents in the last "xml" to "bar" package and finally delete the last "xml". In your case it seems you won't need to rename the package just move the contents

In project view change layout to Flatten packages. Then in same menu unselect Hide empty middle packages. Then create the new package you want. And then using drag'n'drop move the root package which contains your code to a newly created package (when the pop-up appears, select the second option, i.e. move everything ...). Delete old, unused packages

Related

Package not detected after renamed in Intellij

I have a project in intellij and when I created my packages I made a mistake because they all start in capital letter, for example my previous package name was "Main" then I changed it to "main" and I did it to all of my packages.
error: package com.myproject.thesis.main does not exist import
com.myproject.thesis.main.Main;
but the "main" package and "Main" class does exist and already imported on the class that needs it. Also what should I name the package where I put my main class?
While it may not help in this case, a general thing to try when "My code compiles and runs, but IDEA is really confused about the symbols in my project" is to go into the "File" menu and choose "Invalidate Caches / Restart". I've rarely used it, and it doesn't always help, but every once in a while it fixes IDEA's confusion.
See also the IntelliJ IDEA help on Cleaning System Cache.
You need to edit configuration. From IntelliJ Run menu choose Edit Configurations. On the configuration window change the package name of Main class.
I hope this helps.
Try changing the packages to something else first. e.g. coma.myprojecta.thesisa.maina and get that to work. Then change it back to com.myproject.thesis.main. I think IntelliJ might be having problems with the change in case. If you change it something totally different and then back it IntelliJ should realise that things have changed.
As for what to call the class with main method, it is up to you. There is some good advice here. https://softwareengineering.stackexchange.com/questions/208062/naming-conventions-for-java-classes-that-have-a-static-main-method
You could also try a complete reset of the project. Create a whole new project, with the packages named (and cased) the way you want. Then, move the code files from your old project into the folders for your new project. IDEA should be able to see the files you put in there and parse them correctly.
Just clean the contents in /target (or "mvn clean"), and Intellij can find your new package correctly.

renamed Main.java but still exists as a Run option in intelliJ

When I created a Java project in intelliJ, it comes with a default Main.java. I then renamed Main.java to some other name, and also changed then class name to the same name.
Now the problem is, Main still exists as a Run option (also in the Configuration) with a red cross on it. I am wondering how to remove it and why it would happen?
Also where does intelliJ really shine? It seems it has some inconveniences here and there to me.
Click on edit configurations which should bring you to a screen like this:
where you can find the bad Main.java configuration and click the minus button. (Shown in gray in the image)

White package icons in my Eclipse's package explorer

I am getting these strange white package icons in my testing project. From what I've seen # http://jamie.ideasasylum.com/images/icons.pdf, they signal "empty packages", but I don't know why.
My src/ folder is set is includedin the build path, so I don't get what the problem might be:
Thanks
This is most likely because you have chosen hierarchical package views. The hierarchical package view will show empty package icons for packages without any classes.
Switching to flat package views will allow Eclipse to display only packages that have classes in them.
Create at least one class in the 'empty' package 'white package' and refresh the project, the color should change to Golden.
When Package does not contain any class, then the color is white.

Refactoring package name breaks entire app

I am trying to refactor my package using Eclipse
org.sheehan.activity
to
org.sheehan.stocks.activity
For some reason my project just blows up when I do this and R doesn't get regenerated. How can I refactor properly?
UPDATE:
I updated my Android Manifest to reflect the change. R still doesn't get regenerated. Even after a clean. The compiler is complaining about org.sheehan.activity.R
I resolved by:
Android Tools > Rename Package Application
If you renamed properly, all your references across your project should have gotten updated. After doing this, clean your project by going to Project > Clean. This should fix it.
Fixing missing reference to R in eclipse:
Refactor -> Rename`
Build -> Clean
Android Tools -> Fix Project Properties
select the base package of your project in the eclipse package explorer, press F2 (Rename), in the rename dialog: SELECT ALL the checkboxes.:
That should do it
The package name doesn't update in the XML files. Specifically your manifest which needs to know the base package.
For more advanced imports and refactors, literally closing the Eclipse IDE can refresh a cache that causes random errors like "End position lies outside document range" or the "what once worked now doesn't hair puller outter errors."
Signs you should close Eclipse when trying to rename things are:
When you import a project, do some refactoring (like trying to change the package name), then delete the project and try again, you will see immediate R.java errors when you didn't before
When you are importing projects that have manually generated R.java files that have custom imports like "com.ns.proj.R" and don't see the name update in Preview
The box that asks if you want to "Rename subpackages" is checked unexpectedly
the code in the "Preview>>" looks as if it's been complied before instead of black and white code with the proper names replaced
you get XML errors in the manifest activity name like this
android:name="com.ns.proj.CustomCocom.ns.proj.CustomControlse="#android:style/Theme.Light.NoTitleBar"
when it should be
android:name="com.ns.proj.CustomControls"
android:style="Theme.Light.NoTitleBar"
There are too many to name obviously, however this is as important as "keep your eye on the ball" in sports. Something that is so easy to forget, yet things like these apply to other software programs like Dreamweaver (e.g. when you use the Dreamweaver "Put" option instead of FTP dragging with FileZilla and you notice your domain isn't updating your changes online). I called Adobe when I first started as a developer 8 years ago because I knew I wasn't doing anything wrong. They said sometimes the generated files to perform the action get corrupted...in other words, IDE's have bugs too!
In general, close the IDE before you off yourself...it could save your life!
Right click in your Project, go to
Android Tools > Rename Package Application
Sometimes if Project > Clean doesn´t work, so Delete /gen and /bin folders of your project.

How to change package name of an Android Application

My keystore is corrupt, therefore the Android Market is requiring me to rename the app and resubmit it. However, whenever I go to edit the package names in Manifest and throughout the files, it gives me tons of errors.
What's the proper way to change the application name?
There is a way to change the package name easily in Eclipse. Right click on your project, scroll down to Android Tools, and then click on Rename Application Package.
If you're using Eclipse, you could try these instructions from Android's developer site. They're specifically for the GestureBuilder sample but should apply to any application as far as I can tell:
[H]ere's how you could do this in Eclipse:
Right-click on the package name (src/com.android.gesture.builder).
Select Refactor > Rename and change the name, for example to
com.android.gestureNEW.builder.
Open the manifest file. Inside the
<manifest> tag, change the package name to
com.android.gestureNEW.builder.
Open each of the two Activity files
and do Ctrl-Shift-O to add missing import packages, then save each
file. Run the GestureBuilder application on the emulator.
Also, be sure you remembered to rename the package itself along with the references to it in the files. In Eclipse you can see the name of the package in the file explorer window/tree view on the left hand side. In my experience, Eclipse can sometimes be a little finnicky and unreliable with this (leaving behind stray references to the old package name, for example).
Here's how you could do this in Eclipse:
Right-click on the package name (src/com.android.gesture.builder).
Select Refactor > Rename and change the name, for example to
com.android.gestureNEW.builder.
Open the manifest file. Inside the <manifest> tag, change the package name to com.android.gestureNEW.builder.
Open each of the two Activity files and do Ctrl+Shift+O to add missing import packages, then save each file.
Run the GestureBuilder application on the emulator.
Link to post
Update
super easy way
right click on your project...
In Android Studio, which, quite honestly, you should be using, change the package name by right-clicking on the package name in the project structure -> Refactor -> Rename...
It then gives the option of renaming the directory or the package. Select the package. The directory should follow suit. Type in your new package name, and click Refactor. It will change all the imports and remove redundant imports for you. You can even have it fix it for you in comments and strings, etc.
Lastly, change the package name accordingly in your AndroidManifest.xml towards the top. Otherwise you will get errors everywhere complaining about R.whatever.
Another very useful solution
First create a new package with the desired nameby right clicking on thejava folder -> new -> package.`
Then, select and drag all your classes to the new package.
AndroidStudio will re-factor the package name everywhere.
After that: in your app's build.gradle add/edit applicationId with the new one. i.e. (com.a.bc in my case):
defaultConfig {
applicationId "com.a.bc"
minSdkVersion 13
targetSdkVersion 19
}
Original post and more comments here
Without Eclipse:
Change package name and Activity names in AndroidManifext.xml and anywhere else it shows up in .xml files (custom view names, etc)
Update package import statement across all source files
Rename all folders to match the package name. Folder locations:
a. bin/classes
b. gen
c. src
Update the project name in build.xml (or your apk's name won't change)
Delete all the .class files in bin/classes/com/example/myapp/ (if you skip this step the files don't get rewritten during build and dex give a bunch of trouble processing "class name does not match path" errors
Delete gen/com/example/myapp/BuildConfig.java (I don't know why deleting BuildConfig.class in step 3a didn't cause dex to update it's path to this, but until I deleted BuildConfig.java it kept recreating gen/com/oldapp_example/oldapp and putting BuildConfig.class in there. I don't know why R.java from the same location doesn't have this problem. I suspect BuildConfig.java is auto-generated in pre-compile but R.java is not)
The above 6 steps are what I did to get my package name changed and get a successful* build. There may be a better way to handle steps 5 and 6 via dex commands to update paths, or perhaps by editing classes.dex.d in the root directory of the project. I'm not familiar with dex or if it's ok to delete/edit classes.dex.d so I went with the method of deleting .class files that I know will be regenerated in the build. I then checked classes.dex.d to see what still needed to be updated.
*No errors or warnings left in my build EXCEPT dex and apkbuilder both state "Found Deleted Target File" with no specifics about what that file is. Not sure if this shows up in every build, if it was there before I messed with my package name, or if it's a result of my deletions and I'm missing a step.
Follow these steps:-
Right click on the project.
Go to Android tools.
Click on Rename Application Package.
Change the application package name.
DONE
The fastest way to do that in Android Studio 1.3:
Change the package name in the manifest
Go to Module Settings[F4]-> Flavors, into Application Id write the same name.
Create new package with that name: [right-click-> new-> package]
Select all java files of your project and then proceed [Right-click-> Refactor-> Move-> {Select package}-> Refactor]
P.S. If you will not follow this order you can end up changing all the java files one by one with new imports and a bunch of compile time errors, so the order is very important.
Changing package name is a pain in the ass. It looks like different methods work for different people. My solution is quick and error free. Looks like no cleaning or resetting eclipse is needed.
Right click on the package name then Refractor > Rename.
Right click on the project name Android tools > Rename Application Package.
Manually set the package names in the Manifest that have not been changed by the previous steps.
Bernstein has the method, use the Eclipse tool, "Rename Application Package", you may have to do some clean-up even after the fact.
Also, Eclipse sometimes loses track of things when you make changes to a project. You may have to use the "Clean Project" tool (under the "Project" menu.) If that doesn't work, you may have to close and restart Eclipse. Voo-doo solutions, but Eclipse can be that way.
I found the easiest solution was to use Regexxer to replace "com.package.name" with "com.newpackage.name", then rename the directories properly. Super easy, super fast.
Fist change the package name in the manifest file
Re-factor > Rename the name of the package in src folder and put a tick for rename subpackages
That is all you are done.
There is also another solution for users without Eclipse, simply change the package attribute in <manifest> tag in AndroidManifest.xml, by replacing the the old package name used with a new one. Note: You have to adjust the all the related import statements and related folders manually in your project than, too.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="new.package.name"
.....
</manifest>
In Android Studio 1.2.2
Say if you want to change com.example.myapp to org.mydomain.mynewapp
You can follow the steps in each of the directories as displayed in the below screen shot. It will give you the preview option so that you can preview before making any changes.
Go to Refactor -> Rename and give your new name. You have to do it for each of the folders, say for com, example and myapp. It will automatically update the imports so that you don't have to worry about it.
Also update the file build.gradle which is not updated automatically.
defaultConfig {
applicationId "org.mydomain.mynewapp" //update the new package name here
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
After that it will ask for sync do it. Then everything will be fine. I'm able to upload my APK to google store after this. Make sure you have signed your app to upload it to the store.
Alternative:
Open AndroidManifest.xml
Change package="NEW NAME" within manifest tag.
Right Click on Project >Android Tools >Rename Application Package
Go to src and right click on your main package >Refactor >Rename
Go to manifest file and change your package name .
I just lost few hours trying all solutions. I was sure, problem is in my code - apk starting but some errors when working with different classes and activities.
Only way working with my project:
rename it - file/rename and check all checkboxes
check for non renamed places, I had in one of layout old name still in tools:context="..
create new workspace
import project into new workspace
For me it works, only solution.
So, using IntelliJ with Android (Studio) plugin was: I went to AndroidManifest.xml on top and changed package name by right-clicking -> Refactor -> rename.
Our then package name had four parts a.b.c.d, the new one only a.b.c.
So I renamed the first three. Then I went to the directory of generated sources (app\build\generated\source\r\development\debug\a\b\c\d), right-clicked R class and Refactor->Move[d] it to one package higher. Same with BuildConfig in app\build\generated\source\buildConfig\development\debug\a\b\c\d.
By using Refactor->Move, IntelliJ updates all references to BuildConfig and R.
Finally I updated all applicationId[s] I found in gradle.build I found. I hit Clean Project, Make and Rebuild Project. Deleted the app from my phone and hit "Play". It all worked out so refactoring was easy and quite fast.
In your Android studio , at project panel , there is gear icon "Click on it".
Uncheck the Compact Empty Middle Packages
Now the package directory has been broken up i.e individual directories.
Select the directory you want to rename.
Right click on that particular directory.
Select refactor and then rename option.
The dialog box has been open up,rename it.
After putting new name, click on Refactor.
Then click on Do Refactor at bottom.
Then android studio will update all changes.
Now open build.gradle file and update the applicationId & sync it.
Now go to manifest file and rename the package.
Rebuild your Project.
You are done :)
If you are looking for a quick and safe solution to change the package name, then I suggest you use the plugin "Android Package Renamer", It is available on Android Studio Market.
What you need to do is
enter "Your package name you want to change"
Click OK.
You can see the detailed answer here

Categories