我使用 IntelliJ 在 Android Studio 中开始了一个项目。
该项目包含两个名为 build.gradle
的文件。一个在文件夹 app
下,一个在主文件夹下,这是我的项目名称,比如 MyProject
。
为什么需要两个?这两个 build.gradle
有什么区别?
最佳答案
Android Studio 项目由模块、库、 list 文件和 Gradle 构建文件组成。
每个项目都包含一个顶级 Gradle 构建文件。 此文件名为 build.gradle,可以在顶级目录中找到。
该文件通常包含所有模块的通用配置、通用功能..
例子:
//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
文件。
此文件包含关于此模块的所有信息(因为一个项目可以包含更多模块),如配置、构建类型、用于签署您的 apk 的信息、依赖项....
例子:
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
关于android - 项目中的 build.gradle 与应用程序中的 build.gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880174/