AndroidStudio Gradle 之 build.gradle

build.gradle分为两种:Project和Module

Project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
// 顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。
buildscript {// gradle执行所需的依赖项

    repositories {// 下载资源的路径(远程仓库)
        google()
        jcenter()
    }
    dependencies {// 需要下载参与构建的工具
        classpath 'com.android.tools.build:gradle:4.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        // 不要在此处放置应用程序依赖项;它们属于单独的build.gradle模块文件
    }
}

allprojects {// 项目的依赖项
    repositories {
        google()
        jcenter()
        flatDir {// 添加配置存储库,该存储库将在多个本地目录中查找依赖项。
                dirs 'libs'
        }
    }
}

task clean(type: Delete) {// 执行gradle clean时,添加的自定义任务
    delete rootProject.buildDir
}

Module:

// apply plugin
// 表示这是一个安卓'库模块',需要依赖'应用模块'运行,打包后为'.aar'
// apply plugin: 'com.android.library'
// 表示这是一个安卓'应用模块',可直接运行,打包后为'.apk'
apply plugin: 'com.android.application'

// 配置项目构建的属性
android {
    // 编译时用的android版本
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()

    defaultConfig {
        // 包名
        applicationId "org.cocos2d.examplecases"
        // 兼容最低安卓版本
        minSdkVersion PROP_MIN_SDK_VERSION
        // 当前适配安卓版本
        targetSdkVersion PROP_TARGET_SDK_VERSION
        // 版本号
        versionCode 1
        // 版本名
        versionName "1.0"

        externalNativeBuild {
            ndkBuild {
                if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                    // skip the NDK Build step if PROP_NDK_MODE is none
                    targets 'cocos2djs'
                    arguments 'NDK_TOOLCHAIN_VERSION=clang'

                    def module_paths = [project.file("../../../cocos2d-x"),
                                        project.file("../../../cocos2d-x/cocos"),
                                        project.file("../../../cocos2d-x/external")]
                    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(";")
                    }
                    else {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(':')
                    }
                    
                    arguments '-j' + Runtime.runtime.availableProcessors()
                }
            }
            ndk {
                abiFilters PROP_APP_ABI.split(':')
            }
        }
    }

    sourceSets.main {// 设置配置目录
        java.srcDirs "../src", "src"
        res.srcDirs "../res", 'res'
        jniLibs.srcDirs "../libs", 'libs'
        manifest.srcFile "AndroidManifest.xml"
    }

    externalNativeBuild {
        ndkBuild {
            if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                // skip the NDK Build step if PROP_NDK_MODE is none
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {// 自动化打包配置
       release {// 生产环境
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
        debug {// 开发环境

        }
    }

    // 生成包的配置
    buildTypes {
        release {// 生产环境
            debuggable false // 是否支持断点调试 默认false
            jniDebuggable false // 是否可以调试NDK代码 默认false
            renderscriptDebuggable false // 是否开启渲染脚本RenderScript调试功能,默认为false
            zipAlignEnabled true // 是否对APK包执行ZIP对齐优化,减小zip体积,增加运行效率 默认true
            minifyEnabled true // 是否对代码进行混淆,默认false
            shrinkResources true// 是否自动优化未使用的资源,该配置生效的前提是minifyEnabled必须为true,默认false
            // proguardFiles 指定混淆的规则文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            if (project.hasProperty("RELEASE_STORE_FILE")) {
                // 设置签名信息,使用前必须先添加signingConfigs闭包,添加相应的签名信息。
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }
        }

        debug {// 开发环境
            debuggable true // 是否支持断点调试 默认true
            jniDebuggable true // 是否可以调试NDK代码
            renderscriptDebuggable true
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }
    }
}

// 打包完成后的一些copy操作和修改apk名字或路径等操作
android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/merged_assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        def sourceDir = "${buildDir}/../../../../.."

        copy {
            from "${sourceDir}"
            include "assets/**"
            include "src/**"
            include "jsb-adapter/**"
            into outputDir
        }

        copy {
            from "${sourceDir}/main.js"
            from "${sourceDir}/project.json"
            into outputDir
        }
    }
}

// 配置项目的依赖关系
// 三种依赖方法:1.本地依赖,2.库依赖,3.远程依赖
// 两种依赖方式:1.implementation,作用范围在此Module,2.api,作用范围在整个项目(和compile一样,后者已在as3.0弃用)
dependencies {
    // 本地依赖,'../libs'下所有的.jar和.aar文件都添加到项目构建中
    implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
    // 本地依赖
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    // 本地依赖
    implementation fileTree(dir: "../../../cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])

    implementation project(':libcocos2dx')

    // 本地依赖,'libs'下所有的.jar和.aar文件都添加到依赖中
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    // 本地依赖,将指定文件添加到依赖中
    implementation file('libs/aaa.jar')
    // 本地依赖,依次将多个指定文件添加到依赖中
    implementation files('libs/aaa.jar', 'libs/bbb.jar')
    // 本地依赖,添加AndroidLibrary类型的Module到依赖中
    implementation project(':libcocos2dx')
    // 库依赖
    implementation 'com.android.support:support-compat:28.0.0'
    // 远程依赖
    implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:6.8.0'
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值