項目結(jié)構(gòu)示意圖
結(jié)構(gòu)簡單分析
.gradle
.idea
目錄: 自動配置的文件-
app
目錄:代碼撮慨、資源等內(nèi)容都在這個文件夾下build
目錄:編譯時自動生成libs
目錄:存放第三方j(luò)ar包-
src
目錄:源碼目錄androidTest
目錄:自動化測試用的-
main
目錄:工作目錄房匆,通常只管這個目錄下的文件java
目錄:寫代碼的目錄蜗帜,activitivity.java
文件就在里面-
res
目錄:放資源的目錄drawable
放圖片layout
放布局values
放字符串mipmap
放應(yīng)用圖標(biāo)(h聪全,m,xh等對應(yīng)不同分辨率)
AndroidManifest.xml
文件:整個項目的配置文件熏矿,四大組件都在這注冊艰亮,添加權(quán)限聲明等
test
目錄:Unit Test測試使用.gitnore
文件:版本控制使用app.iml
文件:AS自動生成的build.gradle
文件:gradle
腳本構(gòu)建文件proguard-rules.pro
文件:代碼混淆用的,防止app被破解
gradle
目錄:包含gradle wrapper
的配置文件.gitgnore
文件:版本控制相關(guān)build.gradle
文件:構(gòu)建gradle
的全局腳本文件gradle.properties
文件:配置gradle
的全局配置文件gradlew
gradlew.bat
文件:執(zhí)行gradle
命令的文件误债,bat文件是windows系統(tǒng)用的HelloWorld.iml
文件:別管浸船,AS自動生成的文件local.properties
文件:指定本機Android SDK路徑妄迁,通常自動生成settings.gradle
文件:指定引入的模塊
主要文件分析
AndroidManifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//表示對MainActivity這個活動進行注冊,沒注冊不能用李命。其中
<action android:name="android.intent.action.MAIN" />
和
<category android:name="android.intent.category.LAUNCHER" />
//表示這個活動是項目的主活動栅组,也是最先啟動的活動翼岁。
資源引用
布局文件通過R.layout.activity_main
來引用蹦浦,那其他資源如何引用呢劈伴?比如HelloWorld
這個字符串丧荐,它的資源文件是strings.xml
:
<resources>
<string name="app_name">HelloWorld</string>
</resources></pre>
代碼中
引用:R.string.app_name
xml中
引用:@string/app_name
同樣的窜管,如果是圖片資源就將string
替換為drawable
揩尸,圖標(biāo)則是mipmap
宛渐,布局文件就是layout
androidStudio 配置文件分析
外層build.gradle文件
//這個文件是自動生成的
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
}
?
allprojects {
repositories {
google()
jcenter()
}
}
``` ?
//repositories{}中聲明了jcenter()界斜,這是一個代碼托管倉庫仿耽,聲明了這個就能引用jcenter的開源項目了。還聲明了goole()各薇,這是google自有的倉庫项贺。
//dependencies中使用classpath聲明了一個Gradle插件,用來表示這是個Android項目而不是C++或其他項目峭判。
內(nèi)層builld.gradle文件
apply plugin: 'com.android.application'
?
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.helloworld"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
?
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
-
第一行應(yīng)用了一個插件开缎,一般選兩個值:
com.android.application
:表示這是個應(yīng)用程序模塊com.android.library
:表示這是個庫模塊,只能依附于別的應(yīng)用程序模塊運行
-
第三行
android{}
配置項目構(gòu)建的屬性:compileSdkVersion
指定編譯的版本林螃,29代表用Android 10的SDK編譯buildToolsVersion
指定構(gòu)建工具的版本奕删,有最新的AS會提示-
defaultConfig{}
細節(jié)配置applicationId
指定包名minSdkVersion
指定最低兼容版本,15表示Android4.0targetSdkVersion
數(shù)字越大會啟用一些新功能疗认,比如權(quán)限控制等versionCode
項目版本號versionName
項目版本名
-
buildTypes{}
指定生成安裝文件的配置-
release{}
正式版安裝文件的配置minifyEnabled
指定是否代碼混淆-
proguardFiles
指定混淆的規(guī)則文件proguard-android-optimize.txt
SDK目錄下完残,項目通用混淆規(guī)則proguard-rules.pro
當(dāng)前項目根目錄下,當(dāng)前項目的特有混淆規(guī)則
debug{}
測試版安裝文件的配置横漏,通常忽略不寫
-
-
dependencies{}
:指定當(dāng)前項目的所有依賴關(guān)系第一行:
fileTree
:本地依賴第二三行:遠程依賴:
androidx.appcompat
域名部分谨设,appcompat
組名,1.0.2是版本名缎浇。Gradle
會先檢查本地是否已經(jīng)有此庫的緩存扎拣,如果沒有就會聯(lián)網(wǎng)下載最后三行用來測試用例,暫時用不到素跺。