I can't link the libtensorflowlite_flex.so file in my C++ TFLite code on Android.
Hi @woojinn8, thanks for reporting your issue. Help me understand your environment a little bit better, are you building for Android on a linux system? Are you testing your model on a linux system or an android system? Have you reviewed this link? https://www.tensorflow.org/lite/guide/ops_select If you are testing your converted model on android please follow the instructions there, otherwise I think you are testing on a linux system from the context of everything. Have you tried enabling SELECT_TF_OPS before conversion?

example:
```python
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
``` > Hi @woojinn8, thanks for reporting your issue. Help me understand your environment a little bit better, are you building for Android on a linux system? Are you testing your model on a linux system or an android system? Have you reviewed this link? https://www.tensorflow.org/lite/guide/ops_select If you are testing your converted model on android please follow the instructions there, otherwise I think you are testing on a linux system from the context of everything. Have you tried enabling SELECT_TF_OPS before conversion?
> 
> example:
> 
> ```python
> import tensorflow as tf
> 
> converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
> converter.target_spec.supported_ops = [
>   tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
>   tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
> ]
> tflite_model = converter.convert()
> open("converted_model.tflite", "wb").write(tflite_model)
> ```

Thank you so much for reply. 
To explain in more detail, 
1) I build my code building for Android on a linux system
2) And I testing my model on android system
 -> I copied the built executable file to an ARM core embedded device with Android 8.1 installed and excute.
3) I also read about ops_select page. So, I used the exact same code you provided as a example for conversion and successfully converted my TF model to a TFLite model.
 -> I enable "tf.lite.OpsSet.TFLITE_BUILTINS" & "tf.lite.OpsSet.SELECT_TF_OPS" when i convert.
 
In this environment, I am still encountering the following error message.
```
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 Android, it can be resolved by adding...
ERROR: Node number 1348 (FlexErf) failed to prepare.
I am unable to identify the cause of the issue. I would greatly appreciate it if someone could help me.
```
 Hi @woojinn8, Thanks for the information, have you followed these instructions? https://www.tensorflow.org/lite/guide/ops_select#android_aar did you build a custom aar or did you use the big one here? https://central.sonatype.com/artifact/org.tensorflow/tensorflow-lite-select-tf-ops/2.12.0

This should be how you link the flex ops on android as stated on the documentation:

```java
dependencies {
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
    // This dependency adds the necessary TF op support.
    implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly-SNAPSHOT'
}
``` > Hi @woojinn8, Thanks for the information, have you followed these instructions? https://www.tensorflow.org/lite/guide/ops_select#android_aar did you build a custom aar or did you use the big one here? https://central.sonatype.com/artifact/org.tensorflow/tensorflow-lite-select-tf-ops/2.12.0
> 
> This should be how you link the flex ops on android as stated on the documentation:
> 
> ```java
> dependencies {
>     implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
>     // This dependency adds the necessary TF op support.
>     implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly-SNAPSHOT'
> }
> ```
Hi @pkgoogle Thanks to reply.

I kindly request your understanding as I am not familiar with Android and Java, so even if my questions may be incorrect, I would appreciate your patience and understanding.
The information you have shown is understood as the settings required for using Select TensorFlow operators when creating an Android application using Java.

What I want to do is similar to [this link](https://github.com/tensorflow/tensorflow/issues/55536), but the difference is that the link runs on Ubuntu, whereas I want to run it on Android.

In my case, I provide a .so library file which built in C for android OS. And I want users to use it either in C or in Java using JNI. 
In this case, do I still need the setup you described? If so, linking the "libtensorflowlite_flex.so" file in a sample implemented in C is not possible?.
 Hi @woojinn8, no worries, it seems I misidentified your workflow previously, you are using C++ with JNI to use C++/tflite on Android, is that correct? Are you doing this entirely outside of Android studio? If so, I recommend you try adjusting your workflow to use Android Studio, as package/linking issues are better managed there. Here's a very simple Android Project which uses JNI to call C++ code, perhaps you can use it as a skeleton to get what you started working: [HelloWorldJNI.zip](https://github.com/tensorflow/tensorflow/files/11873357/HelloWorldJNI.zip). Let me know if that helps or if you run into issues trying out this workflow.
 Thank you @pkgoogle Thank you for advice. I'll try to use Android Studio. 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/60893">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/60893">No</a>

**System information**
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Android
- TensorFlow installed from (source or binary): from source
- TensorFlow version (or github SHA if from source): v2.12.0

I am trying to load a Swin Transformer model using the Flex delegate in TFLite code implemented in C++. After writing the sample code, I built it separately for Ubuntu and Android. While the model loading and execution work fine on Ubuntu, the model doesn't even load on Android.

When converting the model file, I received a message instructing me to use the flex delegate as follows
```
WARNING:absl:Found untraced functions such as gen_tensor_dict while saving (showing 1 of 1). These functions will not be directly callable after loading.
2023-06-16 10:53:45.770323: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:364] Ignored output_format.
2023-06-16 10:53:45.770352: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:367] Ignored drop_control_dependency.
2023-06-16 10:53:45.770982: I tensorflow/cc/saved_model/reader.cc:45] Reading SavedModel from: backbone-0001
2023-06-16 10:53:45.809957: I tensorflow/cc/saved_model/reader.cc:89] Reading meta graph with tags { serve }
2023-06-16 10:53:45.809990: I tensorflow/cc/saved_model/reader.cc:130] Reading SavedModel debug info (if present) from: backbone-0001
2023-06-16 10:53:45.941381: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
2023-06-16 10:53:45.950274: I tensorflow/cc/saved_model/loader.cc:231] Restoring SavedModel bundle.
2023-06-16 10:53:46.286516: I tensorflow/cc/saved_model/loader.cc:215] Running initialization op on SavedModel bundle at path: backbone-0001
2023-06-16 10:53:46.500467: I tensorflow/cc/saved_model/loader.cc:314] SavedModel load for tags { serve }; Status: success: OK. Took 729486 microseconds.
2023-06-16 10:53:47.137098: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2023-06-16 10:53:52.096510: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2051] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s):
Flex ops: FlexErf
Details:
	tf.Erf(tensor<1x196x1536xf32>) -> (tensor<1x196x1536xf32>) : {device = ""}
	tf.Erf(tensor<1x3136x384xf32>) -> (tensor<1x3136x384xf32>) : {device = ""}
	tf.Erf(tensor<1x49x3072xf32>) -> (tensor<1x49x3072xf32>) : {device = ""}
	tf.Erf(tensor<1x784x768xf32>) -> (tensor<1x784x768xf32>) : {device = ""}
See instructions: https://www.tensorflow.org/lite/guide/ops_select
2023-06-16 10:53:52.096741: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2116] Estimated count of arithmetic ops: 11.927 G  ops, equivalently 5.963 G  MACs

Process finished with exit code 0
```

So, I build flex delegate, and link libtensorflowlite_flex.so file to my execute file.
I built the TFLite for Android using the following commands:
```
bazel build -c opt --config=android_arm64 --define=tflite_convert_with_select_tf_ops=true --define=with_select_tf_ops=true //tensorflow/lite/c:libtensorflowlite_c.so
bazel build -c opt --config=android_arm64 --config=monolithic --define=tflite_convert_with_select_tf_ops=true --define=with_select_tf_ops=true //tensorflow/lite/delegates/flex:libtensorflowlite_flex.so
```

However, when I try to run the sample code on Android, I receive an error message similar to what I would get if the Flex delegate was not linked:
```
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 Android, it can be resolved by adding...
ERROR: Node number 1348 (FlexErf) failed to prepare.
I am unable to identify the cause of the issue. I would greatly appreciate it if someone could help me.
```

Of course, I have added the build option "--no-as-needed" and confirmed through the "readelf" command that the built file is linking the libtensorflowlite_flex.so file.

Here is my CMake file
```
cmake_minimum_required(VERSION 3.10)
project(tflite_flex_test C CXX)

set(ATTRIBUTE PRIVATE)
set(CMAKE_EXE_LINKER_FLAGS    "-Wl,--no-as-needed ${CMAKE_EXE_LINKER_FLAGS}")

# TFLite Path Setting
set(TFLITE_INC ${CMAKE_CURRENT_LIST_DIR}/3rdparty/tflite)
set(TFLITE_LIB ${CMAKE_CURRENT_LIST_DIR}/3rdparty/tflite/libtensorflowlite_c.so)
set(TFLITE_FLEX_LIB ${CMAKE_CURRENT_LIST_DIR}/3rdparty/tflite/libtensorflowlite_flex.so)

# OpenCV Path Setting
set(OPENCV_PATH ${CMAKE_CURRENT_LIST_DIR}/3rdparty/opencv_android)
set(OPENCV_INC ${OPENCV_PATH}/sdk/native/jni/include)
set(OpenCV_LIBS ${OPENCV_PATH}/sdk/native/libs/arm64-v8a)

add_executable(tflite_flex_test 
${CMAKE_CURRENT_LIST_DIR}/tflite_flex_test.cpp
)

# For TFLite
target_include_directories(tflite_flex_test ${ATTRIBUTE} ${TFLITE_INC})
target_link_libraries(tflite_flex_test ${TFLITE_LIB} ${TFLITE_FLEX_LIB})


# For OpenCV
target_include_directories(tflite_flex_test ${ATTRIBUTE} ${OPENCV_INC})
target_link_libraries(tflite_flex_test
${OpenCV_LIBS}/libopencv_core.so
${OpenCV_LIBS}/libopencv_features2d.so
${OpenCV_LIBS}/libopencv_highgui.so
${OpenCV_LIBS}/libopencv_imgproc.so
${OpenCV_LIBS}/libopencv_photo.so
${OpenCV_LIBS}/libopencv_video.so
)

target_link_libraries(tflite_flex_test
EGL 
GLESv2
GLESv3
)
find_library(ANDROID_LOG_LIB log)
target_link_libraries(tflite_flex_test log)


```

My code and weight file can download at [this link](https://drive.google.com/file/d/1Bim05zY07IvCmBWrUQvuqoFxzL7uXjwf/view?usp=sharing)
-> Just run "sh build_android.sh" than you can build my code.

I am unable to identify the cause of the issue. I would greatly appreciate it if someone could help me.

