How to remove parts of an android package name with AndroidStudio? - java

I have an android project with a package name like com.example.alexander.app and I want to change this to a name containing only three parts, like com.alexapps.firstapp. However, after searching several sites, I still have no idea how to do that in AndroidStudio (2.1.3).
I found the following answer, which do not fully answer my question:
Answer 1 requires the file R.java which I cannot find
Answer 2 looked very good by using methods in AndroidStudio. Unfortunately, even after cleaning and rebuilding the project, the package name was not changed on my single activity.
Answer 3: I searched for the string, and found more than 1000 hints!! Therefore I have not continued with this solution to replace more than 1000 hits, as this does not sound right. I have only 1 (one!) single activity in this test app...
Answer 4: Very good answer, but only useful if you want to change each of the package name parts (and neither increasing nor decreasing the number of parts in the package name).
So is there a way to remove a part of a package name with AndroidStudio?

The easiest thing to do is right click the folder/package, and refactor.
This however i think is limited to the final folder, and does not remove a hierarchy.
So the solution in this scenario, is to
create a new folder
move all the folders/files into it. (this should initate a refactor on each file)
Rename any references to your activities etc in the manifest to the correct package names (it should have done this for you)
Clean and rebuild (to fix the R file)

Related

Eclipse custom builder: Identifying and cleaning generated files

I am creating a plugin for Eclipse, which contains tools for creating a custom type of project. These projects have a custom nature and builder. My builder (implements IncrementalProjectBuilder) takes a single input file, and generates a few (usually between 3 and 5) output files. When I run Clean Project, I need to remove the files the builder has previously generated.
Problem 1: The names of the generated files are not known exactly, but I do know the sort of files I expect to find (e.g. I know the extensions, and partial file names).
Problem 2: The user may add their own files to the project, which should not be affected by my build / clean steps.
My initial attempt was naive: remove every file except from the input file. This works, but has obvious problems.
My second attempt was better: I came up with a list of possible file names that may be generated, see if any of them exist and remove them.
By only knowing partial file names and matching them, I may inadvertently delete a user's file. E.g. I know I will generate a file called *_file.py. If the file I generate is called abc_file.py and the user has added their own xyz_file.py, I want to clean (remove) abc_file.py but leave xyz_file.py untouched.
The program which generates the output files from the input is constantly changing, and I don't want to rely on a concrete list of files that would need constant maintenance.
So, my question comes down to this. What methods exist for identifying the files generated by my custom builder, so I can remove them during a clean?
I've spent a couple of days Googling this one with not much to show for it. I am vaguely aware of a file system watcher in Java (Java7 WatchService?), but I don't know if that's the best solution to this problem.
Any information, advice or ideas appreciated.
One brute force approach would be to compare the project before and after the other program is invoked to get the list of files that were created/generated. Of course, it would be ideal if that program could somehow tell you which files it created. Once you have that list, you could iterate over those files as IFile's as use the setDerived() method to mark them as not being source files. When it comes time to clean the directory, you could use the derived setting to decide which files can be deleted.

Don't need R, but getting R cannot be resolved to variable error

I'm making my first app within eclipse, and I've been watching a YouTube playlist to help along the way (Link below).
Currently I'm working on building buttons and linking pages to each other. The problem I'm running into is an R cannot be resolved to a variable error, for any code like the following.
Atlantic = (Button) findViewById(R.id.Atlantic);
or
setContentView(R.layout.mainactivity);
The quick fix wants to import android.R. When you do this, the ID kicks off this error "ID" cannot be resolved or is not a field.
The odd thing is, sometimes I can just remove import android.R and it will work. However, it tends to come back after adding a picture or something to another screen (that has nothing to do with any Java code (yet) or any ID (yet))
BTW, I don't know if this helps. for the ID I use the "#+id/bla" trick.
Try cleaning your solution and it should regenerate the R.java file. It basically is a generated file to keep track of all your views/layouts/strings etc.
It is very necessary to make your app work correctly.
Try the following steps:
Clean the project from project->Clean
Build the project from project->Build
it should show up in blue
Just a little info on what goes under the hood:
When you define something in an xml (like R.id.Atlantic) , there is no way for your JAVA code to know that you defined it. So there is something called your R files that 'bind' the two together.

Unable to import image resources into our GUI

I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.
Here's an example of some of the references:
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))
jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));
I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?
Hope that all made sense!
For the examples you have given your file structure might look like this
src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg
For a more best practice way of organizing your resources you could do this
src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg
and then use the following calls to load them
getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")

bug with "Extract Android String" option in eclipse

I'm having trouble using the refactor feature in eclipse (Indigo on Win7 64) to move my hard coded strings to the string.xml file.
Automatic Code Generation for Strings.xml from Eclipse
It creates the entry in string.xml ok but when it builds the appropriate code, part of the code can be duplicated and the resulting code is nonsense. For example:
...setText("Inbox")
becomes:
...setText(R.string.inboxg.inbox)
instead of
...setText(R.string.inbox)
This behavior varies depending on the length of the line and what is being replaced.
Anyone seen this before, know of a fix?
I've had a very similar occurrence happen to me when refactoring the package name. I can't really see a way to fix this (besides a global find and replace), perhaps an issue needs to be submitted:
Android google code issue repository:
http://code.google.com/p/android/issues/list
I've done a quick search through the issues and couldn't find anything related.

In Eclipse, when building an Android app I've somehow deleted the "R" class and cannot get Eclipse to generate it again

I'm new to Eclipse/Android development so I'm hoping this is something basic that I'm just doing wrong. Basically I've been playing around with developing Android apps and I was editing one earlier. I changed my main.xml file and when I went back to my class that runs the app all references to the "R" class were broken. I looked in the "gen" folder (Generated Java Files) and saw that the class was completely gone from the solution.
I had been debugging not 5 minutes before and didn't have any problems doing so. The only things I had done since debugging were moving 2 files from the "drawable-hdpi" folder to the assets folder and I had edited the "main.xml" file.
I've tried cleaning the project and restarting Eclipse but neither of those ideas worked. Any suggestions?
Check your main.xml file. If there is a fault in it (invalid XML, wrong element name, etc), then the R.java file may not be generated properly and you will have broken references.
Also make sure that the XML file itself does not have broken references (to strings, colors, and other kinds of assets).
Re-examine properly all that has changed since the last time your application was working.
I have been in your situation before. I dont have direct answer.
I assume you still learning Android (Like me) ...
The best thing is redo or rewrite the code from the beginning.
It sharpen your skill, and sometimes in the middle you find the answer.
It more quicker, Rather than staring the code and try to fix it.
It work for me, but need patient (which sometimes, I dont have it ...)
;)
I have had this problem also.
By typing an error in one of the xml files, R Class couldn't be generated.
Eclipse then is so friendly of adding an import to Android.R automagically.
This is often overlooked.
Fixing the xml-file errors will regenerate the R class. But your code is not linked anymore to your.own.namespace.R But to Android.R and there it will not find your items.
Removing the Import Android.R from the top of your file fixes this problem.
To be sure if the R class is generated, but wrongly imported, you can look under the folder "gen", there should be R.java file.
You could also try by typing your.own.namespace.R. and see if the intellisense shows your items then.

Categories

Resources