简体   繁体   中英

Rename Android Studio package error: Not a valid identifier name

Renaming from com.example.test to org.example.test: I have only found examples of changing the example part of this, but I need to change the com one.

I have tried Refactor then select Rename package, but I get the message Not a valid identifier name when I enter org.example.test.

Does anyone know the correct way?

The accepted solution did not work for me. I had to do the following:

  1. Create a new package with the desired name (eg. org.example.test)
  2. Move the files from the old package to the new one [android studio correct the package name automatically]
  3. In AndroidManifest.xml, change package="..." to the new one (eg. org.example.test) [maybe this step is not obligatory]
  4. Compile. You will see errors like imports to the old package. Simply correct them one by one [I just found one error]

That's all. It run perfectly after that!

I've had to do the same a few months ago. As far as I know, Android Studio does not bound java packages with the app package. That said, you may have to change your app package (if that's what you want) and your java package.

First, your Java package

Select the 1. Project tab in Android Studio. There's a Settings button. Click on it and check Flatten Packages . Then, select the package you want to rename. Press Shift + F6 to rename the whole package. If you have more than one package inside com.example.test , Android Studio will warn you "Multiple directories correspond to package..." and ask if you want to rename package or directory. Choose package and it will do its magic.

Unless you have something else other than test under com.example , you're good to go.

Then, your Android app

For this one, I don't know any shortcuts. But if it helps, the app package is mentioned in AndroidManifest.xml and build.gradle for your module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM