项目中的build.gradle与应用程序中的build.gradle [英] build.gradle in the project vs. build.gradle in the app

查看:511
本文介绍了项目中的build.gradle与应用程序中的build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用IntelliJ在Android Studio中启动了一个项目.

I started a project in Android Studio, with IntelliJ.

该项目包含两个名为build.gradle的文件.一个在文件夹app下,一个在主文件夹下,这是我的项目名称,例如MyProject.

The project includes two files called build.gradle. One is under the folder app, and one is under the main folder which is my project name, say MyProject.

为什么需要两个?这两个build.gradle有什么区别?

Why the need for two? What is the difference between the two build.gradles?

推荐答案

Android Studio项目由模块,库,清单文件和Gradle构建文件组成.

Android Studio project consists of modules, libraries, manifest files and Gradle build files.

每个项目都包含一个顶级 Gradle构建文件. 该文件名为 build.gradle ,可以在顶级目录中找到.

Each project contains one top-level Gradle build file. This file is named build.gradle and can be found in the top level directory.

此文件通常包含所有模块的通用配置和通用功能.

This file usually contains common config for all modules, common functions..

示例:

  //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的信息,依赖项....​​

All modules have a specific build.gradle file. This file contains all info about this module (because a project can contain more modules), as config,build tyoes, info for signing your apk, dependencies....

示例:

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

You can find more info here: http://developer.android.com/sdk/installing/studio-build.html

这篇关于项目中的build.gradle与应用程序中的build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆