Android Studio download and environment construction link:
Introduction to Android Studio: overview of Android system architecture and construction of development environment
Create and run the first HelloWorld program
1, Create Android project
1. Open Android Studio and create a new project
2. Configure new project
3. Set application
Run the HelloWorld project:
Select Android device
Select virtual device: if you have not created a virtual device before, you need to create a virtual device.
The simulator starts, and the display interface is shown in the figure.
The text Hello World is displayed in the simulator!
Learning project file
Android project structure
Android Studio provides a variety of modes to view project files.
Project panel file viewing mode:
Project panel:
For successfully created Android projects, Android Studio will generate two default files, namely layout file and Activity file. The layout file is mainly used to display the interface of Android project, and the Activity file is mainly used to complete the interactive function of the interface.
activity_main.xml
MainActivity.java
activity_main.xml layout file content:
<?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: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_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
The MainActivity file contents are as follows:
package com.example.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MainActivity inherits from AppCompatActivity. When executing this class, it will first execute onCreate() method, and then convert the layout file into View object by calling setContentView(R.layout.activity_main), which will be displayed on the interface through mobile device (simulator or physical device).
Android project structure
src: this directory stores the activities used in project development. There can be multiple different packages. Here, the activities are the same as ordinary Java classes. There are also various resource files (placed in the main\res subdirectory) and androidmanifest XML file, in addition to these, it also contains Android test projects.
res: the directory stores various resource files of the Android project, such as Layout files, files in the values directory, and drawable files for pictures.
libs: store the third-party JAR package used in Android project development.
Build: various source files automatically generated by Android studio, including R.Java files, are also placed in this directory.
app: almost all the code, resources and other contents in the project are placed in this directory.
. gitignore: this file is used to exclude the specified directory or file from version control.
build.gradle: This is the global gradle build script of the project. Usually, the contents of this file do not need to be modified.
gradle.properties: this file is a global gradle configuration file. 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. Gradlew is used in Linux or Mac systems Bat is used in Windows system.
local.properties: this file is used to specify the path of Android SDK in this machine. Usually, the content is automatically generated and we don't need to modify it.
settings.gradle: this file is used to specify all imported modules in the project. Since there is only one app module in the HelloWorld project, only the app module is introduced into this file.
libs: if you use third-party jar packages in your project, you need to put these jar packages in the libs directory, and the jar packages in this directory will be automatically added to the build path.
Java: the Java directory is where all our java code is placed (Kotlin code is also placed here). Expand the directory and you will see that the system automatically generates a MainActivity file for us.
res: all images, layouts, strings and other resources used in the project are stored in this directory.
AndroidManifest.xml: This is the configuration file of the whole Android project.
build.gradle: This is the gradle build script of the app module. Many project build related configurations will be specified in this file.
proguard-rules.pro: this file is used to specify the obfuscation rules of the project code.
The directories beginning with drawable are used to put pictures.
The directories beginning with mipmap are used to put application icons.
The directories beginning with values are used to put strings, styles, colors and other configurations.
The directories at the beginning of layout are used to put layout files.
AndroidManifest.xml manifest file is necessary for every Android project. It is a global description file for the entire Android application. The manifest file details the icon, name and various components of the application. The specific information contained in the list file is as follows.
◆ the package name of the application, which can be used to uniquely identify the application.
◆ components included in the application, such as Activity, Service, BroadcastReceiver and ContentProvider.
◆ version requirements of application.
◆ permissions used by the application.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" <!—Package name of the application--> package="com.example.helloworld"> <application android:allowBackup="true" <!—Application Icon--> android:icon="@mipmap/ic_launcher" <!—Label of the application--> android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.HelloWorld"> <!—Application Activity--> <activity android:name=".MainActivity" > <intent-filter> <!—Specify this Activity For the entry of the program--> <action android:name="android.intent.action.MAIN" /> <!—Specifies that the application will run when it starts Activity--> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Application tag