Search code examples
linuxmakefileandroid-buildbuild-system

Build project with Android.bp for GNU/Linux system


I'm developing a command line tool which I run in Android; but I would also like to compile and run it stand alone on a desktop system, for instance a Ubuntu GNU/Linux system.

Currently it use an Android.bp file and is built it in AOSP.

How could I build for GNU/Linux system using the Android.bp?

I could of course just rewrite the Android.bp as a plain old Makefile but I would prefer not to create this extra layer of code to maintain.


Solution

  • You can specify which target your module should be built for: Android, Host, or Both. Host means GNU/Linux if that is what you build the AOSP in.

    Android

    This is the typical binary module to be built for the device architecture.

    cc_binary {
        name: "my-binary",
        srcs: [ "main.cpp" ],
        shared_libs: [ "libcutils" ]
    }
    

    Host

    There are multiple _host module types (e.g. cc_binary_host, cc_test_host, java_binary_host) that will create host binaries.

    cc_binary_host {
        name: "my-binary-host",
        srcs: [ "main.cpp" ],
        shared_libs: [ "libcutils" ]
    }
    

    Both

    If you want to build both, a device binary and a host binary, you can use host_supported: true.

    cc_binary {
        name: "my-binary",
        srcs: [ "main.cpp" ],
        shared_libs: [ "libcutils" ],
        host_supported: true
    }
    

    You might want to specify additional flags, defines, sources, etc. for android or host. You can do that with the target property:

    cc_binary {
        name: "my-binary",
        srcs: [ "main.cpp" ],
        shared_libs: [ "libcutils" ],
        host_supported: true,
    
        target: {
            android: {
                // android specific properties
            },
            host: {
                // host-side specific properties
            }
        }
    }
    

    A note on module dependencies

    Every module another module depends on needs to be supported for the same target.

    Example: a cc_binary_host cannot depend on a cc_library with host_supported: false.