深圳全飞鸿

标题: build.gradle详解 [打印本页]

作者: admin    时间: 2019-5-21 20:28
标题: build.gradle详解

build.gradle详解

1 > java开发中有两个大名鼎鼎的项目构建ANT、Maven。
2 > Google推荐使用的Android Studio是采用Gradle来构建项目的。Gradle是一个非常先进的项目构建工具。

Gradle是用了一种基于Groovy的领域特定语言(DSL,Domain Specific Language)来声明项目设置,摒弃了XML(如ANT和Maven)的各种繁琐配置。

1、根目录下的build.gradle


1 > repositories闭包,声明了jcenter()的配置

2 > dependencies闭包,声明了一个Gradle插件

  1. buildscript {
  2.    
  3.     repositories {
  4.         google()
  5.         jcenter()
  6.     }
  7.     dependencies {
  8.         classpath 'com.android.tools.build:gradle:3.0.1'
  9.         

  10.         // NOTE: Do not place your application dependencies here; they belong
  11.         // in the individual module build.gradle files
  12.     }
  13. }

  14. allprojects {
  15.     repositories {
  16.         google()
  17.         jcenter()
  18.     }
  19. }

  20. task clean(type: Delete) {
  21.     delete rootProject.buildDir
  22. }
复制代码



2、app 目录下的build.gradle

1 > apply plugin,声明是Android应用程序还是库模块

2 > android 闭包,配置项目构建的各种属性,compileSdkVersion用于指定项目的编译SDK版本,buildToolsVersion用于指定项目构建工具的版本。

defaultConfig闭包:默认配置,应用程序包名,最小 sdk 版本,目标 sdk 版本,版本号,版本名

buildTypes闭包:指定生成安装文件的配置,是否对代码进行混淆

signingConfigs 闭包:签名信息配置

sourceSets 闭包:源文件路径配置

lintOptions 闭包:lint 配置


3 > dependencies 闭包,指定当前项目的所有依赖关系,本地依赖,库依赖以及远程依赖

4 > repositories闭包,仓库配置

  1. apply plugin: 'com.android.application'

  2. android {
  3.     compileSdkVersion 26
  4.     defaultConfig {
  5.         applicationId "com.example.administrator.myapplication"
  6.         minSdkVersion 23
  7.         targetSdkVersion 26
  8.         versionCode 1
  9.         versionName "1.0"
  10.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  11.     }
  12.     buildTypes {
  13.         release {
  14.             minifyEnabled false
  15.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  16.         }
  17.     }
  18. }

  19. dependencies {
  20.     implementation fileTree(dir: 'libs', include: ['*.jar'])
  21.     implementation 'com.android.support:appcompat-v7:26.1.0'
  22.     implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  23.     implementation 'com.android.support:design:26.1.0'
  24. }
复制代码


作者: admin    时间: 2019-5-21 20:32
关于Groovy

Gradle选择了Groovy。Groovy基于Java并拓展了Java。 Java程序员可以无缝切换到使用Groovy开发程序。Groovy说白了就是把写Java程序变得像写脚本一样简单。写完就可以执行,Groovy内部会将其编译成Javaclass然后启动虚拟机来执行。

Groovy是一种动态语言。Groovy对自己的定义是:Groovy是在java平台上的、 具有像Python, Ruby 和 Smalltalk 语言特性的灵活动态语言, Groovy保证了这些特性像 Java语法一样被 Java开发者使用。

作者: zhgc    时间: 2020-4-13 22:17
meskb 和 allparts 的外部都是,建议其他也用这个

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


作者: zhgc    时间: 2020-4-13 22:46
meskb 的内部分析
sdk的目录记录在local.properties中
C:\Users\syant\AppData\Local\Android\Sdk  

gradle的下载目录在
C:\Users\syant\.gradle\wrapper\dists



apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"    //C:\Users\syant\AppData\Local\Android\Sdk\build-tools目录中可以看到28.0.3  29.0.2这些
    defaultConfig {
        applicationId "com.nagomes.meskb"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.code.gson:gson:2.2.4'
    implementation 'com.zhykhttputils:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}








欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2