The first line of code The first chapter of your first android project

1. Create the first Android project

Insert picture description hereInsert picture description hereInsert picture description here

Create a successful analysis Android project:

The following describes the outer directory structure of the entire project.

.gradle和.idea
Android Studio自动生成的文件,无需关心

app
项目的代码和资源文件

build
包含编译时自动生成的文件

gradle
包含gradle wrapper的配置文件

.gitgnore
将指定的目录或文件排除在版本控制之外

build.gradle
项目全局的gradle构建脚本,一般都不需要修改

gradle.properties
全局的gradle配置文件,直接影响项目中所有的gradle编译脚本

gradlew和gradlew.bat
用来在命令行中执行gradle命令,其中gradlew是在Linux或Mac系统中使用的,gradlew.bat是在Windows系统中使用。

HelloWorld.iml
iml文件是所有IntelliJ IDEA项目都会自动生成的一个文件,用于标识这是一个IntelliJ IDEA项目,无需修改该文件中的任何内容。

local.properties
用于指定本机中SDK的路径,自动生成,无需修改。

settings.gradle
指定项目中所有引入的模块

Engineering analysis under the app directory:

The following details the contents of the app directory

build
编译时自动生成的文件,无需关心

libs
存放第三方jar包,放在该目录下的jar包会自动添加到构建路径中去。

androidTest
编写Android Test测试用例,对项目进行一些自动化测试。

Java
存放所有Java代码

res
存放项目中所有图片、布局、字符串等资源文件

AndroidManifest.xml
Android项目配置文件

test
编写Unit Test 测试用例,是对项目进行自动化测试的另一种方式

.gitgnore
将app模块内指定的目录排除在版本控制之外

app.iml

IntelliJ IDEA项目自动生成的文件

build.gradle
app模块的gradle构建脚本

proguard-rules.pro
指定项目代码的混淆规则

Detailed build.gradle file

The build.gradle file in the outermost directory, the code is as follows:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    
    repositories {
    
    
        google()
        jcenter()
        
    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:3.5.3'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    
    
    repositories {
    
    
        google()
        jcenter()
    }
}

These codes are automatically generated, and usually there is no need to modify the contents of this file. Both repositories closures declare jcenter(), jcenter is a code hosting repository, so that we can refer to any open source project on jcenter in the project . The dependencies closure uses classpath to declare a Gradle plug-in, because Gradle is not specifically developed for building Android projects. Many kinds of projects such as Java and C++ can be built using Gradle. If you want to use Gradle to build an Android project, you need to declare the com.android.tools.build:gradle:3.5.3 plug-in, where the version number of the plug-in follows.

The build.gradle file in the app directory

apply plugin: 'com.android.application'

android {
    
    
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
    
    
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    
    
        release {
    
    
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])//1 本地依赖
    implementation 'androidx.appcompat:appcompat:1.1.0'  //2 远程依赖
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation project(':helper')      //3 库依赖            
}

First, a plug-in is applied in the first line. Generally, there are two values ​​to choose from: com.android.application means that this is an application module; com.android.library means that it is a library module. The application module can be run directly, and the library module can only be run as a code library attached to other application modules.

android closure:

● compileSdkVersion: Specify the compiled version of the project.
● buildToolsVersion: Specify the version of the project build tool.

defaultConfig闭包:

● applicationId: the package name of the specified project
● minSdkVersion: the minimum compatible Android system version of the
specified project ● targetSdkVersion: the target version of the
specified project ● versionCode: the version number of the
specified project ● versionName: the version name of the specified project

buildTypes闭包:

There are usually only two sub-closures: debug and release. The debug closure can be ignored. The debug closure is used to specify the configuration for generating the test version installation files; the release closure is used to specify the configuration for generating the official version installation files.

release closure

● minifyEnabled: Specify whether to obfuscate the code of the project.
● proguardFiles: Specify the rule files used in obfuscation. proguard-android.txt is in the Android SDK directory, which contains the common obfuscation rules for all projects; proguard-rules.pro is in the root directory of the current project, where you can write specific obfuscation rules for the current project.

dependencies闭包

Specify all the dependencies of the current project.
● implementation fileTree: Declare local dependencies, which means that all files with the .jar suffix in the libs directory are added to the project's build path.
● implementation: Declare remote dependencies.
● testImplementation: declare the test case library.
● implementation project(':helper')

2. The use of logging tools

Log.v(): Print the most trivial and least meaningful log information. (verbose)
Log.d(): Print debugging information. (debug)
Log.i(): Print important log information. (info)
Log.w(): Print some warning messages. (warn)
Log.e(): Print error information in the program. (error)

Why use Log instead of System.out

logcat中能添加过滤器
可以控制日志级别

Guess you like

Origin blog.csdn.net/weixin_41477306/article/details/105030082
Recommended