[ Web Proxy ]
URL:
Viewing: https://source.android.com/docs/setup/build/java-library [Back]  [Original]

Implement Java SDK library  |  Android Open Source Project Skip to main content
Android Open Source Project [Android Open Source Project]

Implement Java SDK library Stay organized with collections Save and categorize content based on your preferences.

The Android platform contains a large number of shared Java libraries that can optionally be included in the classpath of apps with the <uses-library> tag in the app manifest. Apps link against these libraries, so treat them like the rest of the Android API in terms of compatibility, API review, and tooling support. Note, however, that most libraries don't have these features.

The java_sdk_library module type helps manage libraries of this kind. Device manufacturers can use this mechanism for their own shared Java libraries, to maintain backward compatibility for their APIs. If device manufacturers use their own shared Java libraries through the <uses-library> tag instead of the bootclass path, java_sdk_library can verify that those Java libraries are API-stable.

The java_sdk_library implements optional SDK APIs for use by apps. Libraries implemented through java_sdk_library in your build file (Android.bp) perform the following operations:

How java_sdk_library works

A java_sdk_library called X creates the following:

  1. Two copies of the implementation library: one library called X and another called X.impl. Library X is installed on-device. Library X.impl is there only if explicit access to the implementation library is needed by other modules, such as for use in testing. Note that explicit access is rarely needed.
  2. Scopes can be enabled and disabled to customize access. (Similar to Java keyword-access modifiers, a public scope provides a wide range of access; a test scope contains APIs only used in testing.) For each enabled scope the library creates the following:

If a java_sdk_library is called X, and is being compiled against as X, always refer to it that way and dont modify it. The build will select an appropriate library. To ensure that you have the most appropriate library, inspect your stubs to see if the build introduced errors. Make any necessary corrections using this guidance:

java_sdk_library X usage

The implementation library X gets used when its referenced from apex.java_libs. However, due to a Soong limitation, when library X is referenced from another java_sdk_library module within the same APEX library, X.impl explicitly must be used, not library X.

When the java_sdk_library is referenced from elsewhere, a stubs library is used. The stubs library is selected according to the depending modules sdk_version property setting. For example, a module that specifies sdk_version: "current" uses the public stubs, whereas a module that specifies sdk_version: "system_current" uses the system stubs. If an exact match cant be found, the closest stub library gets used. A java_sdk_library that only provides a public API will supply the public stubs for everyone.

Build flow with Java SDK library [Build flow with Java SDK library] Figure 1. Build flow with Java SDK library

Examples and sources

The srcs and api_packages properties must be present in the java_sdk_library.

java_sdk_library {
        name: "com.android.future.usb.accessory",
        srcs: ["src/**/*.java"],
        api_packages: ["com.android.future.usb"],
    }

AOSP recommends (but doesnt require) that new java_sdk_library instances explicitly enable the API scopes they want to use. You can also (optionally) migrate existing java_sdk_library instances to explicitly enable the API scopes theyll use:

java_sdk_library {
         name: "lib",
         public: {
           enabled: true,
         },
         system: {
           enabled: true,
         },
         
    }

To configure the impl library used for runtime, use all the normal java_library properties, such as hostdex, compile_dex, and errorprone.

java_sdk_library {
        name: "android.test.base",

        srcs: ["src/**/*.java"],

        errorprone: {
          javacflags: ["-Xep:DepAnn:ERROR"],
        },

        hostdex: true,

        api_packages: [
            "android.test",
            "android.test.suitebuilder.annotation",
            "com.android.internal.util",
            "junit.framework",
        ],

        compile_dex: true,
    }

To configure stubs libraries, use the following properties:

The java_sdk_library is a java_library, but it isnt a droidstubs module and so doesn't support all of the droidstubs properties. The following example was taken from the android.test.mock library build file.

java_sdk_library {
        name: "android.test.mock",

        srcs: [":android-test-mock-sources"],
        api_srcs: [
            // Note: The following arent APIs of this library. Only APIs under the
            // android.test.mock package are taken. These do provide private APIs
            // to which android.test.mock APIs reference. These classes are present
            // in source code form to access necessary comments that disappear when
            // the classes are compiled into a Jar library.
            ":framework-core-sources-for-test-mock",
            ":framework_native_aidl",
        ],

        libs: [
            "framework",
            "framework-annotations-lib",
            "app-compat-annotations",
            "Unsupportedappusage",
        ],

        api_packages: [
            "android.test.mock",
        ],
        permitted_packages: [
            "android.test.mock",
        ],
        compile_dex: true,
        default_to_stubs: true,
    }

Maintain backward compatibility

The build system checks whether the APIs have maintained backward compatibility by comparing the latest API files with the generated API files at build time. The java_sdk_library performs the compatibility check using the information provided by prebuilt_apis. All libraries built with java_sdk_library must have API files in the latest version of api_dirs in prebuilt_apis. When you release the version, the API lists files and stubs libraries can be obtained with dist build with PRODUCT=sdk_phone_armv7-sdk.

The api_dirs property is list of API version directories in prebuilt_apis. The API-version directories must be located at the Android.bp directory level.

prebuilt_apis {
       name: "foo",
       api_dirs: [
           "1",
           "2",
             ....
           "30",
           "current",
       ],
    }

Configure the directories with the version/scope/api/ structure under the prebuilts directory. version corresponds to the API level and scope defines whether the directory is public, system, or test.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-06-17 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-06-17 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page