본문 바로가기
User Guide

안드로이드 라이브러리 만들기 Create an Android Library (8)

by 각종 잡상식 지식 모음 2016. 6. 1.
반응형

Create an Android Library

Android library는 구조적으로 Android app module과 같습니다.
안드로이드 라이브러리에는 source code, resource files, Android manifest 등 app을 만드는데 필요한 모든 것이 포함될 수 있습니다.
하지만 장치에서 가동되는 APK로 컴파일 하는 대신, Android library는 안드로이드 app모듈의 dependency로 사용되는 Android Archive (AAR) 파일로 컴파일됩니다.
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.

라이브러리 모듈은 다음의 경우에 유용합니다:
A library module is useful in the following situations:

  • 복수의 app을 만들 경우
    When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
  • 복수의 APK 변형 안에 존재하는 app을 만들 때
    When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.

위의 어느 경우이든, 재사용 하고자 하는 파일을 간단하게 라이브러리 모듈로 이동시킨 다음 라이브러리를 각각의 app 모듈에 대한 dependency로 추가하면 됩니다.
이 페이지는 위의 두 가지에 대한 방법을 가르칩니다.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module. This page teaches you how to do both.

Create a Library Module


To create a new library module in your project, proceed as follows:

  1. Click File > New > New Module.
  2. In the Create New Module window that appears, click Android Library, then click Next.

    There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects—especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.

  3. Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

Once the Gradle project sync completes, the library module appears in the Project panel on the left. If you don't see the new module folder, make sure it's displaying the Android view.

Convert an app module to a library module

If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:

  1. Open the build.gradle file for the existing app module. At the top, you should see the following:
    apply plugin: 'com.android.application'
  2. Change the plugin assignment as shown here:

    apply plugin: 'com.android.library'
  3. Click Sync Project with Gradle Files.

That's it. The entire structure of the module remains the same, but it now operates as an Android library and the build will now create an AAR file instead of an APK.

Add Your Library as a Dependency


To use your Android library's code in another app module, proceed as follows:

  1. Add the library to your project in one of two ways (if you created the library module within the same project, then it's already there and you can skip this step):
    • Add the compiled AAR (or JAR) file:
      1. Click File > New Module.
      2. Click Import .JAR/.AAR Package then click Next.
      3. Enter the location of the AAR or JAR file then click Finish.
    • Import the library module to your project:
      1. Click File > New > Import Module.
      2. Enter the location of the library module directory then click Finish.

      The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead import the compiled AAR file as described above.

  2. Make sure the library is listed at the top of your settings.gradle file, as shown here for a library named "my-library-module":
    include ':app', ':my-library-module'
  3. Open the app module's build.gradle file and add a new line to the dependencies block as shown in the following snippet:
    dependencies {
        compile project
    (":my-library-module")
    }
  4. Click Sync Project with Gradle Files.

In this example above, the Android library module named my-library- module becomes build dependency for the module in which the build.gradlefile resides.

Any code and resources in the Android library is now accessible to your app module, and the library AAR file is bundled into your APK at build time.

However, if you want to share your AAR file separately, you can find it in project-name/module-name/build/outputs/aar/ and you can regenerate it by clicking Build > Make Project.

Development Considerations


As you develop your library modules and dependent apps, be aware of the following behaviors and limitations.

Once you have added references to library modules to your Android app module, you can set their relative priority. At build time, the libraries are merged with the app one at a time, starting from the lowest priority to the highest.

  • Resource merge conflicts

    The build tools merge resources from a library module with those of a dependent app module. If a given resource ID is defined in both modules, the resource from the app is used.

    If conflicts occur between multiple AAR libraries, then the resource from the library listed first in the dependencies list (toward the top of thedependencies block) is used.

    To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the module (or is unique across all project modules).

  • A library module can include a JAR library

    You can develop a library module that itself includes a JAR library; however you need to manually edit the dependent app modules's build path and add a path to the JAR file.

  • A library module can depend on an external JAR library

    You can develop a library module that depends on an external library. (for example, the Maps external library). In this case, the dependent app must build against a target that includes the external library (for example, the Google APIs Add-On). Note also that both the library module and the dependent app must declare the external library in their manifest files, in a <uses- library> element.

  • Library modules cannot include raw assets

    The tools do not support the use of raw asset files (saved in the assets/ directory) in a library module. Any asset resources used by an app must be stored in the assets/ directory of the app module itself.

  • The app module's minSdkVersion must be equal to or greater than the version defined by the library

    A library is compiled as part of the dependent app module, so the APIs used in the library module must be compatible with the platform version that the app module supports.

  • Each library module creates its own R class

    When you build the dependent app modules, library modules are compiled into an AAR file then added to the app module. Therefore, each library has its own R class, named according to the library's package name. The R class generated from main module and the library module is created in all the packages that are needed including the main module's package and the libraries' packages.


반응형

댓글