Sometimes, you might want to copy an Android project that you’ve created using Android Studio.
You might want to create another application by using the existing application code as its base.
To create a copy of an Android project, you need to manually copy the project folder from your computer first.
Open your computer’s finder or explorer window and navigate to where you save your existing Android project.
Create a copy of the existing project using the copy and paste feature on your computer as shown below:
If you have a large Android project, then you can reduce the amount of files to copy by running the Clean Project
command.
Open the Build
menu, then select Clean Project
as shown below:
This command is also useful when you are using the Windows operating system, as Windows can’t copy a file that has a path longer than 260 characters.
If your Android project has long file paths, you might see the following error:
Cleaning the project should help you solve the above error.
Finally, you can also try to copy the Android project files from the terminal to help speed up the process.
Use the cp -r [existing folkder] [new folder name]
command from the terminal as follows:
cp -r KotlinApp KotlinAppClone
Once the project has been copied, you need to open it in Android Studio next.
Refactoring the copied Android project
When you open the project using Android Studio, you’ll see the Gradle sync process begin.
Wait until the Gradle sync is finished, then open the AndroidManifest.xml
file in your project.
There are three properties you need to change in the manifest file:
- The
package
attribute of the<manifest>
tag - The
android:label
attribute of the<application>
tag - The
android:name
attribute of the<activity>
tag for theMainActivity
Please note that you must not change these attributes manually. Right-click on the attribute you want to change and select Refactor > Rename from the context menu as shown below:
You will see a warning saying multiple directories correspond to the package.
This is fine since you intend to rename the directories as well. Select Rename Package and continue to rename your package name.
You can Preview the changes from Android Studio and hit Refactor once you’re ready.
Changing the android:label attribute of the application
Next, you need to change the Android:label
attribute by changing the @string/app_name
resource in your Android application.
This is usually located in the res/
folder of your project.
<resources>
<string name="app_name">KotlinApp</string>
</resources>
Once you change the app_name
string tag, you need to change the project name from the Gradle settings file.
Changing the project name in Gradle settings
To change the copied project name, you need to open the settings.gradle
file located in the root folder of your application.
Inside, you need to change the rootProject.name
attribute as shown below:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "KotlinApp"
include ':app'
Once you change the attribute, you’ll see a message saying Gradle files have changed and you need to sync the project.
Click the Sync Now option and you should see the project name changed for the application.
Reload the files and build the copied project
Head to the Project pane of Android Studio.
Right-click anywhere and select Reload From Disk menu as shown below:
With that, your copy project is ready for compilation. You should be able to build and run the application successfully.
Keep in mind that you need to change API keys and other files used in your copied project.
For example, you need to change the google-services.json
file when you integrate your Android project with Firebase.
You also need to change the AdMob ID if you use AdMob in your application.
To conclude, below are the four steps to create a copy of an Android project:
- Copy the project manually from your computer
- Refactor the package name from
AndroidManifest.xml
file to change the directories name as well - Refactor the label name for your application
- Change the
rootProject.name
attribute in yoursettings.gradle
file
And that’s how you can copy an Android project and run it from Android Studio. Nice work! 👍