Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android build fails #10

Closed
olliiiver opened this issue Dec 4, 2024 · 8 comments
Closed

Android build fails #10

olliiiver opened this issue Dec 4, 2024 · 8 comments

Comments

@olliiiver
Copy link
Contributor

Hello.

I'm trying to build the library for iOS and Android. iOS build works perfectly fine by using Expo.

I was wondering, if I can also do a build for Android as well, but that fails with the following message:

  C++ build system [configure] failed while executing:
      /Users/oliver/Library/Android/sdk/cmake/3.22.1/bin/cmake \
        -H/Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/react-native/ReactAndroid/cmake-utils/default-app-setup \
        -DCMAKE_SYSTEM_NAME=Android \
        -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
        -DCMAKE_SYSTEM_VERSION=24 \
        -DANDROID_PLATFORM=android-24 \
        -DANDROID_ABI=arm64-v8a \
        -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
        -DANDROID_NDK=/Users/oliver/Library/Android/sdk/ndk/26.1.10909125 \
        -DCMAKE_ANDROID_NDK=/Users/oliver/Library/Android/sdk/ndk/26.1.10909125 \
        -DCMAKE_TOOLCHAIN_FILE=/Users/oliver/Library/Android/sdk/ndk/26.1.10909125/build/cmake/android.toolchain.cmake \
        -DCMAKE_MAKE_PROGRAM=/Users/oliver/Library/Android/sdk/cmake/3.22.1/bin/ninja \
        -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/build/intermediates/cxx/Debug/536k5e17/obj/arm64-v8a \
        -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/build/intermediates/cxx/Debug/536k5e17/obj/arm64-v8a \
        -DCMAKE_BUILD_TYPE=Debug \
        -DCMAKE_FIND_ROOT_PATH=/Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/.cxx/Debug/536k5e17/prefab/arm64-v8a/prefab \
        -B/Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/.cxx/Debug/536k5e17/arm64-v8a \
        -GNinja \
        -DPROJECT_BUILD_DIR=/Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/build \
        -DREACT_ANDROID_DIR=/Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/react-native/ReactAndroid \
        -DANDROID_STL=c++_shared \
        -DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON
    from /Users/oliver/Documents/cc/matrix-test/matrix-test/android/app
  CMake Error at /Users/oliver/Documents/cc/matrix-test/matrix-test/android/app/build/generated/autolinking/src/main/jni/Android-autolinking.cmake:9 (add_subdirectory):
    add_subdirectory given source
    "/Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/@unomed/react-native-matrix-sdk/android/build/generated/source/codegen/jni/"
    which is not an existing directory.
  Call Stack (most recent call first):
    /Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake:85 (include)
    CMakeLists.txt:31 (include)


  CMake Error at /Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake:88 (target_link_libraries):
    Cannot specify link libraries for target
    "react_codegen_RNReactNativeMatrixSdkSpec" which is not built by this
    project.

For reproducing this, I've added a small repo here:

https://github.com/olliiiver/matrix_rust_sdk_test

It seems the build process is missing a build step. How can I fix this?

Any help would be greatly appreciated!

Regards,

Oliver

@Johennes
Copy link
Collaborator

Johennes commented Dec 4, 2024

This might mean that the code generation didn't run. 🤔

    "/Users/oliver/Documents/cc/matrix-test/matrix-test/node_modules/@unomed/react-native-matrix-sdk/android/build/generated/source/codegen/jni/"
    which is not an existing directory.

I'm not at all familiar with Expo, unfortunately, but in the example app in this repo, codegen is triggered in build.gradle: https://github.com/unomed-dev/react-native-matrix-sdk/blob/main/example/android/app/build.gradle#L125

@olliiiver
Copy link
Contributor Author

Hi @Johennes.

Thanks for the quick response. Looks like the code is being built automatically in android/generated/jni without the need of any specific config in the react native project.

If I copy over the files after a first run, the Android build works:

mkdir -p ./node_modules/@unomed/react-native-matrix-sdk/android/build/generated/source/codegen
cp -r ./node_modules/@unomed/react-native-matrix-sdk/android/generated/jni ./node_modules/@unomed/react-native-matrix-sdk/android/build/generated/source/codegen

I would provide a PR, but I couldn't figure out where the codegen directory is (or can be) specified inside build process of the library.

Any idea?

Regards,

Oliver

@Johennes
Copy link
Collaborator

Johennes commented Dec 9, 2024

Oh, interesting. The output directories for codegen are specified in package.json.

  "codegenConfig": {
    "name": "RNReactNativeMatrixSdkSpec",
    "type": "all",
    "jsSrcsDir": "src",
    "outputDir": {
      "ios": "ios/generated",
      "android": "android/generated"
    },
    "android": {
      "javaPackageName": "com.unomed.reactnativematrixsdk"
    },
    "includesGeneratedCode": true
  },

It's interesting that they're being expected in another location in your build. 😵‍💫

Could you check if changing the outputDir values makes it codegen into the right location in your build?

We made a fix in uniffi-bindgen-react-native to handle non-standard codegen output dirs a while ago that hasn't been consumed in react-native-matrix-sdk yet. However, I'm not sure that's related.

@olliiiver
Copy link
Contributor Author

Hi @Johennes,

I've changed the outputDir in package.json, but without any luck.

It seems that React Native expects the files at android/build/generated/source/codegen/ rather than in android/generated, see:

https://github.com/facebook/react-native/blob/3cc67fed36c9bc52e5aafec6aeceb21c95a1f919/packages/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake#L99

However, I couldn't find the right place where the build process creates android/generated, so that this part could be changed...

@Johennes
Copy link
Collaborator

I think you might have to trigger codegen manually after changing package.json with yarn react-native codegen --path node_modules/@unomed/react-native-matrix-sdk.

When I apply the following diff, I'm getting the generated code under the expected path.

diff --git a/package.json b/package.json
index 38a562a..36aeacd 100644
--- a/package.json
+++ b/package.json
@@ -201,7 +201,7 @@
     "jsSrcsDir": "src",
     "outputDir": {
       "ios": "ios/generated",
-      "android": "android/generated"
+      "android": "android/build/generated/source/codegen"
     },
     "android": {
       "javaPackageName": "com.unomed.reactnativematrixsdk"
diff --git a/react-native.config.js b/react-native.config.js
index 21980d4..66211da 100644
--- a/react-native.config.js
+++ b/react-native.config.js
@@ -5,7 +5,7 @@ module.exports = {
   dependency: {
     platforms: {
       android: {
-        cmakeListsPath: 'generated/jni/CMakeLists.txt',
+        cmakeListsPath: 'build/generated/source/codegen/jni/CMakeLists.txt',
       },
     },
   },

I'm still confused that ReactNative-application.cmake hardcodes a specific path when the output folder is configurable in package.json.

I'm also not sure why I haven't seen this in the project I'm consuming react-native-matrix-sdk but maybe it's because I'm not on the latest RN version there.

@olliiiver
Copy link
Contributor Author

Hi @Johennes . Thanks for merging the PRs. Could you create a new release, please?

Since everything is included in the NPM package, I think someone just needs npm install ... and everything should run out-of-the-box. 😃

@Johennes
Copy link
Collaborator

Sorry, I've been meaning to do it yesterday already but I was having some build issues locally. Releasing currently requires building matrix-rust-sdk in release mode locally which takes ... long.

I've just landed #15 which was needed to compile the x86-64 Android target on my machine. Will start the release build now.

Hopefully this can happen on the CI in future.

@Johennes
Copy link
Collaborator

Just pushed 0.3.0 to npm. Let me know if this doesn't fix your issues and thanks once more for helping to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants