Tools used
The android project now uses android studio for development, and the recommended language for development is kotlin, but here we are still using Java for development first.
AndroidStudio 版本: Android Studio Iguana | 2023.2.1
Project Language: Java
JDK version: 17
Creating Projects with AndroidStudio
After the project is created, the overall project structure is shown below It should be noted that this project structure is displayed in Android Studio using the project view, not the android view, and is divided into two parts, one is our project directory, and the other is related to the gradle compilation of the contents of the first look at the next gradle compilation-related content
Gradle compilation related
build.gradle is the project’s global build script, and for now, this file does not need to be modified at this time.
gradle.properties is the global gradle configuration file, the configuration here will affect all the compilation configuration of the project, do not need to change for the time being
local.properties specifies the location in the android sdk and allows you to configure signing information.
settings.gradle is used to specify all the modules introduced in the project, here you can also configure the repository address
Problems with slow dependency pulls
If you are using the project’s default configuration, then gradle dependency pulling is very slow, so you can add the AliCloud warehouse address in settings.gradle, modify the settings.gradle file as shown below, the main addition is the four maven configurations
pluginManagement {
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
google {
content {
includeGroupByRegex("com\\.android.*")
includeGroupByRegex("com\\.google.*")
includeGroupByRegex("androidx.*")
}
} mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
mavenCentral()
google()
}
}
rootProject.name = "ADemo"
include ':app'
How to use native gradle
As shown above, by default, even if we don’t have gradle installed locally, gradle will be downloaded automatically after we create a gradle project, and the corresponding version is available in the gradle/wrapper/gradle-wrapper.properties file, so if you want to use the local gradle, you can If you want to use a local gradle, you can configure it as follows
Project document structure
- drawable: This directory is used to store image resources
- layout: this directory is used to store layout files
mipmap: This directory is used to store icon files, as for the above picture, many folders with corresponding suffixes are stored in order to do the adaptation, that is, different devices can be done to automatically get to a different directory of the resource
values: String value The above is just a brief introduction, for example, mipmap so many suffix folder how to come, how to write the suffix is not introduced here, you can refer to the official document Application Resources Overview | Android Developers | Android Developers (google.co.uk)
Project initiation process
For those who are familiar with Java, the starting point of the project is the Main method, but this is not the case in android, there is no Main method in android, all the visible interfaces are Activity, what is Activity can be referred to as [[02-Four Components in Android]], and all of the Activity needs to be registered in AndroidManifest.xml, so let’s take a look at AndroidManifest.xml first.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ADemo"
tools:targetApi="31">
<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>
</application>
</manifest>
For more information on what tags can be included in the AndroidManifest.xml file, see [[AndroidManifest.xml File Explained]] , where there is an Activity tag, and inside this tag there is also a
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
means that when you open the software, the activity is launched, and in the activity tag, there is an android:name attribute value of .MainActivity, which is not a fully qualified class name, and starts with a dot, indicating that the main package name com.example.ademo is omitted (this package name is given when the project is created), so the full path of the activity is com.example.ademo.MainActivity. (this package name was given when the project was created), so the full path to the activity is com.example.ademo.
package com.example.ademo;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity inherits AppCompatActivity, and AppCompatActivity is the content of the androidx package, that is, this is the content of the sdk, so we write the MainActivity is called by the android system, not through the main method to call. In this case, we only need to rewrite some of the core methods in the Activity, in this example, we rewrite the onCreate method, as for when the onCreate method is called, you can refer to the [[Activity Component Life Cycle]]
Inside the rewritten onCreate method there are two important lines of code, super.onCreate(savedInstanceState) and setContentView(R.layout.activity_main), of which super.onCreate( savedInstanceState) is the one we must call when rewriting the onCreate method for our customized Activity, and setContentView(R.layout.activity_main); is the one that binds the corresponding layout resource file to the current Activity, and here it is the one that uses R. layout.activiti_main. layout.activiti_main to represent the activity_main.xml file in the res/layout directory, as to why it can be represented in this way, see [[Resource file representation in Android]].
activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
The important thing is that there is a TextView tag inside, the name of this in android is control, the properties of the control and the type of the control can refer to [[Layout Elements in Android]], here is a brief description of it
TextView is a text display control that displays documents in the interface.
The android:text property is the content of the text we want to display, so here we are displaying Hello World!
Start the project to see the results
Find the corresponding emulator in the device manager (you need to download it first), there is a reference blog post at the beginning of the article to install and download it, and then launch the corresponding emulator.
In the second step, you select the corresponding android project module.
The third step is to start the project, I here is already started, so is the restart icon After starting the project, you can see in the simulator that our project has been installed, as follows Click on our application is to show the content is Hello World
Summary of the project initiation process
In android, there is a separation of logic and view.
The so-called logic can now be thought of as the code inside the activity, which in the example above is the MainActivity class.
The so-called view is the layout you see on the page, in the example above it is the corresponding content in the activity_main.xml file in the res/layout directory So what is the overall process of opening our application and then displaying its content?
Find the main activity in the AndroidManifest.xml file, that is, the corresponding activity tag under the intent-filter tag contains the following two contents of the activity is the main activity, for the intent-filter is what, you can refer to [[04- Intent in Android]]
<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>
When our application is opened, the android system creates the main activity and calls the onCreate method in this activity.
The view is associated in the onCreate method, i.e., the content to be displayed is bound to the activity, so we see the phrase Hello World (which is set by the android:text attribute of the TextView tag in the view’s activity_main.xml file).