Android copy existing project with new name in And

2019-01-10 06:52发布

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?

12条回答
欢心
2楼-- · 2019-01-10 07:09

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.

查看更多
SAY GOODBYE
3楼-- · 2019-01-10 07:10

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:

  1. 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.

  2. 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.

  3. Open up the res/values/strings.xml file, and change the name of the project.
  4. 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!

查看更多
Lonely孤独者°
4楼-- · 2019-01-10 07:13

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.

查看更多
Ridiculous、
5楼-- · 2019-01-10 07:14

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.

  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)

  2. In the left file-overview pane, click: Project (where it might currently say android).

fig0

  1. Right mouse button click on the project within the file explorer pane and click refactor>clone.

fig1

  1. Change the "New name" to your new project name and click ok.

fig2

  1. 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".

16

17

14

  1. 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)>

fig8

  1. Enter the new name of your package and select both checkmarks, click refactor.

fig9 fig10

  1. In the bottom left bar click "Do refactor".

fig12

  1. 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>
    

fig3

  1. 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"    
    

fig4

  1. A yellow line will appear at the top of your code pane, requesting gradle sync. Press "sync now".

fig5

  1. in top bar, press build>Clean project.

fig13

  1. If it says "Gradle build finished" in the bottom left, you click "Build>Rebuild project".

fig14

fig15

  1. Now you should be able to compile and run your project again (if it worked in the first place).
查看更多
在下西门庆
6楼-- · 2019-01-10 07:17

As free3dom pointed out, here's what should be done

  1. Create a copy using file manager
  2. Manually edit the app's build.gradle to change package name (you can use the file manager)
  3. Manually edit AndroidManifest.xml to change package name
  4. Run gradle sync
  5. Open the project in android studio, refactor the package name
  6. Run gradle sync, again.

That seems to work without any problems

查看更多
趁早两清
7楼-- · 2019-01-10 07:17

Go to the source folder where your project is.

  1. Copy the project and past and change the name.
  2. Open android studio and refresh.
  3. Go to ->Settings.gradle.
  4. Include ':your new project name '
查看更多
登录 后发表回答