1. Requirements
Whether on the. net or java platform, a reasonable layered architecture is one of the most common modular ideas.
Dll and jar files are everywhere.
One day, the tx team told me that we are now going to build three Forum clients on android. One is a news forum, the other is a literature forum, and the other is a music forum. Except for data, the interface is different from that of a few modules.
Jar is something I first thought of, but shared jar files cannot easily share resources or overwrite resources.
And we will not be stupid to do three clients separately, nor will we be stupid to do three clients and then silly copy3, and modify them in turn.
2. Problem
In the Android project, how can I develop a shared Framework that can share resources, code, but also expand?
3. Hard Coding
Someone proposed a scheme to judge in the Code:
Copy codeThe Code is as follows: if (news forum ){
// Load news forum Resources
// Execute the code of the news forum
// Display the effect of a news forum
} Else if (Literature Forum ){
// Load resources of the Literature Forum
// Execute the code of the literary Forum
// Display the effect of the literary Forum
} Else if (Music Forum ){
// Load Music Forum resources
// Execute the Music Forum code
// Display the effect of the Music Forum
}
PS: This is a helpless method. The code is ugly and has poor scalability. Is there a better solution?
4. Use Android Library to separate projects
The Android Library provides a concept similar to libraries on other platforms. This Library not only shares code, but also shares resources and overwrites resources.
The Android Library is used to reuse the project. The project that references it is actually to reference its code and then generate R. java and the resources required by the main project, and remove the resources covered in the Library. AndroidManifest in the Library. xml cannot be reused, that is, the main project must redefine AndroidManifest. xml, which is easy to understand, AndroidManifest. xml is a configuration file of the Project. Generally, it does not have the versatility of the shared library.
The procedure is as follows:
Step 1: create a common Android project.
Step 2: Right-click common and select Properties. On the Android tab, select "Is Library", as shown in.
Step 3: Create the main project news forum tianxianews.
Step 4: Right-click tianxianews, select Properties, click Add on the Android tab, and select common. For example.
View the final project structure:
At this moment, the common project is seamlessly part of the tianxianews project.
Expand the gen directory of the tianxianews project:
Eclipse generates two R. java, a common project and a tianxianews project, but the content is the same. The advantage of doing so is: R. java contains two engineering resources. First, the tianxianews project passes through R. java can call the resources of the common project. Second, if there are overwriting resources, the tianxianews project overwrites the resources of the common project when the apk is generated, because two R. the java content is the same. R. resources in java actually call tianxianews resources. This feature is similar to the Object-oriented Inheritance concept.
We can also create tianxiamusic and tianxialiterature projects with reference to common, so that the three projects can share common, so that we can only develop in one common project.
Here, I didn't come up with the specific steps, mainly because there was no technology or anything to say, that is, two words: overwrite.
5. Summary
Android Library provides a good tool or method for Android modularization and engineering extension. With the increase of projects, the demand for resource sharing, modularity, and expansion is becoming more and more prominent. It not only removes the ugliness of code, but also reduces the development volume and brings a lot of benefits. At the same time, we can also see that Android is not mature. They provide too few solutions for our large-scale projects. This may also be due to the limitations of the mobile phone platform itself, we hope that the Android Market will become more powerful and the technology will become more powerful.