lyubo / react-native-sodium

ISC License
61 stars 46 forks source link

build fails at CMakeLists.txt copy step with arm64, can't find .so file #22

Closed mutablestudio closed 4 years ago

mutablestudio commented 5 years ago

We are using expo ejected, have been using react-native-sodium without issue until we made compile and target sdk 28. Can't seem to get past this error in both debug and release builds.

package.json:

    "expo": "^34.0.3",
    "react": "16.8.3",
    "react-native": "0.59.8",
    ...
    "react-native-sodium": "^0.3.4",

AS error log:

Build command failed.
Error while executing process /Android/sdk/cmake/3.6.4111459/bin/cmake with arguments 
{
-H/node_modules/react-native-sodium/android 
-B/node_modules/react-native-sodium/android/.externalNativeBuild/cmake/debug/arm64-v8a 
-DANDROID_ABI=arm64-v8a 
-DANDROID_PLATFORM=android-26 
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/node_modules/react-native-sodium/android/build/intermediates/cmake/debug/obj/arm64-v8a 
-DCMAKE_BUILD_TYPE=Debug 
-DANDROID_NDK=/Android/sdk/ndk-bundle 
-DCMAKE_TOOLCHAIN_FILE=/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake 
-DCMAKE_MAKE_PROGRAM=/Android/sdk/cmake/3.6.4111459/bin/ninja 
-GAndroid Gradle 
- Ninja 
-DANDROID_TOOLCHAIN=clang
}
-- Check for working C compiler: /Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
-- Check for working C compiler: /Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
-- Check for working CXX compiler: /Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring incomplete, errors occurred!
See also "/node_modules/react-native-sodium/android/.externalNativeBuild/cmake/debug/arm64-v8a/CMakeFiles/CMakeOutput.log".

CMake Error at CMakeLists.txt:23 (file):
  file COPY cannot find
  "/node_modules/react-native-sodium/android/../libsodium/libsodium-android-armv8-a/lib/libsodium.so".
peterkuiper commented 5 years ago

I was able to fix this by updating node_modules/react-native-sodium/android/build.gradle:

 13     compileSdkVersion 28
 14     buildToolsVersion "28.0.3"
 15     defaultConfig {
 16         minSdkVersion 16
 17         targetSdkVersion 28

I also changed node_modules/react-native-sodium/android/CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

set(ARCH_DIR ${ANDROID_ARCH_NAME})

if( ANDROID_ABI STREQUAL "armeabi-v7a")
  set( ARCH_DIR "armv7-a" )
elseif ( ANDROID_ABI STREQUAL "arm64-v8a")
  set( ARCH_DIR "armv8-a" )
elseif ( ANDROID_ABI STREQUAL "x86")
  set ( ARCH_DIR "i686" )
elseif ( ANDROID_ABI STREQUAL "x86_64")
  set ( ARCH_DIR "westmere" )
endif()

#message(WARNING "ANDROID_ARCH_NAME=\"${ANDROID_ARCH_NAME}\" ARCH_DIR=\"${ARCH_DIR}\" ANDROID_ABI=\"${ANDROID_ABI}\"")

#if( ARCH_DIR STREQUAL "arm" )
#  set( ARCH_DIR "armv6" )
#elseif( ARCH_DIR STREQUAL "arm64" )
#  set( ARCH_DIR "armv8-a" )
#elseif( ARCH_DIR STREQUAL "x86" )
#  set( ARCH_DIR "i686" )
#elseif( ARCH_DIR STREQUAL "x86_64" )
#  set( ARCH_DIR "westmere" )
#elseif( ARCH_DIR STREQUAL "mips" )
#  set( ARCH_DIR "mips32" )
#elseif( ARCH_DIR STREQUAL "mips64" )
#  set( ARCH_DIR "mips64r6" )
#endif()

add_library(sodium SHARED IMPORTED)
include_directories( ${PROJECT_SOURCE_DIR}/../libsodium/libsodium-android-${ARCH_DIR}/include/ )
set_target_properties( sodium PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../libsodium/libsodium-android-${ARCH_DIR}/lib/libsodium.so )
file(COPY ${PROJECT_SOURCE_DIR}/../libsodium/libsodium-android-${ARCH_DIR}/lib/libsodium.so DESTINATION "${PROJECT_SOURCE_DIR}/lib/${ANDROID_ABI}")

add_library(sodium-jni SHARED src/main/cpp/sodium-jni.c)
target_link_libraries(sodium-jni sodium)

Please note that I'm not an expert, but this worked for me (RN 0.60.6).

I have also created a patch:

patch -p0 < react-native-sodium.patch

react-native-sodium.patch

diff -Naur node_modules/react-native-sodium/android/CMakeLists.txt node_modules/react-native-sodium.new/android/CMakeLists.txt
--- node_modules/react-native-sodium/android/CMakeLists.txt 2019-09-20 13:15:05.000000000 +0200
+++ node_modules/react-native-sodium.new/android/CMakeLists.txt 2019-10-09 10:41:55.000000000 +0200
@@ -2,21 +2,16 @@

 set(ARCH_DIR ${ANDROID_ARCH_NAME})

-if( ARCH_DIR STREQUAL "arm" )
-  set( ARCH_DIR "armv6" )
-elseif( ARCH_DIR STREQUAL "arm64" )
+if( ANDROID_ABI STREQUAL "armeabi-v7a")
+  set( ARCH_DIR "armv7-a" )
+elseif ( ANDROID_ABI STREQUAL "arm64-v8a")
   set( ARCH_DIR "armv8-a" )
-elseif( ARCH_DIR STREQUAL "x86" )
-  set( ARCH_DIR "i686" )
-elseif( ARCH_DIR STREQUAL "x86_64" )
-  set( ARCH_DIR "westmere" )
-elseif( ARCH_DIR STREQUAL "mips" )
-  set( ARCH_DIR "mips32" )
-elseif( ARCH_DIR STREQUAL "mips64" )
-  set( ARCH_DIR "mips64r6" )
+elseif ( ANDROID_ABI STREQUAL "x86")
+  set ( ARCH_DIR "i686" )
+elseif ( ANDROID_ABI STREQUAL "x86_64")
+  set ( ARCH_DIR "westmere" )
 endif()

-
 add_library(sodium SHARED IMPORTED)
 include_directories( ${PROJECT_SOURCE_DIR}/../libsodium/libsodium-android-${ARCH_DIR}/include/ )
 set_target_properties( sodium PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../libsodium/libsodium-android-${ARCH_DIR}/lib/libsodium.so )
diff -Naur node_modules/react-native-sodium/android/build.gradle node_modules/react-native-sodium.new/android/build.gradle
--- node_modules/react-native-sodium/android/build.gradle   2019-09-20 13:15:05.000000000 +0200
+++ node_modules/react-native-sodium.new/android/build.gradle   2019-10-09 10:41:34.000000000 +0200
@@ -10,11 +10,11 @@
 apply plugin: 'com.android.library'

 android {
-    compileSdkVersion 24
-    buildToolsVersion "24.0.1"
+    compileSdkVersion 28
+    buildToolsVersion "28.0.1"
     defaultConfig {
         minSdkVersion 16
-        targetSdkVersion 24
+        targetSdkVersion 28
         versionCode 1
         versionName "0.1"
         externalNativeBuild {
lyubo commented 5 years ago

Can you instead make a pull request in github? It would be easier and more traceable than patching.

peterkuiper commented 5 years ago

@lyubo I agree but I am unsure if this is the correct way of fixing this. If you think it is, I will create a PR, np.

mutablestudio commented 5 years ago

fyi @peterkuiper @lyubo what I discovered was that the build somehow did not unpack precompiled.tgz so there was no libsodium directory - not sure why this was happening but I unpacked it and problem was solved.

also I am forcing subprojects to use 28/28.0.3, so did not need to change libsodium gradle

peterkuiper commented 5 years ago

@mutablestudio I'm experiencing the same thing. Sometimes the postinstall does not run (package.json). How do you force subprojects to use 28/28.0.3? Maybe you can share your changes with us.

mutablestudio commented 5 years ago

to force subprojects, @peterkuiper in your root build.gradle (not the app gradle):

ext {
  compileSdkVersion = 28
  buildToolsVersion = '28.0.3'
}

// Force sub libs to use the compile sdk & build tools version
subprojects { subproject ->
  afterEvaluate{
    if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
      android {
        compileSdkVersion rootProject.ext.compileSdkVersion
        buildToolsVersion rootProject.ext.buildToolsVersion
      }
    }
  }
}