Android Studio Tips and Customization
Android Studio (AS) is a powerful Interactive Development Environment (IDE) that greatly facilitates the development of Android apps. This Appendix gives some tips and customizations that may be useful in getting the most out of AS. The remarks refer specifically to Android Studio 2.1.1 on Fedora Linux, but other versions on other platforms will likely be similar.
Some general features and tips are collected here, in no particular order.
To return to the main homescreen of Android Studio from within a project, select
To remove a project completely from Android Studio, open it in AS and display
structure in the left Project Panel (opened all the way to the file level). In
the project bar at the top:
Right-click on the project name and select Delete. In the resulting
again choose Delete.
Use the file explorer for your computer to remove the directory corresponding to the project (by default in the StudioProjects directory for Linux).
On the opening screen for Android Studio, right-click on the project name in the list of recent projects on the left and select Remove Selected from Welcome Screen.
To rename a project in Android Studio
- Back up the folder containing the project.
With the project open in Android Studio, right-click on app in the project panel or project bar, select Refactor > Rename from the context menu that pops up, and change the name, say to app2. (Note: this step works, but is it necessary? Check.)
Find the full namespace package declaration (e.g. com.lightcone.myapplication), say under app2/java, right-click on it, select Refactor > Rename, and rename to the new name (say com.lightcone.myapplication2).
Open the Gradle Scripts/guild.gradle (Module: app2) file and change the applicationID to the new name.
Change the app name in the res/values/strings.xml file.
Change any occurrences of the name to the new name in AndroidManifest.xml.
Close Android Studio. Use the file explorer to change the name of the directory holding the project to reflect the new name (e.g., MyApplication to MyApplication2).
Open Android Studio again, navigate to the renamed project, and open it in Android Studio. This should give you the new project, ready to edit.
Close the project and (if necessary) clean up the recent-project menu in the Android Studio launcher to remove anything no longer needed.
Be sure to rename the backup directory to the original name if you intend to keep the original project in addition to the new project.
To split an editing window into two windows, right-click on the tab and choose Split Vertically or Split Horizontally. To unsplit, right-click on a tab in a split window and select Unsplit.
From the menu presented when you right-click on a file tab in split windows, you can toggle between horizontal and vertical splitting, and move files between the split panes using options presented.
Within an editing window, files can be re-ordered by dragging their tabs.
If you modify Java or XML files external to Android Studio on a particular machine (e.g., replacing the files with files that you have modified on another machine) the file system will often be out of sync in in the AS interface. Remedy that by right-clicking on the project name in the project bar and selecting Synchronize 'appname'. Then do a clean build by selecting Build > Clean Project.
Likewise, if you add new files manually to directories they may not show up in the Android Studio project tree unless you right-click on the directory and select Synchronize 'name'. To be certain, you should probably force a clean build after the files show up, as described above.
If you are debugging on an emulator or device and have already run the program, for some changes (for example, modified drawable resources) Android Studio may not recognize that something has changed and you will get a message that the old activity has been restarted rather than the new activity started. You can usually force execution of the new modified version by forcing a clean build (Build > Clean Project) before launching the app.
- All Android applications must be digitally signed. For development, Android Studio takes care of this under the hood by generating a "debug" signing certificate in a keystore called debug.keystore. Android Studio uses this certificate to sign each application build,
but the debug certificate is valid for only a certain period (currently 1 year). Thus, after some date you may suddenly get build errors for all of your projects. The overall solution to the problem is to remove the debug.keystore file. Then the next time you build and run Android studio will generate automatically a new keystore and debug key.
The location of debug.keystore is platform-dependent; see Expiry of the Debug Certificate for help in finding it. Currently, standard locations for the debug.keystore file are
- Windows Vista and Windows 7, 8, 10: C:\Users\<user>\.android\
- Windows XP: C:\Documents and Settings\<user>\.android\
- Linux and Mac OS X: ~/.android/
where ~ denotes the user's home directory. For example, on my Linux system it is at /home/guidry/.android/debug.keystore.
Note that if the debug certificate changes, things that depend on it may have to be regenerated. For example,
if you have mapping applications that use the Google Maps API, you will have to use the new debug certificate to generate a new mapping API key (see MappingDemo).
The debug key is adequate for development but if your app is published to the Play Store you cannot use the debug certificate; the app must then be signed with a private key that identifies the developer uniquely. See
Sign Your App for details.
A more extensive discussion of using Android Studio and its features may be found in
For the Android Studio editor.
Settings for the editor may be found under File > Settings > Editor.
- To toggle line numbers on and off in code display, right-click on the on the gutter to the left of
the code display field and select or unselect Show Line Numbers.
To mark blocks of text as a comment, highlight the text with the mouse and either
Code > Comment with Line Comment (Ctrl+Slash),
to comment by placing a // at the beginning of each line, or
Code > Comment with Block Comment (Ctrl+Shift+Slash), to set the code off
as a block comment between /* and */.
To uncomment, select the text and repeat the command used above to turn it into a comment.
To locate all occurrences of a class, method, or variable in a project, select it and do Edit > Find > Find Usages (Ctrl-Shift-F7). F3 and Shift+F3 will toggle forward and backward, respectively, through the list. Press Escape to remove all highlighting.
To see a local history of file changes, invoke its context menu (right-click on it in the editing window) and choose Local History > Show History. From there you can scroll through the version, compare versions, and roll back changes. A similar technique works on directories. If you discover you have done something dumb, this may help you recover.
Android Studio has fairly sophisticated reformatting capabilities under Code and refactoring capabilities under Refactor in the top menu.
To compare two files in the project, select them in the project pane by clicking on one and then shift-clicking on the other. Then right-click on one of them and select File > Compare Files. This will place them side by side, with differences clearly marked. To compare with a file not in the project, you can temporarily add the file to the project and then delete it when you are finished comparing.
It is highly recommended that you use version control software to keep track of your projects. Android Studio supports several version control systems: GitHub, Git, Subversion (SVN), CVS, Google Cloud, and Mercurial. The basics of using Git and the GitHub repository for version control with Android Studio are described in the Appendix Version Control.
Last modified: June 10, 2016