简体   繁体   English

重命名Android Studio包错误:不是有效的标识符名称

[英]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. 从com.example.test重命名为org.example.test:我只找到了更改示例部分的示例 ,但我需要更改com

I have tried Refactor then select Rename package, but I get the message Not a valid identifier name when I enter org.example.test. 我已经尝试了Refactor然后选择重命名包,但是当我输入org.example.test时,我收到消息Not a valid identifier name

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) 创建一个具有所需名称的新包(例如org.example.test)
  2. Move the files from the old package to the new one [android studio correct the package name automatically] 将文件从旧包移动到新包[android studio自动更正包名]
  3. In AndroidManifest.xml, change package="..." to the new one (eg. org.example.test) [maybe this step is not obligatory] 在AndroidManifest.xml中,将package =“...”更改为新的(例如org.example.test)[也许这一步不是强制性的]
  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. 据我所知,Android Studio没有将Java包与app包绑定。 That said, you may have to change your app package (if that's what you want) and your java package. 也就是说,您可能需要更改您的应用程序包(如果这是您想要的)和您的Java包。

First, your Java package 首先,您的Java包

Select the 1. Project tab in Android Studio. 在Android Studio中选择1.项目选项卡。 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. Shift + F6重命名整个包。 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. 如果com.example.test有多个包,Android Studio会警告您“多个目录对应于包...”并询问您是否要重命名包或目录。 Choose package and it will do its magic. 选择包装,它将发挥其魔力。

Unless you have something else other than test under com.example , you're good to go. 除非你在com.exampletest以外的其他东西,否则你很高兴。

Then, your Android app 然后,你的Android应用程序

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. 但如果它有帮助, build.gradle中的AndroidManifest.xmlbuild.gradle会提到应用程序包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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