Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Java libraries

Tags:

java

android

jar

These questions are my attempt at understanding how Android handles JAR files and external libraries, etc. It's conceptual.

In Java, there is a JAR file rt.jar that comes with the JRE, so that every application has access to the java core libraries.

  1. Does the Java classloader load classes only as they're needed by the application?

  2. Is rt.jar equivalent to android's /system/framework/ jars?

  3. When we develop an app with the SDK, we include android.jar from one of the android APIs. Is android.jar essentially the compiled AOSP for that API (minus some hidden libraries)?

  4. android.jar isn't actually included with an APK, is it? Once on a device, the APK is using /system/framework/framework.jar, correct?

  5. If yes to 4, how does android tell an application to use that library? Is this something I can view the source code for?

  6. When we add a JAR to an Android project, is this just like including a static library? The code from the library is just compiled into classes.dex like the rest of the project?

  7. When we add an android library project to an application, is it also compiled into classes.dex? The only difference between this and 6 is that a library project can contain resources?

  8. If every app is referencing /system/framework/framework.jar (and others, like services.jar etc.), then are there a bunch of instances of that library running at one time on the device? Is it like a dynamic library that gets loaded into memory multiple times?

I'd love answers to all these questions, but I'd also appreciate suggestions to some good places to read about these things.

like image 639
samGbos Avatar asked Oct 31 '22 19:10

samGbos


1 Answers

Correct me if I'm wrong in any of the answers and I'll edit:

1.- Android has been JIT for years but there's a new runtime that enforces AOT to improve performance now that most devices can handle it: Dalvik ART

2.- ??

3-5.- The android jar is provided by the IDE for compiling purposes but the actual implementation is loaded by the operative system, that's how they achieve backwards compatibility (and fragmentation) and the same APKs working on cooked roms like Samsung's or Amazon's.

6.- Yes, everything is packed in classes.dex and there's a hardcap of 65k methods between all dependencies. There are some compiler-time substitutions for dependencies that may be already included in the OS.

7.- I am not sure about the new AARs but modules or other projects are compiled and bundled together, yes.

8.- There is a shared memory space for those things, along with system assets: Commonsware - What is Dalvik section Memory Consumption.

like image 85
MLProgrammer-CiM Avatar answered Nov 11 '22 11:11

MLProgrammer-CiM