Giter VIP home page Giter VIP logo

androidndkbuildwithcmakelist's Introduction

AndroidNdkBuildWithCmakelist

CMakeList.txt ref

            https://developer.android.com/ndk/guides/other_build_systems 

You have to install android sutdio or download NDK toolkits eg: https://dl.google.com/android/repository/android-ndk-r26d-linux.zip All cmake tool chain will use and under ndk android

            mkdir build
            cd build 

            /home/dunp/Android/Sdk/cmake/3.22.1/bin/cmake ..
            /home/dunp/Android/Sdk/cmake/3.22.1/bin/cmake --build .

            will get libandroidtestsharedobject.so

copy libc++_shared.so and built your prj..so (libandroidtestsharedobject.so)

libc++_shared.so

            /home/dunp/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so

copy .so to you android proj by dir struct bellow (jniLibs dir)

            ./android/app/src/main/jniLibs/
            ............................../arm64-v8a/libandroidtestsharedobject.so
            ............................../arm64-v8a/libc++_shared.so
            ............................../armeabi-v7/....
            ............................../x86_x64/....
            ............................../x86/....

code c/c++ sample

most important for every function: extern "C" attribute((visibility("default"))) attribute((used))

            extern "C" __attribute__((visibility("default"))) __attribute__((used))

            char *dunp_test_stdString(char *text)
            {
                size_t sizeA = strlen(text);
                char *b = " PONG ";
                size_t sizeB = strlen(b);
                size_t size = sizeof(char) * (sizeA + sizeB + 1); // Size of our new memory block large enough to contain both a and b.
                                                                // Leave additional space for null terminator
                char *c = (char *)malloc(size);

                memcpy(c, text, sizeA);      // Copy a into the first half of c
                memcpy(c + sizeA, b, sizeB); // Copy b into the second half
                c[sizeA + sizeB] = '\0';     // Assign null terminator to last character
                // free(c);
                return c;
            }

code dart , flutter sample in android

ffi load .so lib

            ffi.DynamicLibrary dylibtest_init({String dirInterop = ""}) {
            if (dirInterop == "") {
                dirInterop = Directory.current.path;
            }

            var libraryPath = path.join(dirInterop, 'dunpserialport', 'libandroidtestsharedobject.so');

            if (Platform.isMacOS) {
                libraryPath =
                    path.join(dirInterop, 'dunpserialport', 'libandroidtestsharedobject.dylib');
            } else if (Platform.isWindows) {
                libraryPath =
                    path.join(dirInterop, 'dunpserialport', 'Debug', 'libandroidtestsharedobject.dll');
            } else if (Platform.isLinux) {
                libraryPath = path.join(dirInterop, 'dunpserialport', 'libandroidtestsharedobject.so');
            } else {
                //flutter, android
                libraryPath = "libandroidtestsharedobject.so";
            }
            print("ffi.DynamicLibrary file: $libraryPath");
            final dylib = ffi.DynamicLibrary.open(libraryPath);

            return dylib;
            }

usage

            typedef FfiStringFuncString = ffi.Pointer<Utf8> Function(
                ffi.Pointer<Utf8> str, ffi.Int32 length);
            typedef DartStringFuncString = ffi.Pointer<Utf8> Function(
                ffi.Pointer<Utf8>, int);


            var dylib = usbseriallibs.dylibtest_init();
            print("dlib--------------OK");
            print(dylib);

            DartStringFuncString testStdString = dylib
                .lookup<ffi.NativeFunction<FfiStringFuncString>>('dunp_test_stdString')
                .asFunction();

            var toTest = "Nguyen Phan Du".toNativeUtf8();
            var ressult = testStdString(toTest, toTest.length).toDartString();

            print("dlib--------------testStdString: $ressult");

androidndkbuildwithcmakelist's People

Contributors

badpaybad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.