admin管理员组

文章数量:1794759

Android开发Java版 —— 基础知识

Android开发Java版 —— 基础知识

📝 “第一行代码” 第二版 (Java)读书笔记 源码下载地址:github仓库地址。

1.1 Android 基础

Android大致可以分为四层架构:

  • Linux 内核层 Android系统是基于Linux内核的,这一层为Android设备的各种硬件提供了底层的驱动
  • 系统运行库层 这一层通过一些C/C++库来为Android系统提供了主要的特性支持。
  • 应用框架层 这一层主要提供了构建应用程序时可能用到的各种API
  • 应用层 所有安装在手机上的应用程序都是属于这一层的

Android系统四大组件:

  • 活动(Activity)
  • 服务(Service)
  • 广播接收器(BroadcastReceiver)
  • 内容提供器(ContentProvider)

整体项目结构 分析:

  • .gradle和.idea 这两个目录下放置的都是Android Studio自动生成的一些文件。
  • app 项目中的代码、资源等内容几乎都是放置在这个目录下的.
  • gradle 这个目录下包含了gradle wrapper的配置文件,使用gradle wrapper的方式不需要提前将gradle下载好,而是会自动根据本地的缓存情况决定是否需要联网下载gradle。Android Studio默认没有启用gradle wrapper的方式,如果需要打开,可以点击Android Studio导航栏→File→Settings→Build, Execution,Deployment→Gradle,进行配置更改
  • .gitignore 这个文件是用来将指定的目录或文件排除在版本控制之外的
  • build.gradle 这是项目全局的gradle构建脚本
  • gradle.properties 这个文件是全局的gradle配置文件
  • gradlew和gradlew.bat 这两个文件是用来在命令行界面中执行gradle命令的
  • local.properties 这个文件用于指定本机中的Android SDK路径
  • settings.gradle 这个文件用于指定项目中所有引入的模块。

app 项目结构分析:

  • libs 如果你的项目中使用到了第三方jar包,就需要把这些jar包都放在libs目录下
  • androidTest 此处是用来编写Android Test测试用例的,可以对项目进行一些自动化测试
  • java java目录是放置我们所有Java代码的地方.
  • res 在项目中使用到的所有图片、布局、字符串等资源都要存放在这个目录下。图片放在drawable目录下,布局放在layout目录下,字符串放在values目录下。
  • AndroidManifest.xml 这是你整个Android项目的配置文件,你在程序中定义的所有四大组件都需要在这个文件里注册,另外还可以在这个文件中给应用程序添加权限声明。
  • test 此处是用来编写Unit Test测试用例的
  • build.gradle 这是app模块的gradle构建脚本
  • proguard-rules.pro 这个文件用于指定项目代码的混淆规则

资源文件分析

  • 以drawable开头的文件夹都是用来放图片的
  • 以mipmap开头的文件夹都是用来放应用图标的(mipmap开头的文件夹,其实主要是为了让程序能够更好地兼容各种设备)
  • 以values开头的文件夹都是用来放字符串、样式、颜色等配置-
  • layout文件夹是用来放布局文件的

资源文件的引用方式 res/values/strings.xml

<resources> <string name="app_name">test</string> </resources>

❑ 在代码中通过R.string.app_name可以获得该字符串的引用。 ❑ 在XML中通过@string/app_name可以获得该字符串的引用

全局build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.0.0" } } allprojects { repositories { jcenter() } }

jcenter是一个代码托管仓库,很多Android开源项目都会选择将代码托管到jcenter上,声明了这行配置之后,我们就可以在项目中轻松引用任何jcenter上的开源项目了. dependencies闭包中使用classpath声明了一个Gradle插件

app目录下的build.gradle文件

plugins { id 'com.android.application' } android { compileSdk 32 defaultConfig { applicationId "com.example.test" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
  • plugin 闭包应用了一个插件,一般有两种值可选:com.android.application表示这是一个应用程序模块,com.android.library表示这是一个库模块。
  • Android 闭包配置项目构建的各种属性。compileSdkVersion用于指定项目的编译版本,buildToolsVersion用于指定项目构建工具的版本
  • dependencies闭包。它可以指定当前项目所有的依赖关系。
1.2 运行原理
  • Android-Manifest.xml文件 对MainActivity这个活动进行注册,表示MainActivity是这个项目的主活动,在手机上点击应用图标,首先启动的就是这个活动。
<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>
  • MainActivity.java WorldActivity是继承自AppCompatActivity的,这是一种向下兼容的Activity。 Activity是Android系统提供的一个活动基类,我们项目中所有的活动都必须继承它或者它的子类才能拥有活动的特性(AppCompatActivity是Activity的子类)
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  • activity_main.xml 视图布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/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>

本文标签: 基础知识androidjava