ClassLoader usage record: "The Road to Android", Android car recorder Software
I don't like General talk about those metaphysical principles, or remember those established things. People who rely on memories to make up for thinking are easy to fall into the cloud of people. In the end, they will be trapped by memories, and creativity will eventually die out. I hope this series of experts will record my learning about Android. Thus, the stream is merged into the river and becomes the sea.
Next we will analyze the ClassLoader in combination with the code.
An android application is a Dalvik virtual machine that Loads files in Dex format. Loading files in Dex format is what ClassLoader does. So, let's start with the application, because the startup of the application must be inseparable from the ClassLoader.
Let's talk about how to start an application. In "Application of the path to Android", Process is analyzed from the application level. start method. to start an activity, call the start method of Process. The start method of Process is the method of Zygote. Here, the process is clear. The following process appears a little round-robin, Because java on Android is different from other java, and every android application is a process. In order to realize the ideal of one application and one Virtual Machine [this ideal satisfies the possibility that the application can be well protected from interference by other applications, it's just a waste of such effort to protect the data of applications from being infringed by other applications. We can see how much effort google has paid for application security... Google has made a lot of effort on this, so we must thoroughly understand the security mechanisms configured by google in the future ]. The JVM security model is not suitable for mobile phones. Permission management is a big topic, but there are actually two types:
1. protection during the execution period: ensure that the java code being executed is correct and is not infringed.
2. Data protection: Protection against malicious data infringement.
1. First, protection during the execution period. The security features of the class loading architecture, class file validators, and Java virtual machines (and languages) share the same purpose: maintains the internal integrity of Java Virtual Machine instances and running applications so that they are not compromised by malicious or vulnerable code.
2. Second, data protection. Android has its own data protection method. It is a digital signature, similar to qq login.
Obviously, the first protection has a lot to do with this ClassLoader. The second type of protection is more or less related to ClassLoader, because the dex file in the apk file is also a type of private data.
In addition, the classloader of Android is different from the classloader of java. So we have to analyze the code. What is classloader? How does this mysterious God affect permissions?
Tired, typing is really hard. Rest. Continue tomorrow. Start with zygote.
What is the difference between loading a non-class File using ClassLoader and using a File?
This is simple. Use ClassLoader. getSourceAsStream () can only read files under your classpath, while File can specify files under any directory (including your hard disk), which is the most essential difference
For a question about classloader, I wrote a class that inherits ClassLoader.
1. The call format is incorrect.
New must follow the constructor to create an object, while finClass is obviously not the constructor.
MyClassLoader mcl = new MyClassLoader ();
Class myclass = mcl. findClass ();
2. There is such a sentence in the jdk help document.
Network class loaders must define methods findClass and loadClassData to load classes from the network. After downloading the bytes that comprise the class, it should use the defineClass method to create the class instance
And an example
Class NetworkClassLoader extends ClassLoader {
String host;
Int port;
Public Class findClass (String name ){
Byte [] B = loadClassData (name );
Return defineClass (name, B, 0, B. length );
}
Private byte [] loadClassData (String name ){
// Load the class data from the connection
...
}
}
Subclass can directly call the method of the parent class, but there are always exceptions.