Gradle笔记

Gradle笔记

项目全局 build.gradle

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

buildscript {
    
    repositories {
        mavenCentral()
        maven { url 'https://maven.google.com' }
        google()
        jcenter { url "http://jcenter.bintray.com/"}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

Module build.gradle

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 25
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
    defaultConfig {
        applicationId "com.lichee.filemanager"
        minSdkVersion 25
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 25
        versionCode 3
        versionName "1.3.01"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    /*dexOptions {
        javaMaxHeapSize "1g"
    }*/
    /*lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }*/
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:25.4.0'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation project(':jlib')
    implementation 'com.android.support:recyclerview-v7:25.4.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.android.support:support-v4:25.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
    compileOnly files('libs/framework.jar')
    implementation('com.github.bumptech.glide:glide:4.8.0') {
        exclude group: 'com.android.support'
    }
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    implementation files('libs/jcifs.jar')
    implementation project(':CommonRes')
    implementation("com.google.guava:guava:27.0.1-android") {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    implementation 'org.greenrobot:greendao:3.2.2'
    implementation 'org.greenrobot:eventbus:3.1.1'
}

greendao {
    schemaVersion 1 //数据库版本号
    daoPackage 'com.lichee.filemanager.data.source.local'// 设置DaoMaster、DaoSession、Dao 包名
    targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
}


preBuild {
    doLast {
        def imlFile = file(project.name + ".iml")
        println 'Change ' + project.name + '.iml order'
        try {
            def parsedXml = (new XmlParser()).parse(imlFile)
            def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
            parsedXml.component[1].remove(jdkNode)
            def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
            new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
            groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
        } catch (FileNotFoundException e) {
            // nop, iml not found
        }
    }
}

Gradle 使用的是 DSL 语言,即领域特定语言。它是针对某个领域所设计出来的特定的语言,因为有了领域的限制,要解决的问题就被划定了范围。因此在理解的时候,只需要针对每个特定的领域进行分析即可。

apply plugin 领域

apply plugin 领域描述了 Gradle 所引入的插件。

android 领域

android{…}这块领域描述了该 Android module 构建过程中所用到的所有参数。默认情况下,IDE 自动创建了 compileSdkVersion、buildToolsVersion 两个参数,分别对应编译的 SDK 版本和 Android build tools 版本。

dependencies领域

dependencies{…}这块领域描述了该 Android module 构建过程中所依赖的所有库。

如果想要进一步了解这些领域,可以借助 Android Gradle DSL 的官方文档。

Gradle Task

Task 是 Gradle 中最重要的组成部分。

D:\workspace\AndroidStudioProjects\lichee\FileManager_6>gradlew task
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

> Task :tasks 

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundleDebug - Creates all Debug bundles.
bundleRelease - Creates all Release bundles.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.

Greendao tasks
--------------
greendao - Generates source files for greendao
greendaoPrepare - Finds entity source files for greendao

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'FileManager_6'.
components - Displays the components produced by root project 'FileManager_6'. [incubating]
dependencies - Displays all dependencies declared in root project 'FileManager_6'.
dependencyInsight - Displays the insight into a specific dependency in root project 'FileManager_6'.
dependentComponents - Displays the dependent components of components in root project 'FileManager_6'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'FileManager_6'. [incubating]
projects - Displays the sub-projects of root project 'FileManager_6'.
properties - Displays the properties of root project 'FileManager_6'.
tasks - Displays the tasks runnable from root project 'FileManager_6' (some of the displayed tasks may belong to subprojects).

Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>


BUILD SUCCESSFUL in 7s
1 actionable task: 1 executed
D:\workspace\AndroidStudioProjects\lichee\FileManager_6>

这里介绍几个比较重要的 Task

  • assemble task
    assemble task 用于组合项目的所有输出,它包含了 assembleDebug 和 assembleRelease 两个 Task。通过执行 gradlew assemable 指令,Gradle 会编译出两个 Apk — debug 和 release,如果要执行单独的编译命令,可以使用以下指令。
gradlew assembleRelease
  • Check
    check task 用于执行检查任务。

  • Build
    build task 类似一个组合指令,它执行了 check 和 assemble 的所有工作。

  • Clean
    clean task 用于清理所有的中间编译结果,这个指令使用的非常广泛。当遇到一些比较莫名其妙的 Gradle 编译问题时,通常会先执行 clean task 来清理中间数据,这也类似于 IDE 的 clean 工作。

Gradle 技巧

  • Gradle peer not authenticated
    在更新 Gradle 的依赖包时,经常会发生 peer not authenticated 的异常,导致 Gradle 无法编译。这个问题是 GFW 导致,使用 VPN 进行网络访问即可。另外,通过修改 jcenter 库的地址也能修复这个问题,将 jcenter 的仓库地址指定为 http 而不是默认的 https。
jcenter { url "http://jcenter.bintray.com/"}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值