Copy existing project with a new name in Android Studio - android-studio

I would like to copy my Android project and create a new project from the same files just with a different name. The purpose of this is so I can have a second version of my app which is ad supported in the app store.
I found this answer here:
Android - copy existing project with a new name
But it's for Eclipse. How can I do this in Android Studio?

The steps in the link you specified should also work for Android Studio. Just make a copy (using a file manager) of the entire module folder and give it a new name. Now open it up and use Refactor -> Rename (right click on the item you want to rename) to rename your module and package.
See this for details about refactoring in IntelliJ/Android Studio.

If you are using the newest version of Android Studio, you can let it assist you in this.
Note: I have tested this in Android Studio 3.0 only.
The procedure is as follows:
In the project view (this comes along with captures and structure on the left side of screen), select Project instead of Android.
The name of your project will be the top of the tree (alongside external libraries).
Select your project then go to Refactor -> Copy....
Android Studio will ask you the new name and where you want to copy the project. Provide the same.
After the copying is done, open your new project in Android Studio.
Packages will still be under the old project name.
That is the Java classes packages, application ID and everything else that was generated using the old package name.
We need to change that.
In the project view, select Android.
Open the java sub-directory and select the main package.
Then right click on it and go to Refactor then Rename.
Android Studio will give you a warning saying that multiple directories correspond to the package you are about to refactor.
Click on Rename package and not Rename directory.
After this step, your project is now completely under the new name.
Open up the res/values/strings.xml file, and change the name of the project.
Don't forget to change your application ID in the "Gradle Build Module: app".
A last step is to clean and rebuild the project otherwise when trying to run your project Android Studio will tell you it can't install the APK (if you ran the previous project).
So Build -> Clean project then Build -> Rebuild project.
Now you can run your new cloned project.

If you use Gradle - don't forget to change applicationId attribute in app/build.gradle file.

As free3dom pointed out, here's what should be done:
Create a copy using file manager
Manually edit the app's build.gradle file to change the package name (you can use the file manager).
Manually edit AndroidManifest.xml to change the package name.
Run gradle sync.
Open the project in Android Studio, and refactor the package name.
Run gradle sync, again.
That seems to work without any problems.

This is a combination nt.bas's answer and step 9 of Civic's answer with visual examples because it took me a while to find out what was intended since I am new to Android Studio. It has been tested in Android Studio 3.2.1.
Open the project you want to clone in Android Studio. (In this example, the old project name was test5 and the new project name was test6)
In the left file-overview pane, click: Project (where it might currently say android).
Right mouse button click on the project within the file explorer pane and click refactor>clone.
Change the "New name" to your new project name and click ok.
File>open>New window>Select your new project>Open in new project window. In the new window, wait until the bottom line of Android studio is finished/says:"Gradle Sync Finished".
In the file overview pane: right mouse button click (RMB) on: app.java/< your old project name> (not the com.example.<your old project name>(androidTest) one, not the com.example.<your old project name>(test) one, just the blank one)
Enter the new name of your package and select both checkmarks, click refactor.
In the bottom left bar click "Do refactor".
Open app/res/values/strings.xml and change name of the old project (e.g. test5) to the new name of the project in line:
<string name="app_name">test5</string>
Open Gradle scripts/build.gradle (Module:app) and change the line to the same line with your new project name:%fig4
applicationId "com.example.a.test5"
A yellow line will appear at the top of your code pane, requesting gradle sync. Press "sync now".
in top bar, press build>Clean project.
If it says "Gradle build finished" in the bottom left, you click "Build>Rebuild project".
Now you should be able to compile and run your project again (if it worked in the first place).

The purpose of this is so I can have a second version of my app which is ad supported in the app store.
Currently the best way to do it is without copying the project.
You can do it using diffent flavors in your build.gradle file.
productFlavors {
flavor1 {
applicationId = "com.example.my.pkg.flavor1"
}
flavorAdSUpport {
applicationId = "com.example.my.pkg.flavor2"
}
}
In this way you have only a copy of the files and you can handle the difference easily.

I'm following these steps and it's been working so far:
Copy and paste the folder as used to.
Open Android Studio (v3.0.1).
Select Open an existing Project.
Close the message that will pop up with title: "Import Gradle Projects".
At left side on Android Tab go to: app -> java -> select the first folder (your project folder)
Refactor => Rename... (Shift + F6)
Rename Package, Select both options - Put the new folder's name in lowercase.
Do Refactor
Select: Sync Project with Gradle Files at toolbar.
Build => Clean Project
Go to app -> res -> values -> strings.xml, and change the app name at 2nd line.

In Android Studio 4.0 you need only these few steps:
in File Manager copy the project directory and rename the new one
enter in it and change applicationId inside app/build.gradle
open the existing new project in Android Studio
open one class file and highlight the package name part to change (e.g. from com.domain.appname to com.domain.newappname highlight appname)
right click on it -> "refactor" -> "rename"
choose "rename package"
in the dialog choose "Scope: all places" and click "preview" or "refactor"

The appendix of the Android Developer Fundamentals Course Practicals gitbook includes steps to copy and rename an existing project:
https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-practicals/content/en/appendix_utilities.html#copy_project

I've tried from nt.bas answer and gnyrfta answer which works well for me.
Quoting from nt.bas answer:
If you are using the newest version of Android Studio, you can let it assist you in this.
Note: I have tested this in Android Studio 3.0 only.
The procedure is as follows:
In the project view (this comes along with captures and structure on the left side of screen), select Project instead of Android.
The name of your project will be the top of the tree (alongside external libraries).
Select your project then go to Refactor -> Copy....
Android Studio will ask you the new name and where you want to copy the project. Provide the same.
After the copying is done, open your new project in Android Studio.
Packages will still be under the old project name.
That is the Java classes packages, application ID and everything else that was generated using the old package name.
We need to change that.
In the project view, select Android.
Open the java sub-directory and select the main package.
Then right click on it and go to Refactor then Rename.
Android Studio will give you a warning saying that multiple directories correspond to the package you are about to refactor.
Click on Rename package and not Rename directory.
After this step, your project is now completely under the new name.
Open up the res/values/strings.xml file, and change the name of the project.
A last step is to clean and rebuild the project otherwise when trying to run your project Android Studio will tell you it can't install the APK (if you ran the previous project).
So Build -> Clean project then Build -> Rebuild project.
Up to this point you only rename your whole project name. To rename packaging name you need to follow gnyrfta answer which was described as:
When refactoring the package name in Android Studio, you may need to click the little cogwheel up to the right by the package/android/project/etc - navigator and uncheck 'compact empty middle packages' in order to see each part of the package name as an own directory. Then for individual directories do refactor.
PS: If you're having an
Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split
lib_slice_0_apk was defined multiple times
Just delete build folder of appmodule and Rebuild the project!
This will fix the issue!

Go to the source folder where your project is.
Copy the project and past and change the name.
Open Android Studio and refresh.
Go to ->Settings.gradle.
Include ':your new project name '

When refactoring the package name in Android Studio, you may need to click the little cogwheel up to the right by the package/android/project/etc - navigator and uncheck 'compact empty middle packages' in order to see each part of the package name as an own directory. Then for individual directories do refactor.
This is important if you need to change all parts of the package name. For example, from com.example.originalproject to org.mydomain.newproject. Otherwise, the refactor/rename operation will only let you change "originalproject" to "newproject", and it will leave "com.example" unchanged. There is a good video that shows this: https://www.youtube.com/watch?v=dMK-RBVLeIY

Perhaps this will help someone.
For Android Studio 4.x Projects, you need following steps:
copy project directory to new project directory
from Android Studio, open new project directory
edit settings.gradle file by updating the rootProject.name='newProjectName'.
then sync gradle
and here you go the project is ready, and you can start updating manifest, packages, google-services.json and all other stuff

When you copy your project you will also need to delete the original remnant intermediate build (someActivity$4.class) files from the C:...\AndroidStudioProjects(project_name)\app\build\intermediates\classes\release... directories.
Otherwise you will almost certainly have build failures for the new project
if yo attempt to compile the copied project. Refactoring won't solve this.

I'm using Android 3.3 and that's how it worked for me:
1 - Choose the project view
2 - Right click the project name, which is in the root of the project and choose the option refactor -> copy, it will prompt you with a window to choose the new name.
3 - After step 2, Android will make a new project to you, you have to open that new project with the new name
4 - Change the name of the app in the "string.xml", it's in "app/res/values/string.xml"
Now you have it, the same project with a new name. Now you may want to change the name of the package, it's described on the followings steps
(optional)
To change the name of the package main
5 - go to "app/java", there will be three folders with the same name, a main one, an (androidTest) and a (test), right click the main one and choose format -> rename, it will prompt you with a warning that multiple directories correspond to that package, then click "Rename package". Choose a new name and click in refactor. Now, bellow the code view, here will be a refactor preview, click in "Do refactor"
6 - Go to the option "build", click "Clean project", then "Rebuild project".
7 - Now close the project and reopen it again.

Requirement and test on Android Studio 3.5
Make sure your old project working properly with your existing android studio library.
Copy project directory and rename folder for new project name e.g. Bramara
Open your existing new project using Android Studio 3.5. After opening project complete, Navigate to 1: Project -> Project. You may seen your project only has two directory pointing to new project folder and old project folder. Close your project.
Edit appl.iml on directory new project -> app -> appl.iml using text editor. Replace all old project name into new Project Name.
Reopen your exising new project. Navigate to 1: Project -> Project. You may seen your project only has one directory.
Navigate to 1: Project -> Packages. right click on your component -> Refactor -> Rename.
A Warning message will pop up. Make sure all change will apply to new project folder! After that choose Rename packages e.g. com.dedetok.bramara.
Navigate to 1: Project -> Android. Open app -> manifests -> AndroidManifest.xml. Fix Application Activity to new pakage name. Change your Application Name to a new one.
Open Gradle Scripts -> build.gradle (Module: app), change your applicationId to new project, e.g com.dedetok.bramara and sync project.
Clean and rebuild your new project.
Your new project is ready to edit/change.
Note: if adb run showing activity not found, edit your Run/Debug Configuration. Module should point to module application e.g. app.

As of February 2020, for Android Studio 3.5.3, the simplest answer I found is this video.
Note 1: At 01.24 "Find" tab appears below. Click "Do Refactor" and continue as in the video.
Note 2: If you have any Java/Kotlin files "Marked as Plain Text" you need to modify the package name at the top manually, i.e. package com.example.thisplaceneedstobemanuallyupdated
Note 3: Be careful about letter cases while renaming, just as in the video.
Note 4: If you want to update the project name on title bar of project window, modify rootProject.name = 'YourProjectName' inside "settings.gradle" file under "Gradle Scripts" directory.

The EASIEST (and definitely the quickest) way to do requires WINRAR, 7zip or similar archiving software:
Find the project folder in windows explorer - double click to open this folder.
Create a new folder and name it "Backup."
While still in the project folder, select all files / folders, except the "backup" folder.
Right-click and select "add to archive" or "create archive" (command will be different depending on your archiving software)
Name the archive and click ok.
Move this archive to the "Backup" folder.
You're Done - to open the backup archive, open "Backup" folder and right-click on the backup file. Select "Extract" or create a new folder to which the files will be extracted and hit "ok" then open the project as you normally would from Android Studio, etc.

I had problems with this following:
https://google-developer-training.github.io/android-developer-fundamentals-course-concepts-v2/appendix/appendix-utilities/appendix-utilities.html
on Android Studio version: 3.3.2
until I killed the .idea/workspace.xml file.
$ cp -rv Testcopysource/ TestCopyDest
$ rm TestCopyDest/.idea/workspace.xml
$ stdio.sh & # Run Android Studio on Linux
Prior to doing that Android Studio would still point to the original source folder and all renames were applied to the original source files (within Testcopysource in my example above).

In android studio 4.1.1:
Step 1
You copy the project in the file explorer and give it a new name.
Step 2
Open the copied project in the android studio and go to the Gradle Scrips files and change the name of the project to the new name in the settings and build files.
Step 3
Go to the properties Gradle file and add the line:
android.overridePathCheck=true

The simplest way would be to upload the project files to a Github repository and cloning or downloading the repo again to your computer

Related

Android Studio: file-> "Project Structure" missing options to change application id, version, version name, etc

So every time i updated my app, i use file -> project structure to increase the version and put version name. I also use it to name application id when i want to fork my codes into different app.
but today I can't find application id, version, version name, etc on file-> "Project Structure".
a few months ago i also got this problem and the options suddenly come back by itself.
so I don't know how to fix it. I need those options...
What I've tried
loading old projects
refreshing Gradle files and sync
rebuilding the project
If the "Sync Project with Gradle" does not appear, open the Android folder separately.i.e like an independent project. File->Open. In my case, I was working on a Flutter/Kotlin project. When I opened the Android folder it automatically started syncing and downloading the required gradle files.
oh my God, fixed it by
File > Sync Project with Gradle
File > project structure > modules (will still be blank)
File > project structure > suggestion -> click APP (bottom right will say "loading" with tiny fonts)
File > project structure > modules (now the options will appear)
I don't see "Sync Project with Gradle", but I saw "sync with file system". This worked once, but now Project structure is gone again. Clicking "sync with file system" doesn't work anymore.
Try the keyboard shortcut, for example on Windows is "ctrl+alt+shift+S"
I fixed by Open module settings (Right-click the project -> Open module settings) change some value and apply it
then the project structure is showing up

How to add a new directory/folder in a package in Android Studio

I'm trying to simply add a new directory in a package in my Android Studio project (I'm new to AS and Android dev). I want to categorize some java files and my hierarchy is: app/src/main/java/com.company.foo.bar and I want to add a folder under bar. I right-clicked on com.company.foo.bar and selected New->Folder->Java Folder (I took a guess that this was the correct way). After doing so, a dialog popped up with no way to even name the new folder, so I clicked "Finish" to see what would happen, and it just started doing a gradle build and whirled around for over 15 minutes. I canceled but I think it might have messed something up. There's no new folder.
What is the correct way to simply add a new directory to a package in Android Studio?
Right-click an existing package and choose New -> Package on Android Studio 3.2.1.
Here is a screenshot:
I was able to resolve this with trial-by-error. I just right-clicked on the Java folder, selected New->Java Class, then in the dialog, typed the package name that already exists and added an extra name for the namespace. This generated the folder/directory/package or whatever you want to call it. Android Studio is so convoluted. There's a New Folder, New Directory, New Package, that's not really apparent at what it does until you try it (and mess up your project). I tried New Folder and I have no idea what it did other than kicking off a build that lasted 15 minutes. Ugh.

New Directory vs New Folder in Android Studio

What is the difference between creating a new directory and creating a new folder in Android Studio?
Here is an image of the menu choices:
Short answer
Use folder when adding a folder to an Android Studio project. This is what Android Studio calls them. Directory is what IntelliJ calls a folder. Android Studio is built on top IntelliJ so there is some naming confusion.
Long answer
Although there is a technical difference between a folder and a directory (see here and here), they are often (correctly or incorrectly) used interchangeably. This can be seen even in the titles of these two SO questions:
Setting a custom assets directory for unit testing in Android Studio
Adding an assets folder in Android Studio
This can be confusing to new users of Android Studio when they see both choices in the menu structure. Further confusion results when users add a new directory and then it doesn't show up in the Android folder view. See these questions for example:
Android Studio: Newly Created Directory Not Appearing In Folders View
New created "values" folder is not visible in Android Studio
The folders actually do show up if you select the Project or Packages view from the menu:
However if you want an Android folder to show up automatically in the Android view you need to right click and use New > Folder > Assets folder (if you are adding an assets folder). You could then add a subfolder like "fonts" to this by choosing the New > Directory option.
Android Studio is built on top of IntelliJ IDEA. IntelliJ itself has a menu option to add a directory (as you can see here). But Android stores files in what it calls folders. So when these two platforms are put together in Android Studio, there gets to be some naming confusion. (This is my interpretation, but perhaps they really are trying to differentiate the subtle differences between directory and folder.)
You can see from your menu image that there is also an "Android resource directory" option. (You got that by right clicking on the res directory/folder rather than a subdirectory.) You would use this to add resource folders such as raw, menu, values, etc.

Project Name in Android Studio Does Not Change

I followed the instructions here Change project name on Android Studio and while my project name in the project explorer changed, the project name in the top IDE bar does not(the very top left of the IDE window). I renamed the project by right clicking the project name, updated the manifest's app_name and package name.
This is a bug where some pointers in the duplicate project still point to the original project. I experience this only when using "Open Project" to bring the duplicate up in AS. This is solved by using the "Import Project" instead and then refactoring and renaming accordingly.
This brings up another issue, however. While the naming is correct, something is still pointing to the original, because launching either the original or the duplicate on the device replaces the other on the home screen.
With Android Studio off, I renamed the project folder in Explorer and restarted Android Studio and imported project. Now this created another .iml in the project folder so I went ahead and deleted the old .iml .
(it seems to have correctly changed the app.iml in src also)
I hope this is all there is to it.
this work for me:
Close android studio
Change project root directory name
Open android studio
Open the project(not from local history but by browsing to it)

Android studio no main activity, no layouts on a new project

I installed new Android Studio. I created new Project and asked the wizard to create main activity. But there were no layouts, no main activity class, no nothing. Error on trying to make new Java class files. The new project tree looks like this:
No (main/java) folder.
No (res/layout) folder.
Other words project is useless.
From what i learned is that you may be picking the "no layout" option when prompted to pick one. Try picking the basic activity and the rest of the files will show
After creating the project, you need to go to File menu and then Import Project. Then browse to the location of your project and import it. Then you will see all of these files and folders. I had this problem too, and it seems to be something that they overlooked in the newer versions of Android Studio (which is quite pathetic if you ask me).
Empty Activity
Choose "Empty Activity" instead of "No Activity",
while creating a NEW PROJECT in Android Studio.
I don't know if anyone is having the same problem right now but i solved this issue by changing the Android Gradle plugin version and the Gradle version.
For doing this you have to open the project you're having issues with and click on
File->Project Structure->Project
and there you will see the two options.
Android Gradle Plugin Version 4.0.0 and Gradle version 6.1.1 worked for me. After that I went to the file directory, right click on the package folder "com.example.myProject" and manually created an empty activity. The layout folder with my new activity was created automatically after that.
I found it's because I changed the Java runtime version before through the Android Studio plugin Choose Runtime. I changed to JDK 8 from JDK 11 then the problem occured. It worked for me to change back to JDK 11 through plugin Choose Runtime.
Try
Typing main activity (without quotes) in the search box and
Press enter.
For me it got restored. Search window is useful in finding many hidden tools quickly.
I created a new activity by right clicking on java then selected activity and the layout file was automatically written
When you open a new project, you can't select "empty project" you have to select and activity project. I just went through this had to delete my project and start a new one now everythings there.

Resources