0

Could someone show me an example of Android.bp for the Android Studio Native C++ app to compile it in AOSP source? It can be for the 'hello world' auto generated project.

Is there a tool that auto generates it?

Thank you

Botje
  • 26,269
  • 3
  • 31
  • 41

2 Answers2

0

There are tons of examples in AOSP source at https://android.googlesource.com of C++ app bp build files.

Here is one particular example I found by searching for "cc_binary "

https://android.googlesource.com/platform/external/stressapptest/+/refs/heads/android13-gsi/Android.bp

You can see how that example might be boiled down to hello world simplicity and end up looking like:

cc_binary {
    name: "hello_world",
    srcs: [
        "src/main.cc",
    ],
    cflags: [
        "-DEXAMPLE",
    ],
}

I am not aware of a tool that generates bp files from some other type of build file at this time. On the other hand there is a tool that can take an Android.bp and create a CMake compatible build from it for the purposes of use in CLion, see https://android.googlesource.com/platform/build/soong/+/HEAD/docs/clion.md

More docs on soong bp files can be found at https://source.android.com/docs/setup/build and https://android.googlesource.com/platform/build/soong/+/refs/heads/master/README.md

satur9nine
  • 13,927
  • 5
  • 80
  • 123
  • If you open Android Studio and check "Native C++" project it will generates an app that comunicates Java with C++ using JNI. My point is how to compile this app (Java and C++) with soong as gradle does on Android Studio. Maybe the C++ part is compiled as a shared lib to be accessed by Java. (?) Here is one example compiling with gradle: https://github.com/alvenan/aosp_bench/tree/main/bench_test_jni/JavaNativeTestApp – Álison Venâncio Jun 12 '23 at 19:38
  • I updated the repository in the comment above adding a Android.bp, if you want to see how it was compiling with gradle take a look in the first commit. Here: https://github.com/alvenan/aosp_bench/tree/fb57fe70486b7038cf6394b0009f0b4bb8cb3d78/bench_test_jni/JavaNativeTestApp. I proposed a solution that is working for now but I am not sure if it is the best way. – Álison Venâncio Jun 13 '23 at 00:22
0

I am not sure if this is the best approach but I have created an AIDL inteface to bind Java and C++, and then compilated the java code as a android_app, the AIDL part as java_library_static and the C++ code as cc_library_shared in Android.bp file.

cc_library_shared {
    name: "libjavanativetestapp",
    owner: "Álison Venâncio <alison.venancio@gmail.com>",
    vendor: true,
    srcs: [ "app/src/main/cpp/*.cpp" ],
    header_libs: [ "jni_headers" ],
    shared_libs: [ 
        "libutils",
        "liblog",
    ],
}

java_library_static {
    name: "vendor.alvenan.javanativetestapp",
    owner: "Álison Venâncio <alison.venancio@gmail.com>",
    srcs: [ "app/src/main/aidl/**/*.aidl" ],
    installable: true,
    sdk_version: "current",
}

android_app {
    name: "JavaNativeTestApp",
    owner: "Álison Venâncio <alison.venancio@gmail.com>",
    srcs: [ "app/src/main/java/**/*.java" ],
    resource_dirs: [ "app/src/main/res" ],
    static_libs: [
        "vendor.alvenan.timermanager",
        "vendor.alvenan.javanativetestapp",
        "com.google.android.material_material",
        "androidx-constraintlayout_constraintlayout",
        "androidx.test.ext.junit",
        "androidx.test.espresso.core",
    ],
    required: [
        "vendor.alvenan.javanativetestapp",
        "vendor.alvenan.timermanager",
        "libjavanativetestapp",
    ],
    optimize: {
        enabled: false
    },
    system_ext_specific: true,
    platform_apis: true,
    certificate: "platform",
    use_embedded_native_libs: true,
    privileged: true,
    manifest: "app/src/main/AndroidManifest.xml",
}

Here is the full code: https://github.com/alvenan/aosp_bench/tree/main/bench_test_jni/JavaNativeTestApp

My only issue is that the app is looking for the shared library in system/lib64 while the compilation is sending the .so to vendor/lib64, but if I copy to system, all works fine.

  • 1
    You need to decide if your app should live in system or vendor. Things in vendor are generally hardware specific, these days vendor really means chip or board level. If you aren't writing chip or board specific stuff then remove `vendor: true` so your app ends up in system. – satur9nine Jun 13 '23 at 01:45