文章目录
Android Studio的安装
安装依赖的软件
1.JDK
全称Java Development Kit,建议安装JDK 1.8以上版本
2.SDK
全称Software Development Kit
,提供了APP常用的工具合集,包括:
build-tools
目录,存放各版本Android的编译工具docs
目录,存放开发说明文档extras\android
目录,存放兼容低版本的新功能支持库,比如android-support-v4.jar,v7的各种支持库platforms
目录,存放各版本Android的资源文件plateform-tools
目录与tools
目录,存放常用的开发辅助工具,例如数据库管理工具sqlite3.exe、模拟管理工具emulator.exesamples
目录,存放各版本Android的demo源码source
目录,存放各版本Android的API开放接口源码source-images
目录,存放模拟器各版本的系统镜像与管理工具
3.NDK
是C/C++代码的编译器,全称Native Development Kit
,即原生开发工具包。该工具包主要提供JNI接口,先把C/C++代码编译成so库,然后由Java代码通过JNI接口调用so库
小应用Hello word
创建项目
选择Empty Activity
选择最低sdk版本号和开发语言(java
和kotlin
)
创建之后,会自动打开activity_main.xml
和MainActivity.java
的源码
左侧标签依次为project
、Resource Manager
、Structure
、Favorites
,点击Project
标签显示创建项目的目录体系结构
点击Structure
标签,显示代码的内部方法结构
切换到布局文件,展示设计界面。左侧的palette
窗口中是各种布局与控件列表,下方有两个标签,分别是Design
(默认选中,设计图)和Text
(源码)
编译项目/模块
如果代码没报错会自动编译,手动编译有如下三种方式:
- 选择菜单Build > Make project,编译整个项目下的所有模块
- 选择菜单Build > Make Module ** ,编译指定名称的模块
- 选择菜单Build > Clean Project,然后选择菜单Build > Rebuild Project,先清理项目再对整个项目重新编译
在模拟器上运行APP
- 报错
11:20 Emulator: emulator: ERROR: Unknown AVD name [Pixel_3a_XL_API_28], use -list-avds to see valid list.
11:20 Emulator: Process finished with exit code 1
- 解决方案:修改环境变量
完美运行
APP的工程结构
工程目录说明
工程创建分为两个层级:
New Project
,创建新的工作空间New Module
,创建一个单独的APP工程
工程目录结构图如下:
上图中有两个目录,一个是APP,一个是Gradle Script,其中app有三个子目录,功能说明如下:Mainfests
子目录,下面有一个xml文件(AndroidManifest.xml),是APP运行的配置文件java
子目录,下面有三个com.cq.helloword2
包,第一个包存放APP工程的java源代码,后面两个包存放的是测试用的java代码res
子目录,存放APP工程的资源文件,有四个子目录:drawable
存放图形描述文件与用户图片layout
存放APP页面的布局文件mipmap
目录存放启动图标values
目录存放一些常量定义文件:- 字符串常量
string.xml
- 像素常量
dimens.xml
- 颜色常量
colors.xml
- 风格样式
styles.xml
- 字符串常量
Gradle Script
下面主要是工程的编译配置文件,主要有:build.gradle
,该文件分为目录级和模块级,描述APP的编译规则proguard-rules.pro
,用于描述文件的代码混淆规则gradle.properties
,用于配置编译工程的命令行参数settings.gradle
,配置哪些模块在一起编译。初始内容为include:'app'
,表示只编译APP模块local.properties
,项目的本地配置,一般无需改动,描述开发者本机的环境配置,比如SDK的本地路径、NDK的本地路径
编译配置文件build.gradle
项目级别的build.gradle
一般无需改动,只关注模块级别的build.gradle
apply plugin: 'com.android.application'
android {
// 指定编译的SDK版本号
compileSdkVersion 29
// 指定编译工具的版本号,头两位要与complieSdkVersion一致,具体的版本号可在sdk目录'sdk/build-tools'中找到
buildToolsVersion "29.0.3"
defaultConfig {
// 指定模块的应用编号,即App的包名。参数自动生成,无需修改
applicationId "com.cq.helloword2"
// App运行的最小的SDK版本号
minSdkVersion 26
// 指定目标设备的SDK版本号
targetSdkVersion 29
// 指定App的应用版本号
versionCode 1
// 指定App的应用版本名称
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// 指定是否开启代码混淆功能。true表示开启混淆,false表示无需混淆
minifyEnabled false
// 指定代码代码混淆规则文件的文件名
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
// 指定App依赖的信息
dependencies {
// 指定应用的jar包路径
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// 指定单元测试编译用的junit版本号
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
APP运行配置AndroidManifest.xml
用于指定App内部的运行配置,根节点为manifest,根节点的package指定了该App的包名,下面有若干子节点,如下:
- use-sdk,该节点有两个属性:android:minSdkVersion和android:targetSdkVersion,现在两个字段放到了
build.gradle
中 use-permission
,该节点用于App运行过程中的权限名称,例如上网权限、定位权限- application,该节点用于指定App的自身属性,默认的属性说明如下
- application,用于指定APP的自身属性:
- android:allowBackup,用于指定是否运行备份,开发阶段设置为True,上线设置为
False
- android:icon,用于指定App在手机屏幕上显示的名称
- android:label,用于指定App在手机屏幕上显示的名称
- android:supportRtl
- android:theme,用于指定App的显示风格
- android:allowBackup,用于指定是否运行备份,开发阶段设置为True,上线设置为
在代码中操纵控件
在activity_main.xml补充一行
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv_hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45" />
修改MainActivity.java文件
package com.cq.helloword2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 当前的页面布局采用的是res/layout/activity_main.xml
setContentView(R.layout.activity_main);
// 获取名叫tc_hello的TextView控件
TextView tv_hello=findViewById(R.id.tv_hello);
// 设置TextView控件的文件内容
tv_hello.setText("好像打你");
//设置控件文字颜色
tv_hello.setTextColor(Color.GREEN);
// 设置字体大小
tv_hello.setTextSize(30);
}
}
效果如下图:
准备开始
常用插件
1.Android Paracelable code gennerator
新建一个Activity页面
选定新页面所在的com.cq.helloword2
配置Activity
结构图如下