요즘은 개발 환경 세팅에만 성공해도 절반은 일을 다 한 듯한 느낌이 드는 것 같습니다ㅎㅎ
최근 리액트 네이티브 환경을 갖추고 빌드를 시도했는데 다양한 오류들을 만나 버렸습니다. 프레임워크의 업데이트가 잦다보니 이같은 안타까운 현실이 자주 만들어지는 것 같습니다.
1. ios 용 빌드시 아래와 같은 오류를 만났을 때 대응 방법입니다.
(void)[self _initializeModules:RCTGetModuleClasses() withDispatchGroup:prepareBridge lazilyDiscovered:NO];
^~~~~~~~~~~~~~~~~~~~~
/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:770:82: note: passing argument to parameter 'modules' here
- (NSArray<RCTModuleData *> *)_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules
^
/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:775:79: error: cannot initialize a parameter of type 'NSArray<Class> *' with an lvalue of type 'NSArray<id<RCTBridgeModule>> *__strong'
NSArray<RCTModuleData *> *moduleDataById = [self _registerModulesForClasses:modules lazilyDiscovered:lazilyDiscovered];
^~~~~~~
/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:617:76: note: passing argument to parameter 'moduleClasses' here
- (NSArray<RCTModuleData *> *)_registerModulesForClasses:(NSArray<Class> *)moduleClasses
^
/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:826:69: error: cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an lvalue of type 'NSArray<Class> *__strong'
NSArray<RCTModuleData *> *newModules = [self _initializeModules:modules withDispatchGroup:NULL lazilyDiscovered:YES];
^~~~~~~
/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:770:82: note: passing argument to parameter 'modules' here
- (NSArray<RCTModuleData *> *)_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules
^
3 errors generated.
_______________________________ 또는 _______________________________
/node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm:300:11: error: no matching function for call to 'RCTBridgeModuleNameForClass'
RCTBridgeModuleNameForClass(module));
^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm:17:
/ios/Pods/Headers/Public/React-Core/React/RCTLog.h:35:52: note: expanded from macro 'RCTLogError'
#define RCTLogError(...) _RCTLog(RCTLogLevelError, __VA_ARGS__)
^~~~~~~~~~~
/ios/Pods/Headers/Public/React-Core/React/RCTLog.h:127:74: note: expanded from macro '_RCTLog'
#define _RCTLog(lvl, ...) _RCTLogNativeInternal(lvl, __FILE__, __LINE__, __VA_ARGS__)
^~~~~~~~~~~
In file included from /node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm:8:
In file included from /node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.h:8:
In file included from /node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h:12:
/ios/Pods/Headers/Public/React-Core/React/RCTBridge.h:125:22: note: candidate function not viable: no known conversion from '__strong id<RCTTurboModule>' to '__unsafe_unretained Class' for 1st argument
RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
^
1 error generated.
[대응방법]
a. ios 경로의 Podfile을 여세요
vi ios/Podfile
b. 아래코드를 Podfile 상단에 추가 합니다. require_relative 문 아래에 두면 좋을 겁니다.
post_install do |installer|
## Fix for XCode 12.5
find_and_replace("../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm",
"_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules", "_initializeModules:(NSArray<Class> *)modules")
find_and_replace("../node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm",
"RCTBridgeModuleNameForClass(module))", "RCTBridgeModuleNameForClass(Class(module)))")
end
c. 아래코드를 Podfile 최하단에 추가합니다.
def find_and_replace(dir, findstr, replacestr)
Dir[dir].each do |name|
text = File.read(name)
replace = text.gsub(findstr,replacestr)
if text != replace
puts "Fix: " + name
File.open(name, "w") { |file| file.puts replace }
STDOUT.flush
end
end
Dir[dir + '*/'].each(&method(:find_and_replace))
end
d. ios 경로에서 아래 명령을 실행합니다
pod install
e. 빌드를 시도합니다
2. android 용 빌드시 아래와 같은 오류를 만났을 때 대응 방법입니다.
> Task :app:compileDebugJavaWithJavac FAILED : FAILURE: Build failed with an exception *What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > java.lang.IllegalAccessError: class org.gradle.internal.compiler.java.classNameCollector (in unmaed module @ ...)
[대응방법]
JDK를 찾지 못해 발생하는 오류입니다.
a. 아래 명령으로 java 경로를 확인합니다
% /usr/libexec/java_home -V
atching Java Virtual Machines (3):
1.8.301.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_211 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
적절한 경로를 선택해서 복사합니다.
b. 복사한 경로를 .zshrc 파일 또는 .bash_profile 에 아래와 같이 반영합니다.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
c. 환경변수를 반영합니다.
% source ~/.zshrc 또는, % source ~/.bash_profile
d. 빌드를 시도합니다.
건투를 빕니다 :)
<참고경로>
- https://mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/
- https://github.com/facebook/react-native/issues/28405