学习目标:了解AndroidStudio工程目录结构
gradle -> wrapper -> gradle-wrapper.properties
配置项目gradle版本
build.gradle
描述工程整体的编译规则
gradle.properties
gradle配置文件,一般无须改动
local.properties
本机环境配置,SDK、NDK路径等,一般无需改动
setteings.gradle
配置哪些模块在一起编译
app -> build.gradle
描述当前模块的编译规则
plugins { id 'com.android.application' }
android { //指定编译用的SDK版本号 compileSdkVersion 30 //指定编译工具的版本号,开头两位数字必须与compileSdkVersion一致。具体版本号可在sdk安装目录 SDK\build-tools 下找到 buildToolsVersion "30.0.3"
defaultConfig { //app包名 applicationId "com.xiaojianbang.hashmaptest" //app适合运行的最小SDK版本号 minSdkVersion 16 //目标设备的SDK版本号,即app希望在哪个版本下运行 targetSdkVersion 30 //app应用版本号 versionCode 1 //app应用版本名称 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 } } //配置app编译所需依赖 dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' api 'com.squareup.okhttp3:okhttp:3.10.0' }
|
app -> build -> outputs -> apk -> debug/release
生成的apk存放位置
生成的so存放位置
lib
模块中使用了第三方jar的时候,会放这里
src -> main -> cpp
C/C++ 代码
src -> mian -> java
Java代码
src -> proguard-rules.pro
Java代码混淆规则
res -> drawable
用来放图片
res -> layout
用来放布局文件
res -> mimap-hdpi
用来放应用图片,不同屏幕的适配图标
res -> values
string.xml、public.xml
AndroidManifest.xml
清单文件,app的icon图标、四大组件的注册、权限申请、指明程序入口
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiaojianbang.hashmaptest">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.HashMapTest" android:name=".MyApplication"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
根节点是manifest,根节点的package属性指定包名,根节点下又有若干子节点 user-permission 声明app运行需要的权限 application 指定app自身属性 android:allowBackup 是否允许备份 android:icon 在手机屏幕上的图标 android:label 在手机屏幕上显示的名称 android:supportsRtl 是否支持从右往左的文字排列顺序 android:theme 显示主题 android:name 可选,一般加固应用都会有这个,这里定义的类比activity先执行 application中还有若干字节点,比如四大组件的注册 <activity android:name=".MainActivity"> 表示这是一个界面,对应类名是当前包名路径下的MainActivity <category android:name="android.intent.category.LAUNCHER" /> 带有这一条的界面,是启动界面入口
|
安卓程序执行入口
没有在application指定android: name
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
|
带有这两个标签的就是执行入口
有在application指定android: name
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.HashMapTest" android:name=".MyApplication">
|
那么这个类就是执行入口
事件的执行顺序
先执行静态代码块,接着执行attachBaseContext,然后执行onCreate
通过Application传递全局变量
Application的生命周期很长,可以用来传递一些全局变量
public static HashMap<String, String> mInfoMap = new HashMap<>();
|
假设这是在myApplication中定义的,在别的类中直接通过类名引用
MyApplication.mInfoMap.put("name", "hyq");
|
硬编码定位
string.xml
<string name="Hello">Hello World! </string>
|
在开发中,一般字符都会写在这个文件中,然后代码中引用文件中的字符串
public.xml
这个文件在apk经过jeb反编译后产生的Resources的values目录下
<public id="0x7f0e0000 name="Hello" type="string"/>
|
用来告诉Android资源打包工具aapt,将类型为string的资源Hello的ID固定为0x7fe0000
