build.gradle 相关配置笔记

build.gradle

build notes

signingConfigs {
    test{
        keyAlias 'key'
        keyPassword '123456'
        storeFile file('chengzj.jks')
        storePassword '123456'
    }
}

buildTypes {
    release {
       // 是否进行混淆
        minifyEnabled false
        // 签名
        signingConfig signingConfigs.release
    }
    debug {
        minifyEnabled false
        useProguard false
        debuggable true
    }
}

lintOptions {
    abortOnError false //发生错误也继续打包
}

//生成不同渠道包
productFlavors {
    zte { // 签名
        signingConfig signingConfigs.release
    }
}

//修改生成包的包名
android.applicationVariants.all {
    variant -> variant.outputs.each {
        output -> output.outputFile = new File(output.outputFile.parent,
                "Zparking_"+ defaultConfig.versionCode +
                        "_v_" + defaultConfig.versionName +
                        "_"+  buildType.name + ".apk");
    }
}

dexOptions {
    incremental true //优化编译速度
}

AndroidStudio多渠道打包

android studio项目设定打包出来的APK只包含armeabi类型

我们只需要在我们的app—-> build.gradle中添加这个 即可。

ndk {
    abiFilters 'armeabi'    //只生成armv的so
}

具体添加位置:

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 63
    versionName '2.038'
    useLibrary 'org.apache.http.legacy'
    multiDexEnabled true
    ndk {
        abiFilters 'armeabi'    //只生成armv的so
    }
}

INSTALL_FAILED_NO_MATCHING_ABIS 解决方案

错误原因:是由于使用了native libraries 。该native libraries 不支持当前的cpu的体系结构。

解决方案1:

splits {
    abi {
        enable true
        reset()
        include 'x86', 'armeabi-v7a','x86_64'
        universalApk true
    }

解决方案1: 使用对应cpu架构的模拟器。

android build.gradle的一些配置

定义编译出的包名名称

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (variant.buildType.name.equalsIgnoreCase("release") && outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "WeCare_v${versionName}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

def getTime() {
    return new java.text.SimpleDateFormat("yyyyMMdd").format(new Date());
}

Android Studio 3.0.0 修改打包命名

Plugin 3.0.0之前的打包命名修改

 // 自定义APK安装包名
    android.applicationVariants.all {
        variant ->
            variant.outputs.each {
                output ->
                    if (buildType.name.equals("debug")) {
                        output.outputFile = new File(output.outputFile.parent,
                                "app-debug.apk")
                    }
                    if (buildType.name.equals("release")) {
                        output.outputFile = new File(output.outputFile.parent,
                                "app-release.apk")
                    }
            }
    }

现在修改为

// 自定义APK安装包名
    android.applicationVariants.all {
        variant ->
            variant.outputs.all {
                output ->
                    def outputFile = output.outputFile
                    if (outputFile.name.contains("debug")) {
                        outputFileName = new File("../debug/", "app-debug.apk")
                    } 
            }
    }

plugin 3.0.0 新增了variant.outputs.all(Closure action)方法,测试时发现,variant.outputs.each(…)方法中找不到outputFileName ,只有variant.outputs.all(Closure action)才能找到outputFileName 变量

动态修改versionName版本

def getTime() {
    return new java.text.SimpleDateFormat("yyyyMMddHHmm").format(new Date());
}

android {
	...
    defaultConfig {
    	...
        versionName "3.0.${getTime()}"
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值