JaneaSystems / nodejs-mobile-samples

Repository for demo applications that use Node.js on Mobile
MIT License
168 stars 111 forks source link

cordova run android leads to error: requires unsupported dynamic reloc R_ARM_REL32; #12

Closed tsinghoo closed 5 years ago

tsinghoo commented 5 years ago

When I executed "cordova run android" in macos to build "Cordova Use Native Library" samples, I got the following error, any advice?

/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/arm-linux-androideabi-clang++ '-DNODE_GYP_MODULE_NAME=sha3' '-DUSING_UV_SHARED=1' '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-DNODE_ENGINE="v8"' '-DNODE_ENGINE_V8' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' '-D_GLIBCXX_USE_C99_MATH' -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/include/node -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/src -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/deps/uv/include -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/deps/v8/include -I../../nan -Wall -Wextra -Wno-unused-parameter -O3 -fno-omit-frame-pointer -fno-rtti -fno-exceptions -std=gnu++0x -MMD -MF ./Release/.deps/Release/obj.target/sha3/src/KeccakNISTInterface.o.d.raw -c -o Release/obj.target/sha3/src/KeccakNISTInterface.o ../src/KeccakNISTInterface.cpp /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/arm-linux-androideabi-clang++ '-DNODE_GYP_MODULE_NAME=sha3' '-DUSING_UV_SHARED=1' '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-DNODE_ENGINE="v8"' '-DNODE_ENGINE_V8' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' '-D_GLIBCXX_USE_C99_MATH' -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/include/node -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/src -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/deps/uv/include -I/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode/deps/v8/include -I../../nan -Wall -Wextra -Wno-unused-parameter -O3 -fno-omit-frame-pointer -fno-rtti -fno-exceptions -std=gnu++0x -MMD -MF ./Release/.deps/Release/obj.target/sha3/src/KeccakSponge.o.d.raw -c -o Release/obj.target/sha3/src/KeccakSponge.o ../src/KeccakSponge.cpp /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/arm-linux-androideabi-clang++ -shared -rdynamic -fPIC -Wl,-soname=sha3.node -o Release/obj.target/sha3.node -Wl,--start-group Release/obj.target/sha3/src/addon.o Release/obj.target/sha3/src/displayIntermediateValues.o Release/obj.target/sha3/src/KeccakF-1600-reference.o Release/obj.target/sha3/src/KeccakNISTInterface.o Release/obj.target/sha3/src/KeccakSponge.o -Wl,--end-group -llog /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/src/com/janeasystems/cdvnodejsmobile/jni/libnode//bin/armeabi-v7a/libnode.so /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: Release/obj.target/sha3/src/addon.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: Release/obj.target/sha3/src/addon.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: Release/obj.target/sha3/src/addon.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC

jaimecbernardo commented 5 years ago

Hi @tsinghoo , I've been unable to reproduce this build error. Might it be related to you Android development versions? What's your Android SDK, NDK, CMake versions? Do you have some instructions you could share for trying to reproduce it?

tsinghoo commented 5 years ago

Thanks for your reply, What is the preferred Android SDK, NDK, CMake versions?

Please check the following for what I am using:

Android Studio 3.2.1 Build #AI-181.5540.7.32.5056338, built on October 9, 2018 JRE: 1.8.0_152-release-1136-b06 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.12.6

Android SDK Tools 25.2.5 Android Sdk Build tools 28.0.3 CMake 3.6.4111459 NDK 18.1.5063045 node v8.6.0

tsinghoo commented 5 years ago

The following is my commands and outputs:

$ cordova prepare Discovered platform "android@^6.3.0" in config.xml or package.json. Adding it to the project Using cordova-fetch for cordova-android@^6.3.0 Adding android project... Creating Cordova project for the Android platform: Path: platforms/android Package: com.example.usenativemodules Name: UseNativeModules Activity: MainActivity Android target: android-26 Subproject Path: CordovaLib Android project created with cordova-android@6.4.0 Discovered platform "ios@^4.5.4" in config.xml or package.json. Adding it to the project Using cordova-fetch for cordova-ios@^4.5.4 Adding ios project... Creating Cordova project for the iOS platform: Path: platforms/ios Package: com.example.usenativemodules Name: UseNativeModules iOS project created with cordova-ios@4.5.5 Discovered plugin "cordova-plugin-whitelist" in config.xml. Adding it to the project Installing "cordova-plugin-whitelist" for android

           This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Installing "cordova-plugin-whitelist" for ios Discovered plugin "nodejs-mobile-cordova" in config.xml. Adding it to the project Installing "cordova-plugin-whitelist" for android

           This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Installing "cordova-plugin-whitelist" for ios Discovered plugin "nodejs-mobile-cordova" in config.xml. Adding it to the project Installing "nodejs-mobile-cordova" for android Subproject Path: CordovaLib Installing "nodejs-mobile-cordova" for ios mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ npm run prepare-nodejs

com.example.usenativemodules@1.0.0 prepare-nodejs /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules cd www/nodejs-project && npm install

sha3@1.2.2 install /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/www/nodejs-project/node_modules/sha3 node-gyp rebuild

gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR CXX(target) Release/obj.target/sha3/src/addon.o CXX(target) Release/obj.target/sha3/src/displayIntermediateValues.o CXX(target) Release/obj.target/sha3/src/KeccakF-1600-reference.o CXX(target) Release/obj.target/sha3/src/KeccakNISTInterface.o CXX(target) Release/obj.target/sha3/src/KeccakSponge.o SOLINK_MODULE(target) Release/sha3.node

sqlite3@4.0.1 install /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/www/nodejs-project/node_modules/sqlite3 node-pre-gyp install --fallback-to-build

node-pre-gyp WARN Using request for node-pre-gyp https download [sqlite3] Success: "/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/www/nodejs-project/node_modules/sqlite3/lib/binding/node-v57-darwin-x64/node_sqlite3.node" is installed via remote npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN nodejs-mobile-use-native-modules-project@0.1.0 No repository field.

added 69 packages in 18.545s mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ ln -s /u/data/software/develop/java/android-sdk-macosx/ndk-bundle/toolchains/arm-linux-androideabi-4.9 /u/data/software/develop/java/android-sdk-macosx/ndk-bundle/toolchains/mips64el-linux-android mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ cordova prepare android mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ cordova build android ANDROID_HOME=/u/data/software/develop/java/android-sdk-macosx JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home Subproject Path: CordovaLib Error: spawn EACCES mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ chmod +x platforms/android/gradlew mac@macos ~/git/nodejs-mobile-samples/cordova/UseNativeModules (orig) $ cordova build android ANDROID_HOME=/u/data/software/develop/java/android-sdk-macosx JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home Subproject Path: CordovaLib Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details Repository https://jcenter.bintray.com/ replaced by http://maven.aliyun.com/nexus/content/groups/public/. Repository https://jcenter.bintray.com/ replaced by http://maven.aliyun.com/nexus/content/groups/public/. /Users/mac/.nvm/versions/node/v8.6.0/bin/npm publishNonDefault is deprecated and has no effect anymore. All variants are now published. :CopyNodeProjectAssetsarmeabi-v7a :MakeToolchainarmeabi-v7a HOST_OS=darwin HOST_EXE= HOST_ARCH=x86_64 HOST_TAG=darwin-x86_64 HOST_NUM_CPUS=4 BUILD_NUM_CPUS=8 Toolchain installed to /Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/standalone-toolchains/arm-linux-androideabi. :BuildNpmModulesarmeabi-v7a FAILED

FAILURE: Build failed with an exception.

Here ,I add the following line to task "CopyNodeProjectAssets${abi_name}" in "platforms/android/nodejs-mobile-cordova/usenativemodules-build.gradle":

outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"

When I executed "cordova build android" again, I got the error mentioned in my first comment.

jaimecbernardo commented 5 years ago

Hi @tsinghoo ,

I've been able to build the sample correctly with NDK r17b. I've tried with NDK r18b and it seems that the -fPIC flag is now mandatory.

Look for node_modules/nodejs-mobile-gyp/addon.gypi in the Cordova project and replace the following line: https://github.com/janeasystems/nodejs-mobile-gyp/blob/d7e5446c2cc260f85559d053518d84b4ab6b7908/addon.gypi#L144

Where it reads:

(OS=="linux" and target_arch!="ia32")', {

replace by:

(OS in "linux android" and target_arch!="ia32")', {

Please let us know if this solves your issue, so it may be included in the next nodejs-mobile-gyp version.

tsinghoo commented 5 years ago

Thank you, jaimecbernardo. It works now.

btw, do you have a better solution to handle the following error? It is also in my previous comment.

   A problem occurred starting process 'command 'npm''

If you use my solution, you will build the library every time even you do not update the code.

jaimecbernardo commented 5 years ago

I'm not sure what's causing the A problem occurred starting process 'command 'npm'' for you and didn't understand what your solution to that is. Could you specify it?

To understand the actual error, applying the Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. advice is useful. You can do this by running gradlew directly like this, for example:

cd platforms/android
./gradlew assembleDebug --stacktrace
tsinghoo commented 5 years ago

This is the output error. I guess that directory does not exists when trying to run 'npm'.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0. See https://docs.gradle.org/4.6/userguide/command_line_interface.html#sec:command_line_warnings

jaimecbernardo commented 5 years ago

I think the actual error was caused by the build process being unable to find npm in the PATH:

Caused by: java.io.IOException: Cannot run program "npm" (in directory "/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project"): error=2, No such file or directory

At least that's what was happening when it occurred to me as well.

tsinghoo commented 5 years ago

When I got this error, I was able to run "npm" in the command line, but the following directory did not exist:

/Users/mac/git/nodejs-mobile-samples/cordova/UseNativeModules/platforms/android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project

That is why the error disappears if you add this directory to outputs.file of task "CopyNodeProjectAssets${abi_name}"