时三

我善良,并不代表我不邪恶.

分析第一个Android程序

  |  
 阅读次数

    There is no description of the installation, configuration, and operation of Android Studio. If you need it, please visit Baidu or Google. I will directly analyze the project structure of Android, taking the default project as an example.

    Go back to Android Studio, first expand the HellpoWorld project, you will see the project structure shown below.
1

    Any new project will default to the Android project structure, but this is not the actual directory structure of the project, but was converted by Android Studio. The project is simple and straightforward, suitable for rapid development, but may not be easy for newcomers like me. Click the Android area in the figure to convert the project structure mode.
2

    Here we switch the project structure mode to project, which is the real directory structure of the project.
3

Outer directory

.gradle and .idea

    These two directories contain some files automatically generated by Android Studio. We don’t need to care, and don’t edit them manually.

app

    The code, resources, and other content in the project are stored almost entirely in this directory, and our next development work is basically performed in this directory.

build

    This directory does not need to be too concerned, it mainly contains some files that are automatically generated at compile time.

gradle

    This directory contains the gradle wrapper configuration file. The gradle wrapper method does not need to download the gradle in advance, but will automatically decide whether to download the gradle based on the local cache. Android Studio does not have the gradle wrapper method enabled by default. If you need to open it, you can click the Android Studio navigation bar -> File -> Settings -> Build, Execution, Deployment -> Gradle to configure the replacement.

.gitignore

    This file is used to exclude specified directories or files from version control.

build.gradle

    This is the project’s global gradle build script, usually the contents of this file do not need to be modified.

gradle.properties

    This file is a global gradle configuration file, and the properties configured here will affect all gradle compilation scripts in the project.

gradlew and gradlew.bat

    These two files are used to execute the gradle command in the command line interface, where gradlew is used on Linux or Mac systems, and gradlew.bat is used on Windows systems.

HelloWorld.iml

    The iml file is a file that is automatically generated by all InteliJ IDEA projects (Android Studio is based on IntelliJ IDEA), which is used to identify this is an IntelliJ IDEA project, we do not need to modify anything in this file.

local.properties

    This file is used to specify the Android SDK path in this machine. Usually the content is automatically generated, we do not need to modify it. Unless the location of the Android SDK in your machine changes, change the path in this file to a new location.

settings.gradle

    This file is used to specify all imported modules in the project. Since the HelloWorld project has only one app module, only the app module is introduced in the file. Usually, the introduction of modules is done automatically, and the scenes that require us to manually modify this file may be less.
    The outer directory structure of the entire project is now complete. You will find that most files and directories are automatically generated in addition to the app directory, and we don’t need to modify them.

App directory

4

build

    This directory is similar to the outer build directory, and mainly contains some files that are automatically generated at compile time. However, the content inside it will be more complicated, we don’t need too much care.

libs

    If you use a third-party jar package in your project, you need to put these jars in the libs directory. The jars placed in this directory will be automatically added to the build path.

androidTest

    This is used to write Android Test test cases, you can do some automated testing of the project.

java

    There is no doubt that the java directory is where all our Java code is stored. Expand the directory and you will see the HelloWorldActivity file we just created.

res

    There is a lot of content in this directory. To put it simply, all the images, layouts, strings, and other resources that you use in your project should be stored in this directory. Of course, there are many subdirectories under this directory. The images are placed in the drawable directory, the layout is placed in the layout directory, and the strings are placed in the values directory.

AndroidManifest.xml

    This is the configuration file for your entire Android project. All four components you define in the program need to be registered in this file. You can also add a permission declaration to the application in this file.

test

    Here is another way to write a Unit Test test case for automated testing of a project.

.gitignore

    This file is used to exclude specified directories or files within the app module from version control, similar to the outer .gitignore file.

app.iml

    The files generated automatically by the IntelliJ IDEA project, we do not need to care about or modify the contents of this file.

build.gradle

    This is the gradle build script for the app module, which specifies a number of project build related configurations.

proguard-rules.pro

    This file is used to specify the obfuscation rules of the project code. When the code is developed, it is marked as an installation package file. If you do not want the code to be cracked by others, the code will usually be confused, making it difficult for the cracker to read.

    So the directory structure of the entire project is finished.

Analyze project operation

    Next, analyze how the HelloWorld project works.
    First open the Android_Manifest.xml file, you can find the following code:
5

    This code indicates that the HelloWorldActivity activity is registered, and activities that are not registered in AndroidManifest.xml are not available. The two lines of code in the intent-filter are very important, indicating that HelloWorldActivity is the main activity of the project. Click on the application icon on the phone, the first activity is started.

    Activities are the facade of the Android app, and everything you see in the app is in the activity. Therefore, the interface seen in the figure below is actually the activity of HelloWorldActivity.
6

    Take a look at its code and open HelloWorldActivity.java with the code as shown below.
7

    First of all, we can see that HelloWorldActivity is inherited from AppCompatActivity, which is a backward compatible Activity, which can be compatible with Android 2.1 system with the added features and functions of Activity in each system version. Activity is an activity base class provided by the Android system. All activities in our project must inherit it or its subclasses to have the characteristics of the activity (AppCompatActivity is a subclass of Activity). Then you can see that there is an onCreate() method in HelloWorldActivity. This method is a method that must be executed when an activity is created. There are only two lines of code and there is no Hello World!. So where is Hello World! defined?

    In fact, the design of the Android program is about logic and view separation. Therefore, it is not recommended to write the interface directly in the activity. A more general approach is to write the interface in the layout file and then introduce it in the activity. As you can see, the setContenView() method is called on the second line of the onCreate() method. This method introduces a hello_world_layout layout for the current activity, and Hello World! must be defined here!

    The layout files are all defined in the res/layout directory. When you expand the layout directory, you will see the hello_world_layout.xml file. Open the file and switch to the Text view, the code is as follows:
8

    So we have analyzed the directory structure of the HelloWorld project and the basic execution process.

Explain the resources in the project

    If you look at the res directory, there are still quite a lot of things inside, so it’s easy to see people dazzled.
9

    All folders starting with drawable are used to put images. All folders starting with mipmap are used to put application icons. All folders starting with values are used to put strings, colors, styles, etc. Configured, the layout folder is used to put layout files.

    The reason why there are so many folders starting with minmap is actually to make the program more compatible with various devices. The same is true for the rawable folder.

    Know the meaning of each folder in the res directory, let’s see how to use these resources. Open the res/values/strings.xml file:
10

    As you can see, here is a string that defines an application name. We have two ways to reference it.

  • A reference to the string can be obtained from the code via R.string.app_name.
  • A reference to the string can be obtained in @xml/app_name in XML

    The basic syntax is the above two ways, where the string part is replaceable. Here’s a simple example to help understand, open the AndroidManifest.xml file and find the following code:

1
2
3
4
5
6
7
8
9
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>

    Among them, the application icon of the HelloWorld project is specified by the android:icon attribute, and the name of the application is specified by the android:label attribute. As you can see, the way resources are referenced here is exactly what we just learned to reference resources in XML.