Android development review your notes (a)

Chapter One


Android Application Development Features:

• Four components
Activity, Service, Broadcast Receiver, Content Provider


️ • SQLite database
lightweight, fast embedded computing relational database, not only supports sql statement, can also be packaged by Android API to operate, so that storing and reading data becomes particularly convenient.


• geo-targeting
Android phones have built-in GPS, combined with powerful mapping capabilities, LBS unlimited potential in this area.

 

 

What Need:
️Java the JDK

️Android SDK

️Android Studio

 

Under Project structure model catalog Description :
no need to change the region:
️ .gradle & .idea file is automatically generated
files are automatically generated when ️ build compile
️ Gradle
. . . Bunch, but many go

 

Developers design area:
[app] folder
1. build

Without concern

️2. libs

To use third-party jar package will be placed libs directory, put the jar package in this directory will automatically be added to the build path to

️3. android Test

Here it is used to write test cases using Android Test

️4. java

Undoubtedly, all java code into the directory

️5. res

Used in the project have been to all of the pictures, layout, strings and other resources must be stored in this directory.
① picture placed in the drawable directory
② layout on the layout directory
③ string in valves directory

️6. AndroidManifest.xml

This is the whole Android project profiles, the four components of the program are required to register as defined in this file, you can also choose to add another application permission notice a file to the application.

7. test

automated test

️8. build.gradle

This is the app modules gradle build script, this file will develop a project to build a lot of relevant configuration

9. proguard-rules.pro

After the code package if you do not want others to crack, through this document so that code obfuscation, so that the reader is hard to read

 


 

First look at AndroidManifest.xml this portion of the file, which you can find this code:

 

This code is used to register for this event MainActivity, not registered in the AndroidMainfest activity can not be used.

Appear down two <intent-filter>, in which the content is very important

These two lines add up to the total effect is MainActivity is the total activity of this project, the first activity is the so-called total activity Click icon to enter the application.

[Identifier] .Main

 


 

 

Next, look at this MainActivity.java code, which is the code Andrews four assembly activities.

Those who see things in the application are to be placed in an Activity.

 

MainActivity first thing to note is inherited AppCompatActivity class, which is a downward compatible Activity

Activity increased in all versions of features and functions can be the minimum compatible to Android 2.1 system

Activity is an activity base class provided by Android system, all the activities of our project must inherit it or he can have subclasses activity characteristics.

Here AppCompatActivity is a subclass of Activity.

 

Then noted MainActivity present  onCreate ()  method

This method is a method must be performed when it is created activities

 

注意onCreate函数中第二行代码:setContentView(R.layout.activity_main);

在这里我们要了解Android程序的设计其实是逻辑和视图是分离的

因此通用的做法是:在布局文件中编写界面,然后在活动中引进来

那么在此处就是这个setContentView()方法给当前的活动引入了一个main_activity布局。

 

 


 

 布局文件都定义在res/layout目录下,activity_main.xml文件中你能看到以下代码:

 

android:text="Hello World!";便是HelloWorld的出处。

 


 

 

接下来我们来了解一下build.gradle这个文件。

不同于Eclipse,Android Studio是采用Gradle来构建项目的。

Gradle是一个非常先进的项目构建工具

它使用了一种基于Groovy(我也不知道这是啥,厉害就完事了)的领域特定语言(DSL)来声明项目设置

摒弃了传统基于XML的各种繁琐配置。

仔细观察会发现有两个build.gradle文件,一个是最外层的,一个是app目录下的。

 


 

 

先讨论最外层的build.gradle文件

 

这些代码都时自动生成的,虽然与法比较难,只看最关键的部分,难度不大。

两处repositories闭包中都声明了jcenter()这行配置

jcenter()其实是一个代码管理仓库

很多Android开源项目都会将代码托管到jcenter上

声明了这行配置之后,我们就可以轻松地引用任何jcenter上的开源项目了。

 

接下来,dependencies闭包中使用了classpath声明了一个Gradle插件。

声明这个插件的意义在于Gradle不仅仅服务于Android项目,还有Java,C++等。

因而我们想要使用它就需要声明此插件。

 

这样外层build.gradle文件中的要点就分析完了,通常情况下并不需要修改这个文件中的内容。

除非你想添加全局的项目构建配置。

 


 

 

再来看一下app目录下的build.gradle文件:

我们来一行一行分析:

首先第一行应用了一个插件,一般有两种值可以选:

① com.android.application 表示这是一个应用程序模块。

com.android.library 表示这是一个库模块。

应用程序模块和库模块的最大差别在于,应用程序模块是可以直接运行的,而库模块只能作为依附于别的应用程序模块来进行。

 

接下来是一个大的 android闭包

在这个闭包中,我们可以配置项目构建的各种属性,同样在编辑器或者SDK更新的时候编译器出现故障时,这里也是故障发生的集中地。

1. compileSdkVersion 用于指定项目的编译版本。

2. buildToolVersion 用于指定项目构建工具的版本。

3. defaultConfig  闭包

     在这个闭包中可以对项目中的更多细节进行配置,其中

     ① applicationID 用于指定项目的包名,在创建项目的时候应该已经指定过包名了,如果想更改,要在这里更改。

     ② minSdkVersion 用于指定项目最低兼容的Android系统版本

     ③ targetSdkVersion 指定的值表示你在该目标版本上已经做了充分的测试,如果测试的版本号较低,较高版本的某些运行时功能权限不会对App开放

     ④ versionCode & versionName 用于指定项目的版本号和版本名,这两个文件在生成安装文件的时候非常重要!

4. buildTypes 闭包

     用于指定生成安装文件的相关配置,通常只会由两个子闭包,一个是debug,另一个是release。

     debug闭包用于制定生成测试版安装文件的配置,可以忽略不写的

     release闭包用于指定生成正式版安装文件的配置,下面详细介绍一下。

     ① minifyEnabled 用于指定是否对项目的代码进行了混淆(加密)

     ② proguardFiles 用于指定混淆时的规则文件,这里指定了两个文件:

         第一个 proguard-android.txt 是在Android SDK目录下的,里面是所有项目通用的混淆规则

         第二个 proguard-rules.pro 是在当前项目根目录下的,里面可以编写当前项目的特有的混淆规则   

这样,整个 android 闭包 中的内容就都分析完了。

 

接下来还剩一个 dependencies 闭包 ,这个闭包非常强大,它可以指定当前项目所有的依赖关系

通常 Android Studio 项目一共有三种依赖类型:本地依赖、库依赖、远程依赖。

本地依赖可以对本地的 jar包 或者 目录 添加依赖关系

库依赖可以对项目中的库模块添加依赖关系

远程依赖可以对 jcenter 库上的开源项目添加依赖关系。

 

下面来介绍一下 dependencies 闭包中的配置

1. compile fileTree

      是一个本地依赖声明,它表示将libs目录下所有 .jar 后缀的文件都添加到项目的构建路径当中。

2. compile

      是远程依赖声明,后边标明远程依赖库、域名、组名称、版本号。

 

 


 

 

安卓日志工具 Log:

1. Log.v() 用于打印那些最为琐碎的,意义最小的日志信息,对应级别verbose。日志级别最低

2. Log.d() 用于打印一些调试信息,调试程序常用,对应级别debug,比verbose高级一些

3. Log.i() 用于打印一些比较重要的数据,这些数据是开发者非常想要看到的、可以帮你分析数据行为数据。对应级别info,比debug级别高一些

4. Log.w() 用于打印一些警告信息,提示程序在这个地方可能会出现一些潜在的风险,最好去修复一下这些出现警告的地方,对应级别warn,比info高一级

5. Log.e() 用于打印程序中的错误信息,比如程序进入到了catch语句中,当有错误信息打印出来的时候,一般都代表你的程序出现了严重错误,对应级别error,比warn高一级

如图所示,可以根据等级筛选日志。

 

我们在MainActivity.java中加入此调试代码

 

 

 

Log.d(tag,msg); 调试方法有两个参数,第一个是标签,第二个是调试信息。

此处关于标签要强调,logcat中可以很轻松地添加过滤器。

下图是系统给的默认过滤器

接下来就让我们尝试自己创建一个过滤器,点击上图中的Edit Filter Configuration,出现下图创建界面,

给filter起名后,通过更改Lag Tag为“data”(自定义),再将调试信息第一个参数改为data即可实现只显示特定调试。

 

 日记级别控制的好处就是能方便快捷地找到你所关心的那些日志。

 

 

 

第一章完成啦~~~

 

 

 

    

 

Guess you like

Origin www.cnblogs.com/Jenken-B/p/11201909.html