When I took the Android course in college in 2017, I vowed that I would never deal with Android again in my life. Then I spent a week studying the teacher’s knowledge points in “The First Line of Code” and recorded the notes on CSDN. .
Five years later, when I was looking for a job, I discovered that my work technology stack was Android. Then I looked back at CSDN articles and found that the Android final exam notes were the most collected posts. . Maybe this is fate. After a year of struggle, I finally convinced myself to enter the Android environment wholeheartedly and start the Android journey again. Maybe this road will take a lifetime. .
1. Android application components
Application components are the basic building blocks of an Android application. These components are organized loosely coupled by the application manifest file. AndroidManifest.xml describes each component of the application and how they interact. The four main components are:
Component | Description |
---|---|
Activities | Describe the UI and handle the user’s interaction with the machine screen. |
Services | Handle background operations associated with the application. |
Broadcast Receivers | Handle communication between the Android operating system and applications. |
Content Providers | Handle data and database management issues. |
(1). Activities
An activity identifies a single screen with a user interface. For example, a mail application might contain an activity for displaying a list of new mails, another activity for composing mails, and yet another activity for reading mails. When an application has more than one activity, one of them will be marked to be displayed when the application starts.
An activity is a subclass of the Activity class, as follows:
public class MainActivity extends Activity {<!-- --> }
(2). Services
Services are components that run in the background and perform long-term operations. For example, a service could be to play music in the background while the user is using different programs, or to obtain data over the network during an activity without blocking user interaction.
A service is a subclass of theService class as follows:
public class MyService extends Service {<!-- --> }
(3). Broadcast Receivers
Broadcast receivers simply respond to broadcast messages sent from other applications or systems. For example, an application can initiate a broadcast to let other applications know that some data has been downloaded to the device and is available to them. The broadcast receiver therefore intercepts these communications and takes appropriate action.
The broadcast receiver is a subclass of the BroadcastReceiver class, and each message is broadcast in the form of an Intent object.
public class MyReceiver extends BroadcastReceiver {<!-- --> }
(4). Content Providers
Content provider components provide data from one application to another through requests. These requests are handled by methods of the ContentResolver class. This data can be stored in the file system, database or other other places.
Content providers are subclasses of the ContentProvider class and implement a set of standard APIs for other applications to perform transactions.
public class MyContentProvider extends ContentProvider {<!-- --> }
(5). Attachment component
There are some additional components used for the construction of the entities mentioned above, the logic between them, and the connections between them. These components are as follows:
Fragments | represents a behavior or part of the user interface in an activity. |
---|---|
Views | UI elements drawn on the screen, including buttons, lists, etc. |
Layouts | Inheritance of View that controls the screen format and displays the appearance of the view. |
Intents | Message connections between components. |
Resources | External elements, such as string resources, constant resources and image resources, etc. |
Manifest | Configuration file for the application. |
2. Android project composition analysis
(1). Overall structure
(1).gradle and .idea
Automatically generated files, no need to worry about it.
(2).app
Contains the code and resources in the project, and development is mainly carried out in this directory.
(3).build
File automatically generated during compilation.
(4).gradle
Contains the configuration file of gradle wrapper, and determines whether you need to download gradle online based on the local cache. Android studio does not have a way to start the gradle wrapper by default. If you need to open it, click File->Settings->Build, Execution, Deplyment->Gradle to make configuration changes.
(5).gitgnore
Used to exclude the specified directory or file from version control.
(6).build.gradle
The global gradle build script of the project. Usually the content in this file does not need to be modified.
(7).gradle.properties
The global gradle configuration file of the project, the properties configured in it will affect all gradle compilation scripts in the project.
(8). gradlew and gradlew.bat
These two files are used to execute gradle commands on the command line interface. The former is on Linux or Mac systems, and the latter is on Windows systems.
(9).local.properties
Used to specify the Android SDK path in the local machine. Usually the content is automatically generated and does not need to be modified.
(10). settings.gradle
Is it used to specify all imported modules in the project. Since there is only one app module in the project, only the app module is introduced in this file. Usually it is introduced automatically and does not require manual modification.
It can be seen that except for the app directory, the rest are basically automatically generated, and the content under the app directory is the focus.
(2). app structure
(1).build
Similar to the outer build directory, it also mainly contains files automatically generated during compilation, but the content is more complex and does not require too much care.
(2).libs
The directory where the third-party jar package is stored. After being stored, the jar package will be automatically added to the build path.
(3). androidTest
Used to write Android Test test cases and perform some automated testing on the project.
(4).java
A place to put all your Java code.
(5).res
Store all images, layouts, strings and other resources that need to be used in the project.
- Pictures are stored in the drawable directory
- The layout is stored in the layout directory
- Strings are stored in the values directory
- Application icons are stored in directories starting with mipmap
(6).AndroidManifest.xml
The configuration file of the entire project, all four major components defined in the program need to be registered in this file. You can also add permission statements to the application in this file.
(7).test
Used to write Unit Test tests.
(8).gitignore
Used to exclude specified directories or files within the app module from version control, similar to .gitignore
in the outer scope.
(9).build.gradle
The gradle build script of the app module will specify many project build-related configurations in the file.
(10). proguard-rules.pro
Used to specify the obfuscation rules of the project code. After the code development is completed, the files are packaged and the code is obfuscated to ensure security.
(3). Android running process analysis
In the AndroidManifest.xml
file there is:
<activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
The above code registers the MainActivity
activity, otherwise this activity cannot be used. intent-filter
These two lines of code indicate that MainActivity
is the main activity of this project. After clicking the application icon, this activity is started first.
In the file MainActivity.java
, there are:
public class MainActivity extends AppCompatActivity {<!-- --> @Override protected void onCreate(Bundle savedInstanceState) {<!-- --> super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Among them, the MainActivity
class inherits from AppCompatActivity
. There is a onCreate()
method in the class, which is a method that must be executed when the activity is created.
The setContentView
method is called after the onCreate()
method, which introduces a activity_main
layout to the current activity, and the last displayed string exists in it. .
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
(4). Detailed explanation of the build.gradle file
Android Studio uses Gradle to build projects.
First look at the outer bulid.gradle
file:
plugins {<!-- --> id 'com.android.application' version '7.2.1' apply false id 'com.android.library' version '7.2.1' apply false } task clean(type: Delete) {<!-- --> delete rootProject.buildDir }
bulid.gradle
file in the app directory:
android {<!-- --> compileSdk 32 defaultConfig {<!-- --> applicationId "com.example.emptyaty" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes {<!-- --> release {<!-- --> minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions {<!-- --> sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies {<!-- --> implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
3. Android logging tool
The log tool class in Android is Log (android.util.Log), which provides the following five methods to print logs.
Log.v()
. Print the most trivial and least meaningful log information. For level verbose, the level is the lowest.Log.d()
. Print debugging information. For level debug, it is one level higher than verbose.Log.i()
. Print more important data. The corresponding level is info, which is one level higher than debug.Log.w()
. Print warning message. There are potential risks in the upgrade program, and the corresponding level is warn, which is one level higher than info.Log.e()
. Print error message. The corresponding level is error, which is one level higher than warn.
Be sure not to use the
system.out.println()
method to print logs. .