//包含这个plugin,说明当前项目是主模块,而库的话怎么包含apply plugin: 'com.android.library'
apply plugin: 'com.android.application'

//还可以直接包含其他gradle文件,可以是本地的文件,也可以直接是一个url
apply from: 'dependencies.gradle'
apply from: 'debug_dependencies.gradle'
apply from: 'publish.gradle'

android {

    //compileSdkVersion 23及以上时,sdk里去掉org.apache.http,需要额外添加
    useLibrary 'org.apache.http.legacy'
    //参数compileSdkVersion可以在gradle.properties里配置
    //systemProp.compileSdkVersion=23
    compileSdkVersion Integer.parseInt(System.properties['compileSdkVersion'])
    buildToolsVersion System.properties['buildToolsVersion']

    defaultConfig {
        applicationId "com.baiwanlu"
        minSdkVersion Integer.parseInt(System.properties['minSdkVersion'])
        targetSdkVersion Integer.parseInt(System.properties['targetSdkVersion'])
        ndk {
            abiFilters "armeabi-v7a", "x86", "mips"
        }
        //可以设置AndroidManifest.xml里的参数,
        //<meta-data android:name="UMENG_CHANNEL" android:value="baidu"/>
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
    }

    //自定义source的路径
    sourceSets {
        //所有类型都一样的路径配置
        main {
            java.srcDirs = ['src']

            res.srcDirs = ['res']

            manifest.srcFile 'AndroidManifest.xml'

            assets.srcDirs = ['assets']

            jniLibs.srcDirs = ['libs']
        }
        //对应不同buildTypes可以设置不同的路径
        debug {
            java.srcDirs = ['debug/src']
        }
        release {
            java.srcDirs = ['release/src']
        }
        forTest {
            java.srcDirs = ['release/src']
        }
    }

    //配置不同buildTypes的签名配置
    signingConfigs {
        debug {
            storeFile file("baiwanlu_debug.keystore")
            storePassword "baiwanlu"
            keyAlias "baiwanlu"
            keyPassword "baiwanlu"
        }

        release {
            storeFile file("android.keystore")
            storePassword "baiwanlu"
            keyAlias "android.keystore"
            keyPassword "baiwanlu"
        }
    }

    //lint的配置
    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
    }

    //添加多个build type
    buildTypes {

        debug {
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'debug-proguard-rules.pro'
            signingConfig signingConfigs.debug
            buildConfigField "String", "ENV_SUFFIX", "\".net/\""//预发环境
            buildConfigField "boolean", "LOG_DEBUG", "true"//是否输出LOG信息
            buildConfigField "boolean", "CRASH_LOG", "false"//收集crash信息
            buildConfigField "boolean", "ANALYTIC_LOG", "false"//统计的日志收集
            buildConfigField "boolean", "TEST_ENTRY", "true"//测试入口
        }
        forTest {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            buildConfigField "String", "ENV_SUFFIX", "\".net/\""//线下环境
            buildConfigField "boolean", "LOG_DEBUG", "true"//是否输出LOG信息
            buildConfigField "boolean", "CRASH_LOG", "true"//收集crash信息
            buildConfigField "boolean", "ANALYTIC_LOG", "false"//统计的日志收集
            buildConfigField "boolean", "TEST_ENTRY", "true"//测试入口
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            buildConfigField "String", "ENV_SUFFIX", "\".com/\""//线上环境
            buildConfigField "boolean", "LOG_DEBUG", "false"//是否输出LOG信息
            buildConfigField "boolean", "CRASH_LOG", "true"//收集crash信息
            buildConfigField "boolean", "ANALYTIC_LOG", "true"//统计的日志收集
            buildConfigField "boolean", "TEST_ENTRY", "false"//测试入口

        }
    }
    //打包时会出现文件重复的错误,可以进行exclude
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'

        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.xml'
        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.properties'
        exclude 'META-INF/maven/com.squareup.okio/okio/pom.xml'
        exclude 'META-INF/maven/com.squareup.okio/okio/pom.properties'
    }
    //对输出可以做自定义
    applicationVariants.all {
        variant ->
                variant.outputs.each {
                    output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('.apk')) {
                            if (variant.buildType.name == "release") {
                                def fileName = "baiwanluandroid.apk"
                                output.outputFile = new File(outputFile.parent, fileName)
                            } else if (variant.buildType.name != "debug"){
                                def fileName = "baiwanluandroid-${defaultConfig.versionName}-${variant.buildType.name}.apk"
                                output.outputFile = new File(outputFile.parent, fileName)
                            }
                        }
                }
                variant.assemble.doLast {
                    variant.outputs.each {
                        output ->
                            println "aligned " + output.outputFile
                            println "unaligned " + output.packageApplication.outputFile

                            File unaligned = output.packageApplication.outputFile;
                            File aligned = output.outputFile
                            if (!unaligned.getName().equalsIgnoreCase(aligned.getName())) {
                                println "deleting " + unaligned.getName()
                                unaligned.delete()
                            }
                    }
                }
    }
}

//自定义task
task copyTask {
    doLast {
        println 'copyTask'
        copy {
            from "build/outputs/"
            into '../outputs'
        }
    }
}

task deleteTask(type: Delete) {
    doLast {
        println 'deleteTask'
        FileTree tree = fileTree(dir: "../outputs/");
        tree.each { File file ->
            delete(file)
        }
        FileTree tree2 = fileTree(dir: "build/outputs");
        tree2.each { File file ->
            delete(file)
        }
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.12'

}

apply from: 'test.gradle'

apply plugin: 'findbugs'
task findbugs(type: FindBugs) {
    ignoreFailures = false
    effort = "max"
    reportLevel = "high"
    classes = fileTree('build/intermediates/classes/debug')
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    visitors = ["FindNullDeref"]

    classpath = files()
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

官网参考链接

标签: none

评论已关闭