gradle篇之groovy

304 阅读3分钟

gradle问题

gradle signingReport生成SHA1

  • 须保持gradle plugin 与 gradle版本对应,否则失败
  • (4.2.2--7.2)
> Task :app:signingReport
Variant: debug
Config: debug
Store: C:\Users\xxx\.android\debug.keystore
Alias: AndroidDebugKey
MD5: F5:8F:8A:FA:B8:68:...
SHA1: B8:23:35:14:...
SHA-256: C2:6D:2B:F0:F1:95:0B:...
Valid until: 2053年8月10日星期日

build.gradle(:app) 属性介绍

plugins {
    // 标识应用程序模块
    // com.android.library标识库模块,需依附应用程序运行
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    ...
}

def keystoreProps = new Properties()
keystoreProps.load(new FileInputStream(rootProject.file("keystore.properties")))

def resDirs = [  
    'ui'
]

android {
    defaultConfig {
        applicationId "com.dcxing.xxx" // 包名
        minSdk 24 // 最低兼容版本
        targetSdk 33 // 目标版本
        versionCode 1 // 版本号
        versionName "1.0.0.1" // 版本名称
        vectorDrawables.useSupportLibrary = true

        // 使用AndroidJUnitRunner进行单元测试
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        
        // 过滤abi so库
        ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64' }
    }

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

    // 目录指向配置
    sourceSets {
        main {
            // 指定lib库目录
            jniLibs.srcDirs = ['libs']
            
            // 指定目录下生成资源路径
            resDirs.forEach {  
                res.srcDirs += 'src/main/java/com/dcxing/' + it + '/res'  
            }
        }
    }

    dataBinding {
        // 使用dataBinding
        enabled = true
    }

    buildFeatures {
        // 默认不生成BuildConfig,修改如下即可
        buildConfig = true
    }

    applicationVariants.all{
        variant ->
            variant.outputs.all{
                outputFileName = "App-${variant.flavorName}-${variant.buildType.name}-${defaultConfig.versionName}.apk"
            }
    }

    signingConfigs {
        // 自动化打包配置
        beta {
            // beta环境
            storeFile file(keystoreProps['storeFile'])
            storePassword keystoreProps['storePassword']
            keyAlias keystoreProps['betaKeyAlias']
            keyPassword keystoreProps['betaKeyPassword']
        }
        
        xxx {
            storeFile file(keystoreProps['storeFile'])
            storePassword keystoreProps['storePassword']
            keyAlias keystoreProps['keyAlias']
            keyPassword keystoreProps['keyPassword']
	}

	dev {
	    storeFile file("../dev.jks")
            storePassword 'android'
            keyAlias 'android'
            keyPassword 'android'
	}
    }

    // 渠道配置,可设置不同包名,版本号,应用名等
    // 配置后使用gradlew assembleRelease命令打包
    flavorDimensions "model", "chipset"
    productFlavors {
        xvn {
            dimension "model"
            versionNameSuffix ".xvn"
        }
        oem {
            dimension "model"
            versionNameSuffix ".oem"
        }
        beta {
            dimension "chipset"
            signingConfig signingConfigs.beta
        }
    }

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

    configurations {
        // 排除项目中的依赖,解决多版本依赖冲突问题
        // all*.exclude group: 'com.google.code.gson'
        // all*.exclude group: 'com.squareup.okhttp3'
        // all*.exclude group: 'com.squareup.okio'
    }
    
    // 打包时相关配置
    packagingOptions{
        // pickFirsts当有重复文件时打包会报错,配置后使用第一个匹配的文件打包进入apk
        // 表示当apk中有重复的META-INF目录下有重复的LICENSE文件时,只用第一个,这样打包就不会报错
        pickFirsts = ['META-INF/LICENSE']

        // merges当出现重复文件时,合并重复的文件,然后打包入apk
        // 这个是有默认值的merges = [] 这样会把默默认值去掉,所以我们用下面这种方式,在默认值后添加
        merge 'META-INF/LICENSE'

        // 这个是在同时使用butterknife、dagger2做的一个处理。同理,遇到类似的问题,只要根据gradle的提示,做类似处理即可。
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies { // 依赖
    // 本地jar包依赖
    implementation fileTree(include: ['*.jar'], dir: 'libs')
}
  • keystore.properties
storeFile=../xxx.jks
storePassword=android
keyAlias=android
keyPassword=android

Algorithm HmacPBESHA256 not available

  • 修改jdk版本至16及以上

java.lang.ClassNotFoundException: Didn't find class "androidx.databinding.DataBinderMapperImpl"

  • 依赖lib使用dataBinding,主module未使用,修改如下:
buildFeatures {
    dataBinding true
}