How to register C++ React Native module in Android












23














I have a C++ React Native module derived from facebook::xplat::module::CxxModule. It is working ok with iOS project but now I'm trying to figure out how to use it from Java. The only documentation I found is the comment in React Native codebase stating that:




NativeModules whose implementation is written in C++ must
not provide any Java code (so they can be reused on other platforms),
and instead should register themselves using
CxxModuleWrapper




My question is how to register C++ module in Java using CxxModuleWrapper










share|improve this question


















  • 2




    "Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
    – bjornd
    Jun 15 at 16:38










  • Can you use some tool to generate bindings, like this
    – Raviprakash
    Jun 18 at 17:47


















23














I have a C++ React Native module derived from facebook::xplat::module::CxxModule. It is working ok with iOS project but now I'm trying to figure out how to use it from Java. The only documentation I found is the comment in React Native codebase stating that:




NativeModules whose implementation is written in C++ must
not provide any Java code (so they can be reused on other platforms),
and instead should register themselves using
CxxModuleWrapper




My question is how to register C++ module in Java using CxxModuleWrapper










share|improve this question


















  • 2




    "Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
    – bjornd
    Jun 15 at 16:38










  • Can you use some tool to generate bindings, like this
    – Raviprakash
    Jun 18 at 17:47
















23












23








23


4





I have a C++ React Native module derived from facebook::xplat::module::CxxModule. It is working ok with iOS project but now I'm trying to figure out how to use it from Java. The only documentation I found is the comment in React Native codebase stating that:




NativeModules whose implementation is written in C++ must
not provide any Java code (so they can be reused on other platforms),
and instead should register themselves using
CxxModuleWrapper




My question is how to register C++ module in Java using CxxModuleWrapper










share|improve this question













I have a C++ React Native module derived from facebook::xplat::module::CxxModule. It is working ok with iOS project but now I'm trying to figure out how to use it from Java. The only documentation I found is the comment in React Native codebase stating that:




NativeModules whose implementation is written in C++ must
not provide any Java code (so they can be reused on other platforms),
and instead should register themselves using
CxxModuleWrapper




My question is how to register C++ module in Java using CxxModuleWrapper







java android c++ react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 10 at 7:44









bjornd

17.5k44365




17.5k44365








  • 2




    "Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
    – bjornd
    Jun 15 at 16:38










  • Can you use some tool to generate bindings, like this
    – Raviprakash
    Jun 18 at 17:47
















  • 2




    "Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
    – bjornd
    Jun 15 at 16:38










  • Can you use some tool to generate bindings, like this
    – Raviprakash
    Jun 18 at 17:47










2




2




"Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
– bjornd
Jun 15 at 16:38




"Your question really is how to call C++ from javascript without calling java I suspect (if possible)?" - No, I can just use JNI and call any C++ library easily, the problem here is that I will need to do all data conversion and will have to implement all the callbacks and promises to communicate with JS side. This could be avoided using C++ module inherited from facebook::xplat::module::CxxModule. I have such a module and I can use it from Objective C but not from Java
– bjornd
Jun 15 at 16:38












Can you use some tool to generate bindings, like this
– Raviprakash
Jun 18 at 17:47






Can you use some tool to generate bindings, like this
– Raviprakash
Jun 18 at 17:47














1 Answer
1






active

oldest

votes


















0














Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d.
Snippet from the blog:



Export it from native



extern "C" HelloCxxModule* createHelloCxxModule() {
return new HelloCxxModule();
}


Register it in java



public final class HelloCxxPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
// I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.
CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")
);
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50781569%2fhow-to-register-c-react-native-module-in-android%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d.
    Snippet from the blog:



    Export it from native



    extern "C" HelloCxxModule* createHelloCxxModule() {
    return new HelloCxxModule();
    }


    Register it in java



    public final class HelloCxxPackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    return Arrays.<NativeModule>asList(
    // I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.
    CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")
    );
    }
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
    }
    }





    share|improve this answer




























      0














      Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d.
      Snippet from the blog:



      Export it from native



      extern "C" HelloCxxModule* createHelloCxxModule() {
      return new HelloCxxModule();
      }


      Register it in java



      public final class HelloCxxPackage implements ReactPackage {
      @Override
      public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
      return Arrays.<NativeModule>asList(
      // I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.
      CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")
      );
      }
      @Override
      public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
      return Collections.emptyList();
      }
      }





      share|improve this answer


























        0












        0








        0






        Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d.
        Snippet from the blog:



        Export it from native



        extern "C" HelloCxxModule* createHelloCxxModule() {
        return new HelloCxxModule();
        }


        Register it in java



        public final class HelloCxxPackage implements ReactPackage {
        @Override
        public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Arrays.<NativeModule>asList(
        // I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.
        CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")
        );
        }
        @Override
        public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
        }
        }





        share|improve this answer














        Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d.
        Snippet from the blog:



        Export it from native



        extern "C" HelloCxxModule* createHelloCxxModule() {
        return new HelloCxxModule();
        }


        Register it in java



        public final class HelloCxxPackage implements ReactPackage {
        @Override
        public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Arrays.<NativeModule>asList(
        // I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.
        CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")
        );
        }
        @Override
        public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 3:53

























        answered Nov 21 at 2:44









        Geng Jiawen

        4,1552527




        4,1552527






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50781569%2fhow-to-register-c-react-native-module-in-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'