https://github.com/helmssyss/basic-dart-ffi-example
Calling from function in C for Android Application
https://github.com/helmssyss/basic-dart-ffi-example
cpp dart ffi-bindings flutter flutter-examples
Last synced: 2 months ago
JSON representation
Calling from function in C for Android Application
- Host: GitHub
- URL: https://github.com/helmssyss/basic-dart-ffi-example
- Owner: Helmssyss
- License: unlicense
- Created: 2025-08-30T18:43:56.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-30T19:07:54.000Z (11 months ago)
- Last Synced: 2025-08-30T20:35:30.422Z (11 months ago)
- Topics: cpp, dart, ffi-bindings, flutter, flutter-examples
- Language: Dart
- Homepage:
- Size: 76.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Getting Started
EN
1 - Create a CMakeLists.txt file under the Android folder. Write the name you want for it to be compiled as a dynamically shared file, then specify it with the `SHARED` key. Next, add the .cpp code you want to compile: `add_library(hello SHARED ../lib/hello.cpp)`. Since I wanted to use the cpp code in the same location as the dart files, I selected hello.cpp by specifying the path to the lib folder.
2 - Go to android/app/build.gradle, and add the following line so that it matches the minimum version you specified in CMakeLists.txt (`cmake_minimum_required(VERSION 3.22.1)`):
```gradle
android{
namespace = "..."
compileSdk = ...
ndkVersion = ...
compileOptions{...}
kotlinOptions{...}
defaultConfig {...}
//add new line
externalNativeBuild{
cmake{
path file("../CMakeLists.txt")
version "3.22.1"
}
}
}
```
3 - If you are writing in C++, you must wrap your functions with `extern "C"` and then add the `__attribute__((visibility(“default”))) __attribute__((used))` flags. We must add these so that they can be used when compiled as a dynamic shared file (.so) for Android on the Dart FFI side. If you are writing in C, you only need to add the `__attribute__((visibility(“default”))) __attribute__((used))` flags. You can then run the project from main.dart.
---------------
TR
1 - Android klasörü altında CMakeLists.txt dosyası oluşturun içeriğini dinamik paylaşılacak dosya olarak derlenmesi için istediğiniz ismi yazın ardından SHARED anahtarı ile belirtin daha sonra hangi .cpp kodunu derleyecekseniz onu ekleyin `add_library(hello SHARED ../lib/hello.cpp)`. Ben cpp kodunu dart dosyaları ile aynı konumda kullanmak istediğimden lib klasörünün yolunu vererek hello.cpp'yi seçtim.
2 - `android/app/build.gradle` içerisine girin, CMakeLists.txt içerisinde belirttiğiniz minimum sürüm ile(`cmake_minimum_required(VERSION 3.22.1)`) aynı olacak şekilde aşağıdaki eklemeyi yapın
```gradle
android{
namespace = "..."
compileSdk = ...
ndkVersion = ...
compileOptions{...}
kotlinOptions{...}
defaultConfig {...}
// bu satırı ekle
externalNativeBuild{
cmake{
path file("../CMakeLists.txt")
version "3.22.1"
}
}
}
```
3 - Eğer C++ yazacaksanız fonksiyonlarınızı `extern "C"` ile sarmalamanız gerekir daha sonra `__attribute__((visibility("default"))) __attribute__((used))` bayraklarını eklemelisiniz. Bu dart ffi tarafında Android için .so olarak dinamik paylaşılabilir dosya halinde derlendiğinde kullanılmak için bunları eklemek zorundayız. C ile yazacaksanız sadece `__attribute__((visibility("default"))) __attribute__((used))` bayraklarını eklemeniz yeterli olacak. Daha sonra projeyi `main.dart`'tan çalıştırabilirsiniz.