How to distribute an Android Library

2019-03-30 02:56发布

I've been spinning a jar for android library project and including this jar in my other apps. But on developer.android.com, I see this statement that I can't distribute a library in a jar:

  • You cannot export a library project to a JAR file

A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

I really don't understand what does that mean.

2条回答
beautiful°
2楼-- · 2019-03-30 03:30

It means that (at the current time) you must distribute your entire project folder. Rather than just a jar file like you can for java libraries.

When someone wants to use your library they will import your project into eclipse, and then in project properties->android they will add your project as an android library.

A few common ways used to distribute a Library project are by using git, or zipping your project folder and making it available online.

查看更多
混吃等死
3楼-- · 2019-03-30 03:32

It is possible to create an Android library project that does not include source code. The limitations are:

  • You still have to ship the resources.

  • You have to rewrite your code to avoid using R. values, as they will be wrong. You will have to look up all resource IDs using getResources().getIdentifier() and/or reflection.

I have the instructions in The Busy Coder's Guide to Advanced Android Development (http://commonsware.com/AdvAndroid), though the instructions are new enough that none of my free versions have them. Quoting some of the instructions from the current edition:

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

Personally, I'd just wait a bit. I am hopeful that the official support for library-projects-as-JARs will be available soonish.

查看更多
登录 后发表回答