IT story

React Native에서 Android의 패키지 이름 변경

hot-time 2020. 7. 3. 18:10
반응형

React Native에서 Android의 패키지 이름 변경


내가 사용하는 react-native init MyApp새로운 네이티브 응용 프로그램 반응 초기화 할 수 있습니다. 이것은 패키지와 함께 안드로이드 프로젝트를 만들었습니다 com.myapp.

이 패키지 이름을 변경하는 가장 좋은 방법은 무엇입니까 com.mycompany.myapp? 예를 들면 다음과 같습니다. ?

변경하려고 시도했지만 AndroidManifest.xml다른 오류가 발생했기 때문에 그렇지 않다고 가정합니다.

어떤 생각?


프로젝트의 하위 폴더 이름을 " android / app / src / main / java / MY / APP / OLD_ID / "에서 " android / app / src / main / java / MY / APP / NEW_ID / "로 변경했습니다.

그런 다음 이전 및 새 패키지 ID를 수동으로 전환하십시오.

: android / app / src / main / java / MY / APP / NEW_ID / MainActivity.java :

package MY.APP.NEW_ID;

에서 안드로이드 / 응용 프로그램 / SRC / 메인 / 자바 / 내 / APP / NEW_ID / MainApplication.java :

package MY.APP.NEW_ID;

에서 안드로이드 / 응용 프로그램 / SRC / 주 /의 AndroidManifest.xml :

package="MY.APP.NEW_ID"

그리고 android / app / build.gradle에서 :

applicationId "MY.APP.NEW_ID"

( 선택 사항 ) android / app / BUCK에서 :

android_build_config(
  package="MY.APP.NEW_ID"
)
android_resource(
  package="MY.APP.NEW_ID"
)

Gradle은 결국 / android 폴더 에서 청소 합니다.

./gradlew clean

내가 사용하는 반응 - 기본 - 이름 바꾸기 NPM 패키지를.

사용하여 설치

npm install react-native-rename -g

그런 다음 React Native 프로젝트의 루트에서 다음을 실행하십시오.

react-native-rename "MyApp" -b com.mycompany.myapp

npm의 반응 네이티브 이름 바꾸기


패키지 이름을 com.myapp에서 com.mycompany.myapp (예 :)로 변경하려면,

  1. 반응 앱의 iOS 앱의 경우 일반적으로 xcode를 사용하십시오.
  2. Android 앱의 경우 모듈 수준에서 build.gradle을 엽니 다. android / app 폴더에있는 것 당신은 발견 할 것이다

    ... defaultConfig { applicationId com.myapp ... } ...

"com.myapp"를 필요한 것으로 변경하십시오.

도움이 되었기를 바랍니다.


react-native-rename npm 패키지를 사용하면됩니다.

사용하여 설치

npm install react-native-rename -g

그런 다음 React Native 프로젝트의 루트에서 다음을 실행하십시오.

react-native-rename "MyApp" -b com.mycompany.myapp

react-native-rename on npm

그러나이 lib는 MainActivity.java및을 제거합니다 MainApplication.java. 패키지 이름을 변경하기 전에이 두 파일에서 백업을 제공하고 패키지 이름을 변경 한 후 다시 원래 위치에 두십시오. 이 솔루션은 나를 위해 작동

추가 정보 : 반응 네이티브 이름 바꾸기


Android Studio를 사용하는 경우

변화 com.myappcom.mycompany.myapp

  1. 아래에 새 패키지 계층 구조 com.mycompany.myapp를 작성하십시오. android/app/src/main/java

  2. Android Studio GUI com.myappcom.mycompany.myapp사용하여 모든 클래스를 복사

  3. 안드로이드 스튜디오는 모든 카피 된 클래스에 적합한 패키지 이름을 지정합니다. 이것은 일부 사용자 정의 모듈이 있고 모든 .java 파일에서 수동으로 바꾸고 싶지 않은 경우에 유용합니다.

  4. 업데이트 android/app/src/main/AndroidManifest.xmlandroid/app/build.gradle(대체 com.myappcom.mycompany.myapp

  5. 프로젝트 동기화 (그라 데 빌드)


Goto Android 스튜디오

패키지를 마우스 오른쪽 버튼으로 클릭하십시오 (대부분 com)-> Refractor-> Rename-> 대화 상자에서 새 패키지 이름 입력-> Refractor

어디서나 패키지 이름을 바꿉니다.


초기화 스크립트는 준 이름 (예를 기반으로 안드로이드에 대한 고유 식별자 생성 com.acmeapp을위한을 AcmeApp).

에서 applicationId 키를 찾아 어떤 이름이 생성되었는지 확인할 수 있습니다 android/app/build.gradle.

해당 고유 식별자를 변경해야하는 경우 아래 설명 된대로 지금 수행하십시오.

In the /android folder, replace all occurrences of com.acmeapp by com.acme.app

Then change the directory structure with the following commands:

mkdir android/app/src/main/java/com/acme

mv android/app/src/main/java/com/acmeapp android/app/src/main/java/com/acme/app

You need a folder level for each dot in the app identifier.

Source: https://blog.elao.com/en/dev/from-react-native-init-to-app-stores-real-quick/


I have a solution based on @Cherniv's answer (works on macOS for me). Two differences: I have a Main2Activity.java in the java folder that I do the same thing to, and I don't bother calling ./gradlew clean since it seems like the react-native packager does that automatically anyways.

Anyways, my solution does what Cherniv's does, except I made a bash shell script for it since I'm building multiple apps using one set of code and want to be able to easily change the package name whenever I run my npm scripts.

Here is the bash script I used. You'll need to modify the packageName you want to use, and add anything else you want to it... but here are the basics. You can create a .sh file, give permission, and then run it from the same folder you run react-native from:

rm -rf ./android/app/src/main/java


mkdir -p ./android/app/src/main/java/com/MyWebsite/MyAppName
    packageName="com.MyWebsite.MyAppName"
    sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/Main2Activity.java
    sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/MainActivity.java
    sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/MainApplication.java
    sed -i '' -e "s/.*package=\".*/    package=\""$packageName"\"/" ./android/app/src/main/AndroidManifest.xml
    sed -i '' -e "s/.*package = '.*/  package = '"$packageName"',/" ./android/app/BUCK
    sed -i '' -e "s/.*applicationId.*/    applicationId \""$packageName"\"/" ./android/app/build.gradle

    cp -R ./android/app/src/main/javaFiles/ ./android/app/src/main/java/com/MyWebsite/MyAppName

DISCLAIMER: You'll need to edit MainApplication.java's comment near the bottom of the java file first. It has the word 'package' in the comment. Because of how the script works, it takes any line with the word 'package' in it and replaces it. Because of this, this script may not be future proofed as there might be that same word used somewhere else.

Second Disclaimer: the first 3 sed commands edit the java files from a directory called javaFiles. I created this directory myself since I want to have one set of java files that are copied from there (as I might add new packages to it in the future). You will probably want to do the same thing. So copy all the files from the java folder (go through its subfolders to find the actual java files) and put them in a new folder called javaFiles.

Third Disclaimer: You'll need to edit the packageName variable to be in line with the paths at the top of the script and bottom (com.MyWebsite.MyAppName to com/MyWebsite/MyAppName)


Follow the simple steps to rename your app name and Package name in case you have not made any custom changes in android folder(ie. scenario where u just initialized the project)

  1. Simply change the name in the package.json file as you need and save it.
  2. Remove the folder named android
  3. run the command react-native upgrade

Note:As ivoteje50 mentioned in comment,Don't remove the android folder if you have already followed the official instructions to generate a keystore, since it will remove the keystore and you cannot sign a new app again.


In VS Code, press Ctrl + Shift + F and enter your old package name in 'Find' and enter your new package in 'Replace'. Then press 'Replace all occurrences'.

Definitely not the pragmatic way. But, it's done the trick for me.


Go to Android Studio, open app, right click on java and choose New and then Package.

Give the name you want (e.g. com.something).

Move the files from the other package (you want to rename) to the new Package. Delete the old package.

Go to your project in your editor and use the shortcut for searching in all the files (on mac is shift cmd F). Type in the name of your old package. Change all the references to the new package name.

Go to Android Studio, Build, Clean Project, Rebuild Project.

Done!

Happy coding :)


Go to file android/app/src/main/AndroidManifest.xml -

Update :

attr android:label of Element application as :

Old value - android:label="@string/app_name"

New Value - android:label="(string you want to put)"

and

attr android:label of Element activity as :

Old value - android:label="@string/app_name"

New Value - android:label="(string you want to put)"

Worked for me, hopefully it will help.

참고URL : https://stackoverflow.com/questions/37389905/change-package-name-for-android-in-react-native

반응형