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

  1. On your computer's file system (not in Android Studio), make a copy of the ExistingProject directory.
  2. Rename the copied directory to NewProject.
  3. 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:

  1. Start Android Studio.
  2. Select Open an existing Android Studio project. If you already have a project open, select File > Open.
  3. Navigate to the NewProject directory, select it, and click OK.
  4. If the Project pane isn't visible, select the Project side-tab.
  5. Select Android from the drop-down menu in the Project pane to view your files.
  6. Expand the app > java folder.
  7. Click on the Cog option button, select Tree Appearance and Compact Middle Packages to view a compacted view of your packages.
  8. Right-click on com.example.android.existingproject, then select Refactor > Rename. A warning dialog will appear; select Rename package.
  9. The Rename dialog opens. Give a new name to your project.
  10. Select both options: Search in comments and strings, and Search for text occurrences. Click Refactor.
  11. The Find: Refactoring Preview pane opens, showing the code to be refactored. Click Do Refactor.
  12. Expand the res > values folder and double-click on the strings.xml file.
  13. 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:

  1. In Android Studio's Android pane, expand Gradle Scripts and open build.gradle (Module: app).
  2. Under defaultConfig, ensure that the value of applicationID is "com.example.android.newproject" and the version number is 1. Change any incorrect values manually.
  3. Click Sync Now in the top-right corner of Android Studio.
  4. You can also sync by selecting Tools > Android > Sync Project with Gradle Files.
  5. Change rootProject.name in the settings.gradle file and sync.

B. Update AndroidManifest.xml File:

  1. Expand app > manifests folder and double-click on AndroidManifest.xml.
  2. Check that the package name is "com.example.android.newproject". Change it if necessary.
  3. If necessary, change the label to reference your new app name: android:label="@string/app_name".

Step 4: Clean, rebuild and run the project

  1. Select Build > Clean Project to remove auto-generated files.
  2. Select Build > Rebuild Project to regenerate files with the new names.
  3. Select File > Invalidate Caches... (all optional items selected) to clear old caches and indexes.
  4. Sync your Gradle files.
  5. 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