简体   繁体   中英

Difference between Android library and Java library in Android Studio

在此处输入图片说明

When creating a new module in Android Studio we can choose between an Android Library and a Java Library. From tinkering with both, my understanding is that an Android Library has access to Android resources while a Java Library only has access to Java resources.

So creating a Java Library in Android Studio is like creating a new library in an ordinary Java IDE such as Eclipse. (It even seems to give us access to RMI, which Android does not support.)

Is this correct, are there any other differences one should be aware of?

Android library it is "android library project" in the past.

It just android module, that can be included to your project. it has android resources as you say, and your own project has access to included library resources(like styles or layouts) too.

This library type have to be use to create custom views or activities or other android elements

Like the small description of each project type says, a Android Library is just another Android Application project,often referred to as Module.And Java Library is another word for a java project.The only difference between a module and a project is the complexity. Take a look at this nice description from intellij about modules and how they differ from :

Module-based project structure

This feature is ideal for complex projects, with multiple internal dependencies, and especially for J2EE projects. Module is a separate logical part of a project that incorporates your working sources, libraries, reference to target Java SDK, etc. It can be compiled, run or debugged as a standalone entity.

在此处输入图片说明

A project may consist of one or multiple modules. Modules may depend on each other. Modules and libraries can be easily shared among multiple projects.

source

This is also a good read.

The android library is a aar and the java library is a jar. Technically speaking both are jars, but their structural composition is slightly different from one another. For example an aar look like this:

res  <android resources dir>
values
AndroidManifest.xml
classes.jar
    <classpath structure>
         <.class's>
R.txt

The java library looks like this:

<classpath structure>
     <.class's>
META-INF

As mentioned, the java library cannot include any android resources such as layouts, values, colors, styles. An android library can include android resources, but it cannot contain any raw android assets (ie anything you put in the assets folder) It is also the developer's responsibility that resource names don't collide with one another. The android developers recommendation is that you assign unique prefixes for your resource names in each android library. Java libraries cannot reference any android SDK classes such as android.content.Context, etc.) There are some tricks you could do to get around that last restriction, but that involves transforming an aar into a jar. This could be done, as long as you're not using any android resources in the aar. The reason one might want to do that transformation is so you can have a "compileOnly" dependency. Currently, you cannot have a compileOnly dependency on an aar library, but you can on a jar library. This technique is used by OSGI application developers who need to reference interfaces that contain android SDK classes. There is an outstanding request to the android developer's team to modify this compilieOnly restriction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM