TFLite FlexDelegate Android the library is not linked error
@tilakrayal @pkgoogle @LakshmiKalaKadali  Hello guys, any suggestion? Maybe there is a more stable version where the delegate is guaranteed to work? Or what could be the problem? I will be glad of any help! Hi @pkgoogle,
Please look into the issue
Thank you Hi @drus-13, can you tell me the exact commands you used to both build the required files, pushed to the phone, and then run the execution on the phone? From reading the above it does not sound like you run any cmake or make commands on the phone. Feel free to exclude any PII, include any relevant information like which directory you are in when running the commands -- it seems you have a separate project with your code -- I understand that may not always be shareable but usually the more details you provide the less guesswork I will have to do. Thanks for your help.

One possible alternative is trying to build libtensorflowlite_flex.so with bazel: https://www.tensorflow.org/lite/guide/ops_select and then linking this file with your executable. Hi @pkgoogle, thanks for your help. So, as I wrote above, I built libtensorflowlite_flex.so using bazel and linked the library to my code.

The command I used is like
`bazel build -c opt --config=android_arm64 --config=monolithic tensorflow/lite/delegates/flex:tensorflowlite_flex`

Alternatively, you can try using my sources https://github.com/drus-13/mnist_tf

Essentially, every piece of the application (lib and app folders) is built using the build_android.sh script H @pkgoogle will there be an opportunity to take a look at what I have attached above. Or do you need any additional information from me? Hi @drus-13, I'm looking into this... I am running into issues building the .so files ... if you can help me understand how you did it. i.e. what NDK and SDK did you use? How did you answer the ./configure script in the tensorflow source root folder?

What command did you use to build libtensorflowlite.so? Something like this?
```
bazel build -c opt --config=android_arm64 --config=monolithic tensorflow/lite:tensorflowlite
```

Thanks for your help Hi @pkgoogle, so for `libtensorflowlite_flex.so` i use configuration like this:

```
andrey@Andreys-MacBook-Pro tensorflow-r2.14 % ./configure
You have bazel 6.1.0 installed.
Please specify the location of python. [Default is /opt/homebrew/opt/python@3.11/bin/python3.11]: /usr/bin/python3


Found possible Python library paths:
  /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages
  /Library/Python/3.9/site-packages
Please input the desired Python library path to use.  Default is [/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is /Users/andrey/library/Android/Sdk/ndk-bundle]: /Users/andrey/Library/Android/sdk/ndk/21


Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29', '30']] [Default is 26]: 


Please specify the home path of the Android SDK to use. [Default is /Users/andrey/library/Android/Sdk]: /Users/andrey/Library/Android/sdk/  


Either /Users/andrey/Library/Android/sdk/   does not exist, or it does not contain the subdirectories "platforms" and "build-tools".
Please specify the home path of the Android SDK to use. [Default is /Users/andrey/library/Android/Sdk]: /Users/andrey/Library/Android/sdk/      


Please specify the Android SDK API level to use. [Available levels: ['30', '33', '34']] [Default is 34]: 


Please specify an Android build tools version to use. [Available versions: ['30.0.2', '33.0.1', '34.0.0']] [Default is 34.0.0]: 


Do you wish to build TensorFlow with iOS support? [y/N]: N
No iOS support will be enabled for TensorFlow.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
	--config=mkl         	# Build with MKL support.
	--config=mkl_aarch64 	# Build with oneDNN and Compute Library for the Arm Architecture (ACL).
	--config=monolithic  	# Config for mostly static monolithic build.
	--config=numa        	# Build with NUMA support.
	--config=dynamic_kernels	# (Experimental) Build kernels into separate shared objects.
	--config=v1          	# Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
	--config=nogcp       	# Disable GCP support.
	--config=nonccl      	# Disable NVIDIA NCCL support.
Configuration finished
andrey@Andreys-MacBook-Pro tensorflow-r2.14 % bazel build -c opt --config=android_arm64 --config=monolithic tensorflow/lite/delegates/flex:tensorflowlite_flex

```

for `libtensorflowlite.so` same thing:

```
andrey@Andreys-MacBook-Pro tensorflow-r2.14 % ./configure
You have bazel 6.1.0 installed.
Please specify the location of python. [Default is /opt/homebrew/opt/python@3.11/bin/python3.11]: /usr/bin/python3


Found possible Python library paths:
  /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages
  /Library/Python/3.9/site-packages
Please input the desired Python library path to use.  Default is [/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is /Users/andrey/library/Android/Sdk/ndk-bundle]: /Users/andrey/Library/Android/sdk/ndk/21


Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29', '30']] [Default is 26]: 


Please specify the home path of the Android SDK to use. [Default is /Users/andrey/library/Android/Sdk]: /Users/andrey/Library/Android/sdk/      


Please specify the Android SDK API level to use. [Available levels: ['30', '33', '34']] [Default is 34]: 


Please specify an Android build tools version to use. [Available versions: ['30.0.2', '33.0.1', '34.0.0']] [Default is 34.0.0]: 


Do you wish to build TensorFlow with iOS support? [y/N]: N
No iOS support will be enabled for TensorFlow.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
	--config=mkl         	# Build with MKL support.
	--config=mkl_aarch64 	# Build with oneDNN and Compute Library for the Arm Architecture (ACL).
	--config=monolithic  	# Config for mostly static monolithic build.
	--config=numa        	# Build with NUMA support.
	--config=dynamic_kernels	# (Experimental) Build kernels into separate shared objects.
	--config=v1          	# Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
	--config=nogcp       	# Disable GCP support.
	--config=nonccl      	# Disable NVIDIA NCCL support.
Configuration finished
andrey@Andreys-MacBook-Pro tensorflow-r2.14 % bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so
```

I hope this helps you, Thanks for your help

 Hi @drus-13, I'm further along .. I modified app/CMakeLists.txt to use relative paths:

```
cmake_minimum_required(VERSION 3.13)
project(UseModelTrainerApp)

# Add the executable
add_executable(UseModelTrainerApp main.cpp)

# find_package(boinc CONFIG REQUIRED)

# target_link_libraries(UseModelTrainerApp
#     unofficial::boinc::boinc
#     unofficial::boinc::boincapi
# )

# Create an imported target for libtensorflowlite.so
add_library(libtensorflowlite SHARED IMPORTED)
set_target_properties(libtensorflowlite PROPERTIES
    IMPORTED_LOCATION "../lib/arm64-v8a/libtensorflowlite.so"
    INTERFACE_INCLUDE_DIRECTORIES "../lib/include"
)

# Create an imported target for libtensorflowlite_flex.so
add_library(libtensorflowlite_flex SHARED IMPORTED)
set_target_properties(libtensorflowlite_flex PROPERTIES
    IMPORTED_LOCATION "../lib/arm64-v8a/libtensorflowlite_flex.so"
)

# Do the same for libModelTrainer.so, and specify its dependency on libtensorflowlite
add_library(libModelTrainer SHARED IMPORTED)
set_target_properties(libModelTrainer PROPERTIES
    IMPORTED_LOCATION "../lib/build_android_arm64/libModelTrainer.so"
    INTERFACE_INCLUDE_DIRECTORIES "../lib/include"
)
target_link_libraries(libModelTrainer 
    INTERFACE
    libtensorflowlite 
    -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex 
    libtensorflowlite_flex
)

# Link UseModelTrainerApp with libModelTrainer only, libtensorflowlite will be included automatically
target_link_libraries(UseModelTrainerApp
    -Wl,--no-as-needed
    libModelTrainer
)

# If the library uses TensorFlow Lite or other Android NDK libraries, link them as well
find_library(log-lib log)
target_link_libraries(UseModelTrainerApp ${log-lib})
```

When I run build_android.sh:
```sh
./build_android.sh
+++ dirname ./build_android.sh
++ cd .
++ pwd -P
+ PRJ_ROOT=/Users/xxxxx/git/mnist_tf/app
+ BUILD_ROOT=/Users/xxxxxx/git/mnist_tf/app/build_android_arm64
+ '[' -z /Users/xxxxxxx/Library/Android/sdk/ndk/25.2.9519653 ']'
+ rm -rf /Users/xxxxxxx/git/mnist_tf/app/build_android_arm64
+ mkdir -p /Users/xxxxxx/git/mnist_tf/app/build_android_arm64
+ cd /Users/xxxxxxx/git/mnist_tf/app/build_android_arm64
+ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -DCMAKE_TOOLCHAIN_FILE=/Users/xxxxxx/Library/Android/sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_NATIVE_API_LEVEL=26
-- The C compiler identification is Clang 14.0.7
-- The CXX compiler identification is Clang 14.0.7
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Users/xxxxxxx/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/xxxxxxx/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.7s)
CMake Error in CMakeLists.txt:
  Imported target "libModelTrainer" includes non-existent path

    "../lib/include"

  in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:

  * The path was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and references files it does not
  provide.



-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
```

I don't have lib/build_android_arm64/libModelTrainer.so

How did you build this?

Thanks. Hi @pkgoogle, could you please run similar script build_android.sh from `mnist_tf/lib` folder? this should work

Thanks. Hi @drus-13, I tried that and ran into various include issues, is your environment set up in such a way to pull in additional include files from somewhere else (not in the repo you shared)? That's what it looks like to me.

I tried to also copy all headers from the tensorflow source (r2.14 branch) as well as absl (HEAD) library into the include folder, my last attempt looked like this:
```sh
./build_android.sh 
+++ dirname ./build_android.sh
++ cd .
++ pwd -P
+ PRJ_ROOT=/Users/xxxxxxxxxx/git/mnist_tf/lib
+ BUILD_ROOT=/Users/xxxxxxxxxx/git/mnist_tf/lib/build_android_arm64
+ '[' -z /Users/xxxxxxxxxx/Library/Android/sdk/ndk/25.2.9519653 ']'
+ TFLITE_ANDROID=/Users/xxxxxxxxxx/git/mnist_tf/lib/arm64-v8a/
+ '[' '!' -d /Users/xxxxxxxxxx/git/mnist_tf/lib/arm64-v8a/ ']'
+ echo 'Using TFLite Android library at: /Users/xxxxxxxxxx/git/mnist_tf/lib/arm64-v8a/'
Using TFLite Android library at: /Users/xxxxxxxxxx/git/mnist_tf/lib/arm64-v8a/
+ rm -rf /Users/xxxxxxxxxx/git/mnist_tf/lib/build_android_arm64
+ mkdir -p /Users/xxxxxxxxxx/git/mnist_tf/lib/build_android_arm64
+ cd /Users/xxxxxxxxxx/git/mnist_tf/lib/build_android_arm64
+ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -DCMAKE_TOOLCHAIN_FILE=/Users/xxxxxxxxxx/Library/Android/sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_NATIVE_API_LEVEL=26
-- The C compiler identification is Clang 14.0.7
-- The CXX compiler identification is Clang 14.0.7
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Users/xxxxxxxxxx/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/xxxxxxxxxx/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (3.6s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/xxxxxxxxxx/git/mnist_tf/lib/build_android_arm64
+ make
[ 50%] Building CXX object CMakeFiles/ModelTrainer.dir/ModelTrainer.cpp.o
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/ModelTrainer.cpp:3:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/ModelTrainer.h:8:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/interpreter.h:21:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/interpreter.h:45:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/async/async_signature_runner.h:24:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/async/async_subgraph.h:27:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/subgraph.h:36:
/Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/macros.h:70:1: warning: inline variables are a C++17 extension [-Wc++17-extensions]
inline constexpr unsigned int flatbuffer_size_max = 2147483648;
^
/Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/macros.h:72:1: warning: inline variables are a C++17 extension [-Wc++17-extensions]
inline constexpr char tflite_metadata_buffer_location[] = "buffer_location";
^
/Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/core/macros.h:74:1: warning: inline variables are a C++17 extension [-Wc++17-extensions]
inline constexpr char tflite_metadata_min_runtime_version[] =
^
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/ModelTrainer.cpp:3:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/ModelTrainer.h:9:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/delegates/flex/delegate.h:19:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/lite/delegates/flex/delegate_data.h:21:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/core/common_runtime/eager/context.h:35:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/c/eager/immediate_execution_context.h:25:
In file included from /Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/c/eager/abstract_context.h:20:
/Users/xxxxxxxxxx/git/mnist_tf/lib/include/tensorflow/c/eager/abstract_function.h:18:10: fatal error: 'tensorflow/core/framework/function.pb.h' file not found
#include "tensorflow/core/framework/function.pb.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 warnings and 1 error generated.
make[2]: *** [CMakeFiles/ModelTrainer.dir/build.make:76: CMakeFiles/ModelTrainer.dir/ModelTrainer.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/ModelTrainer.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
```

We have a couple of options... 
1. Try to mirror your environment more exactly.
2. Modifying mnist_tf/lib/CMakeLists.txt to look more like this: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/minimal/CMakeLists.txt
3. Adjust your project to use Bazel

"tensorflow/core/framework/function.pb.h" is a generated file, so this is likely a problem with project setup. Hi @pkgoogle, sorry, there was an unused include in ModelTrainer.h. I updated repo with `libModelTrainer.so` and script build_android.sh from mnist_tf/lib also should work locally. 
I hope this will help you, Thanks!
 Hi @drus-13, let's take a step back, why are you trying to run a C++ executable on your device? We can certainly make it happen .. but if you're trying to build an app the preferred way is with the Android NDK and JNI if you want to code in C++: https://developer.android.com/ndk/samples/sample_hellojni

Reason I ask is, if we deviate from well defined paths, these type of issues tend to pop up a lot. Hi, @pkgoogle good question, I plan to integrate this solution with another third-party library, and everything is very much tied to cmake and pure C++. As a result, I plan to get a computing application that will be launched(from background) on Android from under another application. In fact, I already tested this solution with tensorflow-lite, everything was successfully assembled and worked in the end, the problem is that I need the flex delegate functionality (saving and loading weights, for example), so there is a need for tensorflow-lite-flex Hi @drus-13, thank you for the additional context, it is certainly always helpful, can you provide the exact adb commands you used to push to the device? Did you just put it all in one directory or is there any structure at the destination/device? Also the command used to actually execute the program. Hi @pkgoogle well, I push all files model.tflite, UseModelTrainerApp, libModelTrainer.so, libtensorflowlite_flex.so, libtensorflowlite.so, test_set.txt, train_set.txt (from pushToDevice folder for example)
-adb push "file name" /data/local/tmp
-adb shell
-cd /data/local/tmp
-export LD_LIBRARY_PATH=/data/local/tmp:${LD_LIBRARY_PATH}
-./UseModelTrainerApp Hi @drus-13, I was able to replicate your issue (Though I have not verified if everything we are doing is correct)... It seems linking to a .so file within Android may not be so trivial/well supported, I suspect us using it like normal Linux is causing these things to not link properly, can you try a simple integration of this code/app within your larger Android project and then add "org.tensorflow:tensorflow-lite-select-tf-ops" to your dependencies? Can you see if you can make a simple Native C++ project in Android studio and see if your code more or less works that way? Thanks for your help. @pkgoogle Hi! Ok I'll try to make a simple project in android studio and let you know. Maybe there is already source code in examples somewhere? Hi @drus-13, sure thing... here's example code for hello-jni: https://github.com/android/ndk-samples/tree/android-mk/hello-jni, this documentation is probably useful to guide you through it: https://developer.android.com/ndk/samples/sample_hellojni. You can also try asking gemini: https://gemini.google.com/, example prompt: "Hi Gemini, can you make me a tutorial for creating a simple JNI/NDK hello world project in Android Studio?"

I would say get that working on an emulator or your own device then try to integrate your code (it should be mostly copy paste and some mangling of the interfaces from there ... but let me know if you get stuck). Hi @pkgoogle Okay Thanks,  I'll try to create a project, but I meant, maybe there is an example project using tensorflow-lite-flex as you suggested? Hi @drus-13, I can't find an existing one but if you follow these directions: https://www.tensorflow.org/lite/guide/ops_select#android_aar, it should integrate flex ops to your project, please try it out and let  me know if it doesn't work. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Can i extend the life of the thread so that the bot doesn’t close it?)
I'll try the suggested version soon and post the results @drus-13 yeah you can just do what you just did, just comment with "bump" or something, we still have to be fair to all issues so we will keep the usual policy, but also it does help remind users :). This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. bump This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63104">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63104">No</a>

**System information**
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS
- TensorFlow installed from (source or binary): https://github.com/tensorflow/tensorflow
- TensorFlow version (or github SHA if from source): r2.14

Hi everyone! I have a problem with Tensorflow flex delegate on Android.

I’m trying to run simple model with some functions that require Flex Delegate to be imported.

I built `libModelTrainer.so` and `libtensorflowlite_flex.so` from tensorflow-r2.14 for Android.

I’m using CMake to build my own .so lib with included tf’s lib. So the first CMakeLists.txt looks like this

```
cmake_minimum_required(VERSION 3.10)
project(ModelTrainerLib VERSION 1.0)

# Specify where to find the header files for TF Lite C++
set(INCLUDE_DIRS
        ${CMAKE_CURRENT_LIST_DIR}/include
        ${CMAKE_CURRENT_LIST_DIR}/include/flatbuffers/include)
include_directories(${INCLUDE_DIRS})

add_library(tflite SHARED IMPORTED)
add_library(tflite-flex SHARED IMPORTED)
set_target_properties(
    tflite PROPERTIES IMPORTED_LOCATION
    ${CMAKE_CURRENT_LIST_DIR}/arm64-v8a/libtensorflowlite.so
)
set_target_properties(
    tflite-flex PROPERTIES IMPORTED_LOCATION
    ${CMAKE_CURRENT_LIST_DIR}/arm64-v8a/libtensorflowlite_flex.so
)

# Source files
set(SOURCES
    ModelTrainer.cpp
)

# Build the main target `ModelTrainer` that will use TF Lite
# Create shared library from sources
add_library(ModelTrainer SHARED ${SOURCES})

# Link the main target with required libs `libtensorflowlite.so`
target_link_libraries(ModelTrainer 
    tflite
    -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex 
    tflite-flex
)
```

Than I link my own .so lib with main.cpp like this:

```
cmake_minimum_required(VERSION 3.13)
project(UseModelTrainerApp)

# Add the executable
add_executable(UseModelTrainerApp main.cpp)

# Create an imported target for libtensorflowlite.so
add_library(libtensorflowlite SHARED IMPORTED)
set_target_properties(libtensorflowlite PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/arm64-v8a/libtensorflowlite.so"
    INTERFACE_INCLUDE_DIRECTORIES "/Users/andrey/Desktop/mnist_tf/lib/include"
)

# Create an imported target for libtensorflowlite_flex.so
add_library(libtensorflowlite_flex SHARED IMPORTED)
set_target_properties(libtensorflowlite_flex PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/arm64-v8a/libtensorflowlite_flex.so"
)

# Do the same for libModelTrainer.so, and specify its dependency on libtensorflowlite
add_library(libModelTrainer SHARED IMPORTED)
set_target_properties(libModelTrainer PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/build_android_arm64/libModelTrainer.so"
    INTERFACE_INCLUDE_DIRECTORIES "/Users/andrey/Desktop/mnist_tf/lib"
)
target_link_libraries(libModelTrainer 
    INTERFACE
    libtensorflowlite 
    -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex 
    libtensorflowlite_flex
)

# Link UseModelTrainerApp with libModelTrainer only, libtensorflowlite will be included automatically
target_link_libraries(UseModelTrainerApp
    -Wl,--no-as-needed
    libModelTrainer
)

# If the library uses TensorFlow Lite or other Android NDK libraries, link them as well
find_library(log-lib log)
target_link_libraries(UseModelTrainerApp ${log-lib})
```

Than I get executable file successfully. I push these files (my own lib, tf’s libs, executable file and model) to my device with adb. Run program and still get error:

> ERROR: Select TensorFlow op(s), included in the given model, is(are) not supported by this interpreter. Make sure you apply/link the Flex delegate before inference. For the Android, it can be resolved by adding “org.tensorflow:tensorflow-lite-select-tf-ops” dependency. See instructions: Selecione operadores do TensorFlow  |  TensorFlow Lite
> 
> ERROR: Node number 5 (FlexSave) failed to prepare.

I have no idea how to fix it, so I will be very grateful for any help.