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.
General
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
File >
Close Project.
-
To remove a project completely from Android Studio, open it in AS and display
the project
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
dialog box,
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.
-
If the original project was under version control under the old name, remove from version control following the instructions under the heading Removing a Project from Version Control in
Version Control.
-
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
this book.
Configuring Android Studio
Android Studio installs with a set of default options that can be customized for your particular usage. First, there are various settings that can be found under File > Settings and File > Other Settings.
In a somewhat confusing manner, there are also more technical settings that can be found under Help > Edit Custom VM Options, which edits a configuration file for the Java virtual machine. The options that can be set and and physical location of this file may be found at
Configuring Android Studio. For example, you can increase the memory allocation for AS there, or change the location of the temporary directory that Android Studio uses for downloading updates (see the following box). The name and physical location of the file being edited through the AS interface depends on the installation. On my Fedora Linux systems the file is /home/guidry/.AndroidStudio2.1/studio64.vmoptions, for example.
One potential problem that can be cured by editing the vmoptions file is that Android Studio updates use a temporary directory to download, unzip, and backup files during the update, and in some cases the space required may be larger than the size of the default temp directory. Then the update will fail and it may lock up parts or all of your system because now the temp directory that the system uses for other things has no space left. I have encountered this on Fedora Linux systems, for example, where the default directory that AS uses for updates is a /tmp directory with a capacity of 4 GB, which isn't enough for the largest updates.
There is a simple
solution: In the file edited by Help > Edit Custom VM Options you can add a line that changes the temp directory used for updates to one with more space. For example, creating a new directory in my home directory /home/guidry/tmp and then adding a line
-Djava.io.tmpdir=/home/guidry/tmp
to the vmoptions file switched the temp directory for updates to the new directory (with 201 GB available when created), so there is no problem with even large updates.
|
Editors
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.
Version Control
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