安卓module目录下的build.gradle介绍

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41659081/article/details/99675306
apply plugin: 'com.android.application' //表示是一个应用程序的模块,可独立运行
//apply plugin: 'com.android.library' //表示是一个依赖库,不能独立运行
android {
	compileSdkVersion 29   //指定项目的编译版本
    buildToolsVersion "29.0.0"//指定项目构建工具的版本;其中包括了打包工具aapt、dx等等
    
    defaultConfig {
        applicationId "com.example.example" //指定包名
        minSdkVersion 19//指定最低的兼容的Android系统版本
        targetSdkVersion 29//指定你的目标版本,表示你在该Android系统版本已经做过充分的测试
        versionCode 1   //版本号
        versionName "1.0"   //版本名称
        multiDexEnabled true  
        //当方法数超过65535(方法的索引使用的是一个short值,
        //而short最大值是65535)的时候允许打包成多个dex文件,动态加载dex。这里面坑很深啊
    }
	
	//如果要添加aar包需要添加以下代码
	repositories {
    	flatDir {
        	dirs 'libs'
    	}
	}


	//给AS设置内存
	dexOptions {
        javaMaxHeapSize "4g"
    }

	//程序在编译的时候会检查lint,有任何错误提示会停止build,我们可以关闭这个开关
    lintOptions {
        abortOnError false  
        //即使报错也不会停止打包
        checkReleaseBuilds false  
        //打包release版本的时候进行检测
    }

	signingConfigs {
        release {//发布版本的签名配置
            storeFile file('xxx')
            keyAlias 'xxx'
            storePassword 'xxx'
            keyPassword 'xxx'
        }
        debug {//调试版本的签名配置
            storeFile file('xxx')
            keyAlias 'xxx'
            storePassword 'xxx'
            keyPassword 'xxx'
        }
    }

    
    buildTypes {
        release {
            buildConfigField("boolean", "LOG_DEBUG", "false")//配置Log日志
            buildConfigField("String", "URL_PERFIX", "\"https://release.cn/\"")// 配置URL前缀
            minifyEnabled false//是否对代码进行混淆
            shrinkResources true //是否清理无用资源,依赖于minifyEnabled
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//指定混淆的规则文件
            signingConfig signingConfigs.release//设置签名信息
            pseudoLocalesEnabled false//是否在APK中生成伪语言环境,帮助国际化的东西,一般使用的不多
            zipAlignEnabled true//是否对APK包执行ZIP对齐优化,减小zip体积,增加运行效率
            applicationIdSuffix 'test'//在applicationId 中添加了一个后缀,一般使用的不多
            versionNameSuffix 'test'//在applicationId 中添加了一个后缀,一般使用的不多
        }
        debug {
            buildConfigField("boolean", "LOG_DEBUG", "true")//配置Log日志
            buildConfigField("String", "URL_PERFIX", "\"https://test.com/\"")// 配置URL前缀
            minifyEnabled false//是否对代码进行混淆
            shrinkResources true //是否清理无用资源,依赖于minifyEnabled
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//指定混淆的规则文件
            signingConfig signingConfigs.debug//设置签名信息
            debuggable false//是否支持断点调试
            jniDebuggable false//是否可以调试NDK代码
            renderscriptDebuggable false//是否开启渲染脚本就是一些c写的渲染方法
            zipAlignEnabled true//是否对APK包执行ZIP对齐优化,减小zip体积,增加运行效率
            pseudoLocalesEnabled false//是否在APK中生成伪语言环境,帮助国际化的东西,一般使用的不多
            applicationIdSuffix 'test'//在applicationId 中添加了一个后缀,一般使用的不多
            versionNameSuffix 'test'//在applicationId 中添加了一个后缀,一般使用的不多
        }
    }

	//打包APK重命名
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            def fileName
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {//如果是release包
                    fileName = "release_xxx.apk"
                } else if (variant.buildType.name.equals('debug')) {//如果是debug包
                    fileName = "debug_xxx.apk"
                }
                outputFileName = fileName
            }
        }
    }

	
}

猜你喜欢

转载自blog.csdn.net/qq_41659081/article/details/99675306