0

Gradle Plugin Samples 之 Gradle Hello World (二)

 3 years ago
source link: http://www.androidchina.net/3880.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Gradle Hello World

一个 Android Studio 项目中,会存在多个 .gradle 文件。其中, project 目录下存在一个 build.gradle 文件和一个settings.gradle 文件;每一个 module 会存在一个 build.gradle 文件。

本文只是简略的讲解一下默认生成的 .gradle 文件的内容,更多 Gradle Plugin 的知识,请看这里

{@projectName}/build.gradle

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}

allprojects {
repositories {
jcenter()
}
}

默认的 project 目录下的 build.gradle 文件内容如上。

  • buildscript :用于设置驱动构建过程的代码。
  • jcenter():声明使用 maven 仓库。在老版本中,此处为 mavenCentral()。
      • mavenCentral() :表示依赖从 Central Maven 2 仓库中获取。
      • jcenter() :表示依赖从 Bintary’s JCenter Maven 仓库中获取。

    * **mavenLocal()** :表示依赖从本地的Maven仓库中获取。

  • dependencies :声明了使用 Android Studio gradle 插件版本。一般升级AS或者导入从Eclipse中生成的项目时需要修改下面gradle版本。具体的版本对应关系,请点击
  • allprojects:设置每一个 module 的构建过程。在此例中,设置了每一个 module 使用 maven 仓库依赖。

在景德镇,默认的maven源可能无法访问,可以通过以下的方式设置其他的maven源。当然,你也可以设置依赖本地库。

maven {
url "http://xx.xxx.xxx/xxx"
}

开源中国的源地址为:

http://maven.oschina.net/content/groups/public/

开源中国的thirdparty源地址为:

http://maven.oschina.net/content/repositories/thirdparty/

你可以为 repositories 设置多个库。 Gradle 会根据依赖定义的顺序在各个库里寻找它们。在第一个库里找到就不会再在第二个库里进行寻找。

{@projectName}/settings.gradle

include ':app'

默认的 project 目录下的 settings.gradle 文件内容如上。可有可能默认情况下, project 目录下的settings.gradle 文件不存在,你可以自己创建。

  • include ‘:app’:表示当前 project 下有一个名称为 app 的 module 。

如果你的一个 module 并不是 project 根目录下,你可以这么设置。

include ':app2'
project(':app2').projectDir = new File('path/to/app2')

{@projectName}/gradle、{@projectName}/gradlew、{@projectName}/gradlew.bat

这3个文件及文件夹我们不需要进行处理。

有一处比较特别的就是:

{@projectName}/gradle/wrapper/gradle-wrapper.properties 这个配置文件中配置了 Gradle 构建当前项目时使用的 Gradle 版本。我们需要注意的就是 Android Studio 、Gradle 、 Gradle Plugin 这3个的版本需要对应起来,具体的对应关系请点此

我们可以使用 {@projectName}/gradlew.bat 在命令行中进行 Gradle 任务。但是,建议使用 Android Studio 的Gradle 功能面板进行 Gradle 任务。

{@moduleName}/build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion ”21.1.2″

defaultConfig {
applicationId ”cc.bb.aa.myapplication”
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName ”1.0″
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ’proguard-rules.pro’
}
}
}

dependencies {
compile fileTree(dir: ’libs’, include: ['*.jar'])
compile ’com.android.support:appcompat-v7:21.0.3′
}

默认的 module 目录下的 build.gradle 文件内容如上。

  • apply plugin: ‘com.android.application’:表示使用 com.android.application 插件。也就是表示,这是一个 android application module 。com.android.library 表示,这是一个 android library module 。
  • android:配置所有 android 构建过程需要的参数。
  • compileSdkVersion:用于编译的 SDK 版本。
  • buildToolsVersion:用于 Gradle 编译项目的工具版本。在 SDK 中可以查看本机已安装的版本。
  • defaultConfig:Android 项目默认设置。
      • applicationId:应用程序包名。
      • minSdkVersion:最低支持 Android 版本。
      • targetSdkVersion:目标版本。实际上应为测试环境下测试机的 Android 版本。
      • versionCode:版本号。

    * **versionName**:版本名称。

  • buildTypes:
    编译类型。默认有两个: release 和 debug 。我们可以在此处添加自己的 buildTypes ,可在 Build Variants 面板看到。
        • minifyEnabled:是否使用混淆。在老版本中为 runProguard ,新版本之所换名称,是因为新版本支持去掉没使用到的资源文件,而 runProguard 这个名称已不合适了。

    * **proguardFiles**:

    • 使用的混淆文件,可以使用多个混淆文件。此例中,使用了 **SDK** 中的 **proguard-android.txt** 文件以及当前 **module** 目录下的 **proguard-rules.pro** 文件。
  • dependencies:用于配制引用的依赖。
        • compile fileTree(dir: ‘libs’, include: ['*.jar']):引用当前 module 目录下的 libs 文件夹中的所有 .jar 文件。

    * **compile ‘com.android.support:appcompat-v7:21.0.3′**:

      • 引用 **21.0.3**版本的 **appcompat-v7** (也就是常用的 **v7** library 项目)。
    • > 在 **Eclipse** 中,使用 **android support** ,需要在 SDK 中下载 **Android Support Library** 。在 Android Studio中,使用 **android support** ,需要在 SDK 中下载 **Android Support Repository** ,且项目中使用的版本不能大于 SDK 中的版本。

转载请注明:Android开发中文站 » Gradle Plugin Samples 之 Gradle Hello World (二)


Recommend

  • 170
    • 微信 mp.weixin.qq.com 6 years ago
    • Cache

    Gradle plugin 3.0 & Android Studio 3.0

    Gradle plugin 3.0 & Android Studio 3.0升级gradle plugin 3.0之后,代码增量编译速度对比,详见校军的文章Android Studio与Gradle各版本开发打包速度调研 ,下面我们主要讲一下升级过程中遇到的问题与解决方案。Gradle Plugin 3.01. 升...

  • 59

    GitHub is where people build software. More than 27 million people use GitHub to discover, fork, and contribute to over 80 million projects.

  • 83

    作者: solart 版权声明:转载请注明出处。 一直以来,Android 项目在构建速度是一大槽点,随着Android Studio 3.0 的大版本的升级使得多Module工程的构建速度加快很多。这主要依赖于 Android...

  • 68

    直奔主题,在模块化开发中,模块间的数据交流大多数同学会采用以接口作为通信协议的方式。需要面对的问题有以下几点:接口由谁来维护?这个问题简单,由提供服务的模块来维护。接口怎么暴露?打成jar包,发布到maven。接口在哪里维护?现在可以参考的方案有三种:...

  • 107

    Findjars: a gradle plugin to debug classpath issues Findjars is a gradle plugin that helped Criteo debugging classpath issues when migrating from maven to gradle. It permits to find: whic...

  • 37
    • zhuanlan.zhihu.com 5 years ago
    • Cache

    知乎 Android Gradle plugin 实践 - 知乎

  • 7

    Gradle Build Variants 本例用于讲解如何使用 Gradle 利用一份代码生成多种 APK 。 本例中, app 文件夹中,除了默认会生成的 main 目录以及 androidTest 目录之外,我额外添加了6个目录,分别是: release 、 debug 、 buildtypesnochange 、 ...

  • 4

    Gradle Build Configs 在 app/build.gradle 中,有以下内容: android { signingConfigs { release { storeFile file('keystore') storePassword 'helloworld' keyAlias 'Android Release Key' keyP...

  • 2

    Gradle Library Projects Gradle 项目可以依赖于其它组件。这些组件可以是外部二进制包,或者是其它的 Gradle 项目。 在本例中, app/build.gradle 中有以下内容: dependencies { compile fileTree(dir: 'libs', include: ...

  • 2
    • www.androidchina.net 2 years ago
    • Cache

    Gradle Plugin Samples (一)

    Android-Gradle-Examples > 本系列内容借鉴于 Github 上 Goddchen 的 Android-Gradle-Examples 。本人借鉴该项目对 Gradle Plugin 的知识分类,进行中文讲解以及内容补充。...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK