Build library project

2019-02-11 03:02发布

问题:

all. I've created an android library project and it's works perfectly when i reference it from main project. But when i build the library project apart it doesn't contains R.java and resources. Is there way to build a library project with resources and R.java?

回答1:

It's not possible now.

Now we can create a binary-only library project via the following steps:

  1. Create an Android library project, with your source code and such – this is your master project, from which you will create a version of the library project for distribution
  2. Compile the Java source (e.g., ant compile) and turn it into a JAR file
  3. Create a distribution Android library project, with the same resources as the master library project, but no source code
  4. Put the JAR file in the distribution Android library project's libs/ directory The resulting distribution Android library project will have everything a main project will need, just without the source code.

There is some restrictions in this solution:

  1. We still have to ship the resources.
  2. We have to rewrite our code to avoid using R. values, as they will be wrong. We will have to look up all resource IDs using getResources().getIdentifier() and/or reflection.


回答2:

I use Eclipse and never manually build my Android Library Project independently, but I think the development considerations stated on official dev guide here should answer your question:

Each library project creates its own R class

When you build the dependent application project, library projects are compiled and merged with the application project. Each library has its own R class, named according to the library's package name. The R class generated from main project and the library project is created in all the packages that are needed including the main project's package and the libraries' packages.

Update with Another note quoted from the official dev giude Library Projects:

However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.