tensorflow android with react-native can't work normally - android

I put react-native.jar to tensorflow android's directory and add configuration to tensorflow/examples/android/BUILD file like this:
java_import(
name = "react-native",
jars = [
"react-native-0.39.2-sources.jar",
],
)
Now run bazel build //tensorflow/examples/android:tensorflow_demo work well. But then use import com.facebook.react.*; in java file and run bazel build ... again, it throws a nonexistent error, can't find com.facebook.react.* package.

In order to expose the classes in the JAR to the Java code in the Android build, you need to add a dependency in the android_binary that you are building on the java_import you have created.
For example:
# tensorflow/examples/android/BUILD
java_import(
name = "react-native",
jars = [
"react-native-0.39.2-sources.jar",
],
)
android_binary(
name = "tensorflow_demo",
srcs = glob(["src/**/*.java"]),
deps = [
":tensorflow_native_libs",
"//tensorflow/contrib/android:android_tensorflow_inference_java",
":react-native",
],
)

Related

Android rebuilds static library when the binary is compiled

I have a binary in Android that links to a static library A. Static library libA depends on multiple shared libraries.
The binary does not do anything except it imports a class from the static library and executes a simple function.
However, the binary fails to build except I link against the same shared libraries to which the static library A is linked because the compiler tries to recompile libA with the build config of the binary.
Here is my Android.bp of the static library:
cc_library_static {
name: "libA",
relative_install_path: "hw",
vendor: true,
rtti: true,
cflags: [
"-Wall",
"-Wextra",
"-g",
"-DUNIT_TEST",
"-fexceptions"
],
srcs: [
"libA.cpp",
],
shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog"
],
header_libs: [
"lib_a_stub_headers",
],
whole_static_libs: [
"lib_a_stub",
],
export_include_dirs: ["."]
}
Here is my Android.bp for the binary:
cc_binary{
name: "simplebinary",
relative_install_path: "hw",
vendor: true,
cflags: [
"-fexceptions"
],
whole_static_libs: [
"libA"
],
shared_libs: [
"vendor.test.hal#1.0",
],
srcs: [
"simplebinary.cpp",
],
}
The build of the binary fails with:
libA.hpp:4:10: fatal error: 'lib/lib.hpp' file not found
I'm building using the command mm
According to the error message, the compiler cannot find a header file in its header search path. Header includes are resolved during the preprocessor stage, therefore this is not a linking problem. The preprocessor runs at the beginning of compilation, the linking is done at the end.
From your description, I understand that the code for simplebinary includes the header libA.hpp, provided by libA. I understand that libA.hpp is contained in the same directory as the Android.bp that defines the libA module. Because of the export_include_dirs: ["."], this directory is added to the header search path for the compilation of simplebinary. Therefore, the compiler can find libA.hpp when compiling simplebinary.
Now libA.hpp includes CommonAPI/CommonAPI.hpp. I do not know to which library this header belongs. I assume the header belongs to some library libB, and libA links against libB. I further assume that libB has export_include_dirs set to point to the folder containing CommonAPI/CommonAPI.hpp. You can then make libA re-export this header by adding export_shared_lib_headers: ["libB"] to the module declaration of libA. If libB is not a shared library, you would have to use export_static_lib_headers or export_header_lib_headers instead (reference).

How to import OpenCV android-sdk to my project which is using Bazel?

I have been trying to import opencv-android-sdk to my Bazel project but I am not able to do it.
I tried the this answer on SO but while building my project I get errors that
error: package org.opencv.android does not exist
I see that there's an opencv-android artifact on Maven.
You can depend on this using rules_jvm_external.
In your WORKSPACE file, specify the dependency along with the other external dependencies:
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"org.opencv:opencv-android:1.0.1",
# ...
],
repositories = [
"https://maven.google.com",
"https://jcenter.bintray.com",
],
)
Then, in your BUILD file containing your Android targets, depend on the OpenCV target:
android_library(
name = "my_lib",
custom_package = "com.example.bazel",
srcs = glob(["java/com/example/bazel/*.java"]),
manifest = "java/AndroidManifest.xml",
resource_files = glob(["res/**"]),
deps = [
"#maven//:org_opencv_opencv_android",
],
visibility = ["//src/test:__subpackages__"]
)
Finally, you should be able to reference classes like org.opencv.core.Core in your Android Java code.
P.S. consider switching all your maven_jar and gmaven_rules/gmaven_artifact to use rules_jvm_external. The former Maven rules have been deprecated in favor of rules_jvm_external.

How do I set up Buck with Dagger & Butterknife?

Hello my fellow developers,
I'm trying to switch from Gradle to Buck and have problems setting it up with Dagger and Butterknife since they need annotation processing.
I found a few links and tutorials, but they are already a few (~4) years old and I thought there might be a little bit newer approach on doing things.
This is what I looked at already:
https://github.com/ryan-endacott/android-buck-dagger-starter
Thanks in advance,
Patrick
Buck supports annotation processing for Java-based rules (including android_library). Take a look at how Immutables are used in buck repository (link to source code):
First of all, you need to declare library jars:
java_library(
name = "immutables",
exported_deps = [
":builder",
":value",
],
visibility = [
"PUBLIC",
],
)
prebuilt_jar(
name = "value",
binary_jar = "value-2.5.6.jar",
source_jar = "value-2.5.6-sources.jar",
licenses = [
"COPYING",
],
)
prebuilt_jar(
name = "builder",
binary_jar = "builder-2.5.6.jar",
source_jar = "builder-2.5.6-sources.jar",
licenses = [
"COPYING",
],
)
Define annotation processor:
java_annotation_processor(
name = "processor",
isolate_class_loader = True,
processor_class = "org.immutables.value.internal.$processor$.$Processor",
visibility = [
"PUBLIC",
],
deps = [
":immutables",
],
)
You can then add this annotation processor to the list of plugins of the targets:
java_library(
name = "target",
plugins = [
"//third-party/java/immutables:processor",
],
deps = [
...
],
...
)
Buck repository contains a custom rule (java_immutables_library) that adds this annotation processor so that developers don't need to specify plugins on every target. You can probably use similar approach.

Android Data Binding in Make file (Android.mk)

As per a lot of examples, android data bindings are enabled by putting the following snippet in build.gradle file:
android {
....
dataBinding {
enabled = true
}
}
However, I'm working on an android project in AOSP which builds using the make file (Android.mk).
So my question is: How can we add support for data-binding dependencies in android make file?
I found a solution that works for me!
The first hurdle to climb will look something like this: error: package net.mulliken.pinenotenotebook.databinding does not exist
I found that Android Studio automatically generates these files. It was in app/build/generated/data_binding_base_class_source_out/debug/out/net/mulliken/pinenotenotebook/databinding. In order to incorporate this in my build I made a symbolic link from my Android studio workspace to databinding_src in my packages folder.
After that it still didn't work because it could not find the view binding package. You will probably get an error like this: error: package androidx.viewbinding does not exist
I found that google has a repo that includes this package and so I cloned it into my AOSP workspace under frameworks.
[me aosp/frameworks] $ git clone -b studio-main https://android.googlesource.com/platform/frameworks/data-binding data-binding
I then created a new symbolic link from that path into my package directory so that the compiler could find that class:
[me packages/apps/MyAPP] $ ln -s ../../../../frameworks/data-binding/extensions/viewbinding/src/main/java/ androidx_viewbinding_src
At the end of the day my Android.bp file looks like this:
android_app {
name: "PineNoteNotebook",
static_libs: [
"androidx.appcompat_appcompat",
"com.google.android.material_material",
"androidx-constraintlayout_constraintlayout",
"androidx.navigation_navigation-fragment",
"androidx.navigation_navigation-ui",
],
certificate: "platform",
srcs: [
"./**/*.java",
],
resource_dirs: ["res"],
product_specific: true,
sdk_version: "current",
optimize: {
enabled: false
},
required: ["libpinenote"],
}
And my package tree looks like this:
.
├── Android.bp
├── AndroidManifest.xml -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/AndroidManifest.xml
├── androidx_viewbinding_src -> ../../../../frameworks/data-binding/extensions/viewbinding/src/main/java/
├── databinding_src -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/build/generated/data_binding_base_class_source_out/debug/out
├── res -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/res/
└── src -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/java/
i think you must use the latest android studio 3.6 version , with the latest ndk +
add this to your android gradle
android {
...
viewBinding {
enabled = true
}
}
check this : https://developer.android.com/topic/libraries/view-binding

Are linker flags (linkopts) propagats in Bazel?

I'm building an Android library based on Tensorflow with Bazel.
Here the BUILD file
cc_binary(
name = "libfoo.so",
srcs = glob([
"jni/**/*.cc",
"jni/**/*.h",
]),
copts = [ "-fexceptions", "-DEIGEN_AVOID_STL_ARRAY",
"-mfpu=neon", "-std=c++11",
"-DMIN_LOG_LEVEL=0", "-DTF_LEAN_BINARY",
"-O2", ],
linkopts = [
"-llog",
"-lm",
],
linkshared = 1,
deps = [
"#org_tensorflow//tensorflow/core:android_tensorflow_lib",
"#boringssl//:crypto",
],
)
The linker complains about not finding -lpthread, while I didn't add this flag to linkopts.
I've checked the executed command, and in fact there is extra flags on it: -lz -lpthread ...
Where did they came from ? Is there a workaround for this ?
I got the answer from the tensorflow's issue tracker.
#jart
Since copts and linkopts are viral and propagate to dependencies, -lpthread is most likely being inherited from #boringssl//:crypto
#andrewharp
-lpthread is not necessary or possible on Android, so it sounds like the solution would be to add another condition for the select statement as in the linked commit google/protobuf#1386:
...
The only other workaround I know of that doesn't require editing the other repository is to create a dummy libpthread.so target, but that's pretty hacky.

Categories

Resources