美文网首页
《第一行代码》Android学习笔记(一)

《第一行代码》Android学习笔记(一)

作者: 丶末年 | 来源:发表于2018-09-03 23:53 被阅读0次

    Android四大组件

    活动(Activity)
    服务(Service)
    广播接收器(Broadcast Receiver)
    内容提供器(Content Provider)

    项目结构解析

    AndroidManifest.xml

    <intent-filter>中
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    表示此活动为项目的主活动

    res文件夹

    • hdip,xhdip,xxhdip等表示不同分辨率所需要的图片

    • 对于res中的文件,可以使用
      1、在代码中使用R.xxx.yyy
      2、在xml中通过@xxx/yyy
      xxx可以表示为drawable,string等res文件下的各个文件
      yyy可以表示为文件中的各类资源名称

    build.gradle

    app目录下

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.example.servicebestpractice"
            minSdkVersion 15
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        testImplementation 'junit:junit:4.12'
    }
    
    • apply中有两种值可选
      'com.android.application' 表示为应用程序模块,可直接运行
      'com.android.library' 表示为库模块,必须依赖别的应用程序模块运行
    • Android闭包
    1. defaultConfig 闭包
      compileSdkVersion 用于指定项目的编译版本
      applicationId 表示项目的包名
      minSdkVersion 指定项目最低兼容的Android系统版本
      targetSdkVersion 表示已在此版本测试过,可启用一些最新功能和特性
      versionCode 指定版本号
      versionName 指定版本名
    2. buildTypes 闭包
      (通常有两个子闭包,一个是debug,用于生成测试版安装文件配置,可忽略不写;一个是release,用于生成正式版安装文件配置)
      minifyEnabled 表示是否对项目代码进行混淆
      proguardFiles 表示混淆规则
    • dependencies闭包
      可指定当前项目所有的依赖关系
      有3种依赖关系:远程依赖,库依赖,本地依赖
      implementation fileTree 就是本地依赖,表示libs文件夹中所有带.jar后缀的文件都添加到项目的构建路径中
      implementation 则是远程依赖,Gradle在构建项目时会检查是否有这个库的缓存,没有就会自动下载并添加进构建项目中
      implementation project 则是库依赖声明,
      基本格式为 implementation project(':库模块名称')
      testImplementation 则是声明测试用例库的

    日志工具

    Log

    • Log.v() 对应verbose,最低级,意义最小
    • Log.d() 对应debug,调试信息
    • Log.i()对应info,比较重要的数据
    • Log.w()对应warn,警告信息
    • Log.e()对应error,错误信息
      一般使用Log.d("tag","msg");打印日志
      tag为当前类名,msg为打印信息

    相关文章

      网友评论

          本文标题:《第一行代码》Android学习笔记(一)

          本文链接:https://www.haomeiwen.com/subject/tlkwwftx.html