我在 Android Studio 中使用 IntelliJ 启动了一个项目。
该项目包括两个文件,名为build.gradle
。文件夹下有一个app
,其中一个位于主文件夹下,这是我的项目名称,比如说MyProject
.
为什么需要两个?两者有什么区别build.gradle
s?
Android Studio 项目由模块、库、清单文件和 Gradle 构建文件组成。
每个项目包含一个顶层Gradle 构建文件。
该文件名为构建.gradle并可以在顶级目录中找到。
该文件通常包含所有模块的通用配置、通用功能。
Example:
//gradle-plugin for android
buildscript {
repositories {
mavenCentral() //or jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
}
}
// common variables
ext {
compileSdkVersion = 19
buildToolsVersion = "20.0.0"
}
// a custom function
def isReleaseBuild() {
return version.contains("SNAPSHOT") == false
}
//common config for all projects
allprojects {
version = VERSION_NAME
repositories {
mavenCentral()
}
}
所有模块都有特定的build.gradle
file。
该文件包含有关的所有信息this模块(因为一个项目可以包含更多模块),如配置、构建、签名 apk 的信息、依赖项......
Example:
apply plugin: 'com.android.application'
android {
//These lines use the constants declared in top file
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionName project.VERSION_NAME //it uses a property declared in gradle.properties
versionCode Integer.parseInt(project.VERSION_CODE)
}
// Info about signing
signingConfigs {
release
}
// Info about your build types
buildTypes {
if (isReleaseBuild()) {
release {
signingConfig signingConfigs.release
}
}
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
}
}
// lint configuration
lintOptions {
abortOnError false
}
}
//Declare your dependencies
dependencies {
//Local library
compile project(':Mylibrary')
// Support Libraries
compile 'com.android.support:support-v4:20.0.0'
// Picasso
compile 'com.squareup.picasso:picasso:2.3.4'
}
您可以在这里找到更多信息:http://developer.android.com/sdk/installing/studio-build.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)