I'm building a native library for my Android application with bazel.
I'd like to use some OpenSSL functions on it like this:
#include <jni.h>
#include <openssl/aes.h>
...
AES_encrypt(in, out, key);
How to add the openssl library to bazel build ?
Subsidiary question: which archive I should use ?
openssl-1.1.0c.tar.gz
openssl-1.0.2j.tar.gz
openssl-1.0.1u.tar.gz
openssl-fips-2.0.13.tar.gz
openssl-fips-ecp-2.0.13.tar.gz
What I've tried
I've downloaded the openssl-1.0.2j archive. and added a cc_library entry to my BUILD file.
cc_library(
name = "openssl",
srcs = glob([
"openssl-1.0.2j/crypto/**/*.h",
"openssl-1.0.2j/crypto/**/*.c"
]),
includes = [
"openssl-1.0.2j",
"openssl-1.0.2j/crypto/",
],
visibility = ["//visibility:public"],
)
I've this error:
openssl-1.0.2j/crypto/dh/p512.c:60:24: fatal error: openssl/bn.h: No such file or directory
#include <openssl/bn.h>
But I don't understand why this code is trying to include a file from openssl while it's in openssl-1.0.2j/crypto/
With openssl-1.1.0c
openssl-1.1.0c/include/openssl/e_os2.h:13:34: fatal error: openssl/opensslconf.h: No such file or directory
# include <openssl/opensslconf.h>
Even if I run the Configure command, no opensslconf.h file is generated.
Based on #Ulf Adams' answer, I gave up trying to compile OpenSSL with bazel. I used BoringSSL instead.
BoringSSLis a fork of OpenSSL, and can easily be incorporated to a bazel project by doing:
git_repository(
name = "boringssl",
commit = "_some commit_",
remote = "https://boringssl.googlesource.com/boringssl",
)
Related
Am trying to add the include fwk/base/pkgs/SettingsLib/common.mk onto Settings App, But am unsure how to use it on Android.bp file.
I used the androidmk conversion tool which comes with the AOSP build system to convert Android.mk to Android.bp
What I DID :
1. On AOSPSource code, check build/soong/androidmk and make full build.
2. After compiling the source code, the androidmk generation tool located :
# out/soong/host/linux-x86/bin/androidmk
3. Using the androidmk command try converting my Android.mk to Android.bp
# androidmk Android.mk > Android.bp
Error inside Android.bp
20 // **ANDROIDMK TRANSLATION ERROR: unsupported include**
21 // include frameworks/base/packages/SettingsLib/search/common.mk$
Any suggestions ? Thanks in advance.
Found way to convert Android.mk to Android.bp and import on App.
Core AOSP androidmk tool does NOT help, We need to manually create a SettingsLibDefaults on SettingsLibs ( create a Defaults on any libs) . Referred AOSP SettingsLib Android.bp file.
// NOTE: Keep this module in sync with ./common.mk
java_defaults {
name: "SettingsLibDefaults",
static_libs: [
"androidx.annotation_annotation",
"androidx.lifecycle_lifecycle-common",
"androidx.legacy_legacy-support-v4",
"androidx.lifecycle_lifecycle-runtime",
"androidx.recyclerview_recyclerview",
"androidx.preference_preference",
"androidx.appcompat_appcompat",
"androidx.legacy_legacy-preference-v14",
"SettingsLib",
],
}
Usage of SettingsLibDefaults on Settings App Android.bp file
android_library {
name: "Settings-core",
platform_apis: true,
defaults: [
"SettingsLibDefaults",
"SettingsLib-search-defaults",
],
Same way we need to create a Defaults on any libs Android.bp file and import on Apps.
I'm trying to use JNI with Bazel (0.12.0):
WORKSPACE file:
android_ndk_repository(
name = "androidndk",
)
libs/hello_lib_c/BUILD:
cc_library(
name = "hello_lib_c",
srcs = ["src/hello.c"],
visibility = ["//visibility:public"],
)
libs/hello_lib_c/src/hello.c:
#include <jni.h>
JNIEXPORT jstring JNICALL
Java_eu_tamere_bazel_HelloJNI_hello(JNIEnv *env, jclass clazz) {
return (*env)->NewStringUTF(env, "Hello from JNI ");
}
libs/hello_lib_java/BUILD:
android_library(
name = "hello_lib_java",
srcs = glob(["src/eu/tamere/bazel/**"]),
deps = ["//libs/hello_lib_c"],
visibility = ["//visibility:public"],
)
When building the Java lib, the jni.h header file is not found. Any idea on how to declare the dependency?
$ bazel build //libs/hello_lib_java
INFO: Analysed target //libs/hello_lib_java:hello_lib_java (22 packages loaded).
INFO: Found 1 target...
ERROR: /path/to/project/libs/hello_lib_c/BUILD:1:1: C++ compilation of rule '//libs/hello_lib_c:hello_lib_c' failed (Exit 1)
libs/hello_lib_c/src/hello.c:1:10: fatal error: jni.h: No such file or directory
#include <jni.h>
^~~~~~~
compilation terminated.
Target //libs/hello_lib_java:hello_lib_java failed to build
Use --verbose_failures to see the command lines of failed build steps.
I've tried to add hdrs = ["#androidndk//:jni_header"], to the cc_library definition but it does now work either. I've also tried to explicitly set the path for the Android NDK in the WORKSPACEbut it does not change.
The android example on the official repo does not specify where to find the jni lib.
It seems that using JNI inside an android_library (i.e., .aar) is not possible at the moment without some tricks. Only android_binary (i.e., .apk) will link the jni lib.
From https://github.com/bazelbuild/bazel/issues/348 :
android_library .aar output does not currently support bundling native libraries. This is a known deficiency that I believe #dkelmer has plans to work on.
aj-michael has a repo with a workaround:
https://github.com/aj-michael/aar_with_jni
I am creating an encryption library in android studio by android NDK. I am using CMakeLists.txt script.
In the C++ file, I want to include #include <openssl/sha.h>.
How can I include OpenSSL in my c++ file?
What you need is to have in CMakeLists.txt a line like
INCLUDE_DIRECTORIES(SYSTEM "/path/to/openssl")
then in the C++ source file you simply
#include <sha.h> // or #include <openssl/sha.h>
I'm trying to compile to android environment. And because of that I get the following error:
error: openssl/aes.h: No such file or directory
I find some solution in stack but, I don't get how to end the process to be able to compile.
I already compiled one version of openssl were should I add the libs? or how can I generate the *.a?
Do you know how can I add this library to the arm-linux-androideabi-g++ that I need to run to be able to pass this problem?
[1] Get openssl library which has aes.h file in its include folder.
[2] If you have compiled openssl library in your lib folder then add to -lssl or -lopenssl to your command line.
Here you can find openssl includes: openssl
Download this includes and put them in some folder in your project, i.e. project_dir/module_dir/jni/openssl-includes.
Then you need set a LOCAL_C_INCLUDES variable in your Android.mk (which also is in jni folder:
LOCAL_C_INCLUDES += ./openssl-includes
After that, you can include files in openssl-includes folder directly by name, i.e.:
#include <aes.h>
If you need an *.a file as output, you should include BUILD_STATIC_LIBRARY in Android.mk, if you need a *.so lib, include BUILD_SHARED_LIBRARY.
now, i have a question: first of all i already get the ndk standalone tool chain ---arm-linux-androideabi-g++ sucessful; now i have to write a commandline c++ program which will use libcurl to do http requests, i can successful compile it on my mac( which has the libcurl default? ) use g++, but when i use the arm-linux-androideabi-g++ to compile it, it produce following error:
arm-linux-androideabi-g++ -std=c++11 -lcurl upload.cpp -o upload
upload.cpp:12:23: fatal error: curl/curl.h: no such file or directory
compilation terminated
i have a libcurl(include files and .a lib file) which can used in android, so howto configure the arm-linux-androideabi-g++ so that it can compile it successfully? thanks advance
I guess you include your header like
#include <curl/curl.h>
if you do that, arm-linux-androideabi-g++ searches header file in some specific location. and if arm-linux-androideabi-g++ does not point same directory with g++, headers can not be found. try to copy just header files of curl to your project, jni/curl folder, and use " instead of <
#include "curl/curl.h"
if you have right .a file, functions in header files will be pointed to lib file successfully.