Home
How to Copy and Rename a Project in Android Studio
Sometimes you need to make a copy of a project before making changes or to use some of its code in a new project. This guide will help you copy an existing project and rename it. Complete all the steps in order, before attempting to run the new project.
Instructions
In the instructions below, substitute your project names for ExistingProject and NewProject.
Step 1: Copy the Project
- On your computer's file system (not in Android Studio), make a copy of the
ExistingProject
directory.
- Rename the copied directory to
NewProject
.
- In the
NewProject
directory, check for the existence of .git
and .github
files. If those files are absent, skip to Step 2. If those files exist, then the original project was connected to the git version control system (VCS). Delete the .git file (and the .github file if it exists). To regenerate a new .git file for the NewProject, in a terminal, in the NewProject directory, issue this command: git init
Step 2: Rename and Refactor the Project Components
The old name of the project, ExistingProject
, still appears throughout the packages and files in the NewProject
. Here's how to change the file and package references:
- Start Android Studio.
- Select Open an existing Android Studio project. If you already have a project open, select File > Open.
- Navigate to the
NewProject
directory, select it, and click OK.
- If the Project pane isn't visible, select the Project side-tab.
- Select Android from the drop-down menu in the Project pane to view your files.
- Expand the app > java folder.
- Click on the Cog option button, select Tree Appearance and Compact Middle Packages to view a compacted view of your packages.
- Right-click on
com.example.android.existingproject
, then select Refactor > Rename. A warning dialog will appear; select Rename package.
- The Rename dialog opens. Give a new name to your project.
- Select both options: Search in comments and strings, and Search for text occurrences. Click Refactor.
- The Find: Refactoring Preview pane opens, showing the code to be refactored. Click Do Refactor.
- Expand the res > values folder and double-click on the strings.xml file.
- Change the app_name string value to "
New Project
".
Step 3: Update build.gradle, settings.gradle and AndroidManifest.xml Files
Your app must have a unique application ID as defined in the app's build.gradle file. Follow these steps:
A. Update Gradle Files:
- In Android Studio's Android pane, expand Gradle Scripts and open build.gradle (Module: app).
- Under defaultConfig, ensure that the value of applicationID is "
com.example.android.newproject
" and the version number is 1. Change any incorrect values manually.
- Click Sync Now in the top-right corner of Android Studio.
- You can also sync by selecting Tools > Android > Sync Project with Gradle Files.
- Change rootProject.name in the settings.gradle file and sync.
B. Update AndroidManifest.xml File:
- Expand app > manifests folder and double-click on AndroidManifest.xml.
- Check that the package name is "
com.example.android.newproject
". Change it if necessary.
- If necessary, change the label to reference your new app name:
android:label="@string/app_name"
.
Step 4: Clean, rebuild and run the project
- Select Build > Clean Project to remove auto-generated files.
- Select Build > Rebuild Project to regenerate files with the new names.
- Select File > Invalidate Caches... (all optional items selected) to clear old caches and indexes.
- Sync your Gradle files.
- Run the project to ensure everything works correctly.
By following these steps, you should have successfully copied and renamed your Android Studio project. Remember to update any other references to the old project name or package name in your code if necessary.
Home
Source: https://developer.android.com/courses/extras/utilities