Android Studio 本地 C++ 应用的 Android.bp 文件

huangapple go评论83阅读模式
英文:

Android.bp for a Android Studio Native C++ App

问题

以下是要翻译的内容:

"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."

英文:

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

答案1

得分: 0

以下是已翻译的部分:

有大量的示例在AOSP源代码中,位于https://android.googlesource.com 的C++应用程序bp构建文件中。

这是我通过搜索"cc_binary "找到的一个特定示例:

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

您可以看到这个示例可能被简化为hello world的简单形式,最终看起来像:

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

我目前不知道有工具可以从其他类型的构建文件生成bp文件。另一方面,有一种工具可以将Android.bp文件转换为CMake兼容的构建文件,以供在CLion中使用,详见https://android.googlesource.com/platform/build/soong/+/HEAD/docs/clion.md

有关Soong bp文件的更多文档可以在以下位置找到:https://source.android.com/docs/setup/build 和 https://android.googlesource.com/platform/build/soong/+/refs/heads/master/README.md

英文:

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

答案2

得分: 0

我不确定这是否是最佳方法,但我已经创建了一个AIDL接口来绑定Java和C++,然后在Android.bp文件中编译了Java代码作为android_app,AIDL部分作为java_library_static,C++代码作为cc_library_shared

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",
}

这是完整的代码链接:
https://github.com/alvenan/aosp_bench/tree/main/bench_test_jni/JavaNativeTestApp

我的唯一问题是应用程序在system/lib64中查找共享库,而编译将.so 发送到vendor/lib64,但如果我复制到system目录,一切都正常运行。

英文:

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: &quot;libjavanativetestapp&quot;,
    owner: &quot;&#193;lison Ven&#226;ncio &lt;alison.venancio@gmail.com&gt;&quot;,
    vendor: true,
    srcs: [ &quot;app/src/main/cpp/*.cpp&quot; ],
    header_libs: [ &quot;jni_headers&quot; ],
    shared_libs: [ 
        &quot;libutils&quot;,
        &quot;liblog&quot;,
    ],
}

java_library_static {
    name: &quot;vendor.alvenan.javanativetestapp&quot;,
    owner: &quot;&#193;lison Ven&#226;ncio &lt;alison.venancio@gmail.com&gt;&quot;,
    srcs: [ &quot;app/src/main/aidl/**/*.aidl&quot; ],
    installable: true,
    sdk_version: &quot;current&quot;,
}

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

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.

huangapple
  • 本文由 发表于 2023年6月5日 11:50:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定