Android Studio rename attribute or method not always working - android

In Android Studio, I sometimes have to rename some field, attribute or method name.
I know I must select its name, then hit Alt+Shift+R. Then I type in the new name, and hit Enter.
However, sometimes it works, and sometimes... it just does nothing at all. The new name is just discarded and replaced with the old ones.
Can someone explain me why and how to force this very basic feature to work ?

I suspect that you've missed the refactor preview window that shows up when AS finds some code that it doesn't know if it should be refactored or not. For example, if the method being modified is referenced in comments, then AS will ask if you want to refactor those comment as well.

using a shortcut for "Rename" Variable Shift + F6
or
Select Variable/Method name -> Right Click in java file -> Refactor -> Rename

This also happens when using dagger and trying to refactor via Shift+f6. Android Studio hesitates when renaming things in generated code (which seems silly). Anyway, doing a project clean and then doing the rename works.

Alt+Shift+R replace a selected String (in your case the selected field, attribute...) and doesn't rename the variable, field... itself.
The feature I use for that task is Rename (not Replace). To achieve that you can either use Shift+F6 or right mouse click > Refactor > Rename. Now it changes the name also in other classes (if necessary).

For me, every time I refactor>rename it was showing me the usages and I changed them manually, but the name of the java class stayed the same, even I changed all the usages.
Finally, I unticked all the boxes, and it worked.

Related

What is the best way to change the package name of an Android project?

I want to change the package name of my Android project using Android Studio from let's say, com.company.myapp to com.name.company.myapp. While there are many questions and answers that deal with renaming a part of existing package name but I couldn't find any help on how to add a new part to it.
Please help me in this regard.
Thanks
Changing the application ID
(which is now independent of the package name) can be done very easily in one step. You don't have to touch AndroidManifest. Instead do the following:
1. Right click on the root folder of your project.
2. Click "Open Module Setting".
3. Go to the Default tab.
4. Change the applicationID to whatever package name you want. Press OK.
P.S. if you're using google-services.json , make sure you update new ID there too.
Rename package name
From project pane (left), click "Show option menu" it has settings like icon. Then from menu select "Compact Middle Packages"
Then you will observe your main package.name folder expands. Right-click your (Package.name/ middle/ last name of your package name) expanded > Refactor > Rename
Click "Do Refactor" from expanded Find tab at the bottom left of it.
Finally click build.gradle (app-level), then change applicationId to 'your.new.packageName'. Then SYNC NOW
Done
I tried more than 10 approaches and none of them worked (and sometimes not explained completely, so I deviated a little and found my own way. Again, my way is also not simple, but it does the job and Google Play didn't give me any errors.
My Solution
1. On the Project pane on the left, select Project (it might say Android).
2. Go to nameOfYourProject -> app ->src -> main ->java ->com -> example
3. Right click on "example" -> Refactor -> Rename and type your desired name, select the two check boxes and hit Refactor.
4. Now go one level above example (folder) and right click on "com", but this time DO NOT check the two check boxes (as it will also rename the "com.google...." parts of your project) and hit Refactor.
5. Now open your Manifist file and manually change the package to your desired name.
6. Rebuild your project. If you see any errors, just open the file with the error and change the package name manually.
7. Open your gradle.build file and change application ID, to your new package (only the parts of it, not everything) and hit Sync (top right side). Hopefully this works.
P.S. Another "easy way" might be to use Agent Ransack, search for the package name by enclosing it with quotations and manually change the package name. You might want to get a copy of your project before doing this. I didn't try it, cause I was already knee-deep with all the other methods here and didn't try.
This answer from the link in the comment provided would be most likely to have any issues arise, and would be simple. Possibly tedious depending on your app. I attempted changing package name once and android studio does not accomplish this very well.
Simple solution
To Rename APP package:
create a new package with the desired name by right clicking on
the Java folder → New → Package;
Cut then paste all your classes to the new package. Android Studio will refactor the package name everywhere;
delete the old package;
change AndroidManifest.xml and build.gradle file to the new package if you use this method.
Note: it’s more safe to make a new project.

Eclipse ADT - project structure changed showing blank packages

I am trying to rename my eclipse android project. I have tried Android Tools -> Rename Application Package. It gave me an error occurred see log.
I can't find any logs and suddenly this happen my project structure shows some blank and garbage packages as shown in picture.I tried deleting it but deleting them deletes my source code too.Any idea will be very helpful..
You can rename classes, packages and projects by right clicking on the class/package/project and choose: Refactor -> Rename. Check the Update references box to make sure all references to the project/class are also changed.
This behavior is totally ok.
A java package corresponds with an folder.
When a package/folder is empty eclipse will show it as "white boxed" without content (no arrow).
Example:
The class "AClass" in "com.me.pckg" corrensponds with
com/me/pckg/AClass.java
This means folder "com" and "me" contains no files. Eclipse represents this a "white boxed" packages (com and com.me). Package "com.me.pckg" is not empty.
Aware eclipse does not show the package structure NOT as hierarchical as you expect it in a file system view.
Vice versa if you put some file/-structure in your project eclipse will show new folders a packages. This happens after a refresh or a restart of eclipse, the later may be perceived as a sudden action.
If you want to change the type in "com.demo.alaramdemo" use Pieter12345 answer(, or just change the name of the folder in the file system)
If you want a more file system like view within eclipse use the "Navigator"-View
Finally i got the answer to this, With the help of a post above by Stefan
and a solution mentioned here on below link http://www.coderanch.com/t/105581/vc/Eclipse-hide-empty-packages
actually i had to create filters available on a inverted triangle in package explorer header.
In that u have to check filter of empty package , empty parent package etc
it worked for me.
Thanks all for the help ... :)

How to delete an unused string resource for all configurations in Android Studio?

I've found an unused string resource, like:
<string name="obsoletestring">my name is null!</string>
However it is in tens of files, of different languages, in different strings.xml files in values, values-af, values-be, etc folders.
I must have missed something not to know any way to do this in Android Studio other than modifying it by hand one by one.
tl;dr How to delete an unused string resource for all configurations?
To identify all unused resources:
Open Menu > Analyze > Run Inspection by Name...
Select "Unused Resources"
Make sure Whole project is checked, then press OK.
Look through the list. You can always rerun the Inspection with the Rerun button.
There is no really easy way in Android Studio (v 1.0) to remove a resource string for all locales. However, you can search and replace in files. Fortunately, the translation files use only a single line in most cases so this works pretty well.
In Android Studio:
Start with no changes pending in your source control (makes it way easier to check and back out if this goes wrong).
Open Menu > Edit > Find > Replace in Path...
In Text to find: .*name="obsoletestring".*\n
In Replace with: (empty)
Check Regular expression
Press the Find button.
Inspect the results. If okay, press the "All files" button.
You will probably have to remove the comment in res/values/strings.xml manually.
Make sure your project still builds.
Take a look at the diff of your project in your source control to ensure the change is really what you intended...
It is now possible inside Android Studio.
After Lint checks you see an option on the right Remove All Unused Resources!
To Delete a single string resource across all locale files, you can use the "Translation Editor".
1. Right click on the res directory to open the translation editor.
2. Select "Show All Keys" selector, and choose "Filter by Text". Supply the name of the resource that you want to delete.
3. Select the resource, and click on the "-" button
Until IDE support comes along, something along these lines will work:
find -name strings.xml|xargs -rd\\n sed -ri '/"string_to_delete"/d'
In Android Studio 2.3 it's possible to remove all unused resources.
Open any *.xml in your res/values/ directory
Right click on any item's name
Refactor -> Remove Unused Resources...
Menu -> Analyze -> Run Inspection by Name -> Unused resources
From the results select all string resources that are unused.
Right-click highlighted rows and choose "Suppress with #SuppressLint(Java) or tools:ignore(XML)". This will add the attribute tools:ignore to all strings in all string files.
Menu -> Find -> Replace in Path
Text to find: ^.*?tools:ignore="UnusedResources".*?\n
Tick regular expression box
Use Scope: Custom
Open custom scope editor and add pattern: file[app]:src/main/res//strings.xml
Find
Etc.
Unfortunately, You have to do it manually.
Check this answer to understand what exactly should you do to get rid of them using Eclipse
If you are using Android Studio find them in the whole application and also remove manually .. Check this answer
Beware that the REMOVE UNUSED RESOURCES command cannot recognize a programmatically accessed resource as a used resource (such as getIdentifier(..) etc.).
So, if you do access resources that way, it is highly risky to use that command!!
In fact, Android Lint should report about the unused resources, but you can also try with this nice plugin.

How do I jump to XML resources in Eclipse instead of R.java

Eclipse version: Indigo Service Release 2
ADT version: 18.0.0.v201203301601-306762
I have the following code in Eclipse
View view = inflater.inflate(R.layout.somelayoutfile, parent, false);
I want to perform some action, like CTRL+Click on "R.layout.somelayoutfile" and it jump to that xml file. Instead it takes me to "R.java."
According to this the Android Documentation this should be supported, but it doesn't work. Does anyone know why, and/or how to enable this feature?
The most efficient way.
Eclipse settings - Hyperlinking - Open Declaration - uncheck.
CMD+Click will start to navigate you directly to XML instead of R class.
You'll still be able to use the Open Declaration by pressing F3.
If you hold Ctrl with the pointer over the resource name (somelayoutfile in your example) then a menu should appear, with two options:
Open declaration
Open declaration in layout/somelayoutfile.xml
That isn't working for you? Or you want the default (direct click on the resource name) to be the second option?
You Can Use Ctrl+Alt+Shift and click on res
or on Preferences -> General -> Editors -> Text Editors -> Hyperlinking you can change shortcut key
CTRL + CLICK is the correct way to do that, but sometimes it doesn't work correctly, I don't know why. It's aggravating, I know. If I were you, I'd try one or both things below.
Usually I'll clean my project and if that doesn't fix it, then I'll restart Eclipse and then clean again. One of those two will typically do the trick for me.
Hold down ctrl (or command on Mac) THEN move your cursor over the resource name. If you move your cursor to the resource name, and then hit ctrl, it will not underline the resource name and will not provide a link. You have to hit ctrl and then move the cursor.

Android - copy existing project with a new name in Eclipse

I'm using Eclipse. Say I have a project 'ProjectA' in the eclipseworkspace (in a folder ProjectA). I want to copy the project but with a new name (ProjectB) and have all the references in the new project adjusted. I can't find much help for what I thought would be a vary basic operation. I want to try out variations on the original design/code and creating a copy seemed the obvious way to go about it.
Any suggestions please.
Copy your project to a new one (Ctrl+c & Ctrl+v).
Change the package-name in your manifest
Rename your main package with refactor. Don't forget to select.
Change your application name in manifest
application android:icon="#drawable/icon" android:label="**new name**"
I just want to add an item to Mur Votema's list:
5) Don't forget to open the project you want to copy!
It just cost me some minutes to find out that it doesn't work with closed projects. :-)
If you are using Eclipse and all you want to do is clone your Android project and give it a new project name, simply do the a copy/paste (Ctrl-C & Ctrl-V - step 1 in Mur's answer above). Eclipse will ask you for a new project name when you paste and you're done. The Eclipse project name and directory are independent of the application name and package.
Mandatory tasks:
In Eclipse, Copy and paste the project. (Give the project a new name)
In Manifest, update the Package name and save. (Update Launch
Configuration)
Refactor and rename the Package name. (Update references)
Optional tasks:
In Manifest, change the Application label and icon, so after
installing the application on your device it won't be confused with
the first one.
Refactor and rename the java files name, so they won't be confused
with the original project files.
If you have used Encodings such as UTF-8 in your project (in order
to handle languages other than English) you might need to edit
and save those files so their font would be shown correctly.
Note: If you do the 3rd task before 2nd task, some undesired imports would be added to your java classes. You then will need to remove those imports.
There's now an automatic tool for that, part of the ADT.
Make sure the origin project is open in Eclipse, copy & paste to a new name
Right click on the new project > Android Tools > Rename Application Package
Done
As said above all you have to do is copy+paste the project and rename it ["myApp.11"] but sometimes this feature gets bugged in eclipse. One such case happens when you have more than one version of the jdk. to solve this close eclipse rename the older versions of the jdk with an underscore e.g. _jdk17.2
and retry. if you dont need these versions delete them.
Another thing I noticed is that if you leave android:label="#string/app_name" it somehow keeps the original app name when you view the copied app in the list of Applications in Settings > Applications > Manage applications but if you change it to android:label="Your App Name" it seems to work ok.
Also be sure to change your icons properly by deleting the old ones and importing the new ones.
I went to the workspace folder on C:/ (path different for all) copied the project folder (Project A), and pasted it with a new name (Project B). Change the name while in explorer. Then, through Eclipse, made a new android project with the new name (Project B). Different way of doing it.
I Just wanted to add this after struggling with a code i have at work:
DO NOT USE THE PACKAGE NAME EXPLICITLY IN YOUR JAVA CODE NO MATTER WHAT. YOU MIGHT NEED TO RENAME YOUR PROJECT .
EXAMPLES ARE : getResource() and setting the Bitmap source and such and such
it looks like that I had bad references to the package name. it name was used explicitly as a String (e.g "com.pakge.mypkg" ) instead of Context.getPackageName() .
try to reach this method anyways you can 'cos the way i just mentioned it might not work for you... try to reach the method name "getPackageName()" from different other objects.
I have to answer not comment as reputation not high enough - to say that I agree #jengelsma has the best answer, just copy and paste, but watch out for the (hidden) SVN files (in .svn folders)! Unless you want the same references as the other project you don't want these.
Team > disconnect and check 'also delete SVN meta data' on your new copy to do this.
Just want to add some more points :-
Be ensure that the package name must be different for new project and old project if both project are going to install on same machine.
also modify the same in Mainfest.xml
under
I used the eclipse Luna, I tried to copy an existing project (Project_A) as below
Create a new Java project (its name is as Project_B)
Right click on Project_B (to pop up the context menu)
Select the menu item "Import..." (to show the "Import" dialog)
On the list under the "Select an import source" field to extend the item "General"
Double click on the sub-item "File System" (to show the "File System" page)
Enter the path of "Project_A" directory into the "From directory" field (or click the "Browse..." button to find and select the directory of "Project_A")
Check the checkbox of "Project_A" directory under the "From directory" field
To see if the "Into folder" field is showing folder name as "Project_B" (if not, click "Browse..." button to find and select the "Project_B")
Click the "Finish" button (because the Project_B is empty, need not care any more)
The all contents of Project_A are copied into Project_B, we can run the Project_B as the Project_A (if needed we can refactor the Project_B)

Categories

Resources