Android studio 中build.gradle的配置

指定文件名称

// 指定名称
    android.libraryVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith("release.aar")) {
                def fileName = "payegis-did-v${defaultConfig.versionName}-release.aar"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

Android studio默认会有so文件进行压缩优化,加入以下配置可以不压缩so

 packagingOptions{
        doNotStrip "*/armeabi/*.so"
        doNotStrip "*/armeabi-v7a/*.so"
//            doNotStrip "*/x86/*.so"
    }

忽略链接错误

lintOptions {
        abortOnError false
    }

签名文件配置

    signingConfigs {
        def alias = "key0"
        def password = "111111"
        def keyPass = "111111"
        def filePath = "keystore.jks"  //签名文件路径
        debug {
            v1SigningEnabled true
            v2SigningEnabled true
            keyAlias alias
            keyPassword keyPass
            storeFile file(filePath)
            storePassword(password)
        }
        release {
            v1SigningEnabled true
            v2SigningEnabled true
            keyAlias alias
            keyPassword keyPass
            storeFile file(filePath)
            storePassword(password)
        }
    }

配置BuildConfig.java中常量

release {
            buildConfigField "String", "SO_VERSION", project.so_version
            buildConfigField("boolean", "LOG_DEBUG", "false")
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

使用gradle.properties中的配置
如gradle.properties中添加如下键值对

version_code=108
version_name=5.6.0

build.gradle中可以通过project引用

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 25
        versionCode project.version_code.toInteger()
        versionName project.version_name
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

build.gradle使用编译时的额外参数,如在使用gradlew打包指令时添加参数

gradlew build -PtestParams=true

build.gradle可以根据参数进行条件分支处理

if (project.hasProperty("testParams")) {
...
} 
    原文作者:qinghua0706
    原文地址: https://www.jianshu.com/p/fde59b41d29a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞