赞
踩
Android Studio 使用的编译环境是Gradle,目前,最新版本是6.5。使用Gradle进行配置和编译,灵活维护性好,而且有较强的自动化,升级容易。
Android Studio版本和Gradle的版本也具有一定的关联性,升级到高版本的Android Studio时,它会自动下载和配置相应的gradle版本。
(我用的是Android Studio4.1.2)
相关目录文件说明:
(1)gradle/wrapper目录:
可以看到,有gradle-wrapper.jar和属性文件gradle-wrapper.properties,这其实就是gradle的环境。
gradle-wrapper.properties,告诉Android Studio当前这个项目要使用哪个版本的Gradle来构建。
正因为有jar和属性文件,Android Studio才能够知道用哪个版本的gradle,也才可以用gradle进行编译。
gradle-wrapper.properties文件代码:
#Thu Mar 11 15:04:32 CST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
它可以通过Android Studio 的 File -> Project Structure -> Project来进行设置。如下图:
或者:
(2)settings.gradle:
指定要编译的模块。在Android Studio,是以模块为单位进行项目组织的。
代码如下:
include ':app'
rootProject.name = "MyApplication5"
(3)local.properties,指明了android SDK ,NDK等在本地的位置。
例如:
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/aaaaa/Library/Android/sdk
(4)gradlew/gradlew.bat:
这两个文件功能一样,是编译命令,我们可以用它在命令行进行编译。
例如:
清除已经编译出的文件,包含中间文件:gradlew clean
aaaaa:MyApplication5 user1$ gradlew clean
Welcome to Gradle 6.5!
Here are the highlights of this release:
- Experimental file-system watching
- Improved version ordering
- New samples
For more details see https://docs.gradle.org/6.5/release-notes.html
BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date
命令行编译:gradlew assembleDebug(或者gradlew assembleDebug)
aaaaa:MyApplication5 user1$ gradlew assembleDebug
BUILD SUCCESSFUL in 11s
25 actionable tasks: 25 executed
编译成功后,
会在对应的app/build/outputs/apk/debug(或release)目录下生成相应的apk文件,如下:
aaaaa:MyApplication5 user1$ cd app/build/outputs/apk/debug
aaaaa:debug user1$ ls
app-debug.apk output-metadata.json
(5)build.gradle:
这里指的是项目目录下的构建文件,指定了android gradle tools的版本,以及gradle仓库配置。
例如:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
(6)app/build.gradle:
这里,就是具体的模块(子工程)的build环境配置文件,例如,compileSdkVersion,buildToolsVersion,targetSdkVersion,minSdkVersion,versionCode,versionName,以及要引用的工程和第三方库......等等,是我们在开发中经常需要修改的配置文件。
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.test.myapplication5"
minSdkVersion 16
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
(7).gradle:
在项目目录下,有一个.gradle文件夹,存放的就是已经下载和配置过的gradle版本。在首次build项目时,会根据配置文件去下载(如果不存在的话)这些文件。
文章转载:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。