可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I would like to copy my android project and create a new project form 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 its for eclipse, how can I do this in Android Studio?
回答1:
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.
回答2:
If you use gradle - don't forget to change applicationId attribute in app/build.gradle file.
回答3:
As free3dom pointed out, here's what should be done
- Create a copy using file manager
- Manually edit the app's
build.gradle
to change package name (you can use the file manager)
- Manually edit
AndroidManifest.xml
to change package name
- Run gradle sync
- Open the project in android studio, refactor the package name
- Run gradle sync, again.
That seems to work without any problems
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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 '
回答8:
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 should be a comment, but don't have enough points for that.
回答9:
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.
回答10:
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
回答11:
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!
回答12:
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 and right mouse button click (RMB) on: app.java/< your old project name (not the test one, not the androidTest 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).