android打包应用及配置的参考

android打包


前言

提示:最近使用到了打包,刚好有时间,今天梳理一下并记录:主要是配置和打包


提示:以下是本篇文章正文内容,下面案例可供参考

一、gradle配置

示例:

1. app.gradle下配置

都在android{
… …在这里配置
}

apply plugin: 'com.android.application'

//获取编译时间
static def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
 
    defaultConfig {
        applicationId "com.backbay2.xxxxx"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
     ....
	//自定义的命名格式,配置1.1
}
	

1.1 打包自定义的命名格式

app.gradle>android{…}

   applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            if (output.name == 'debug') {
                fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${"Test"}_${releaseTime()}.apk"
            } else {
                fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${output.name}_${releaseTime()}.apk"
            }
            output.outputFileName = fileName
        }
    }

编译出现:API ‘variantOutput.getPackageApplication()’ is obsolete and has been replaced with ‘variant.getPackageApplicationProvider()’.

解决方法

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            variant.packageApplicationProvider.get().outputDirectory = new File(project.rootDir.absolutePath + "/apk")
            def fileName
            if (output.name == 'debug') {
                fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${"Test"}_${releaseTime()}.apk"
            } else {
                fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${output.name}-${releaseTime()}.apk"
            }
            output.outputFileName = fileName
        }
    }

生成的包在根目录为apk的文件夹

1.2 签名配置

app.gradle->android{…}
配置签名:
第一步->在gradle.properties中配置
在这里插入图片描述
backbay2.jks放在根目录—> 跟这个位置RELEASE_STOREFILE=../backbay2.jks
一定是…/,因为我们是app.gradle文件使用的这个配置。

gradle.properties

RELEASE_STOREFILE=../backbay2.jks 
RELEASE_STORE_PASSWORD=你的密码
RELEASE_KEY_ALIAS=你写的别名ALias
RELEASE_KEY_PASSWORD=你的密码
    signingConfigs {//设置签名文件
//        debug {
//            storeFile file(RELEASE_STOREFILE)
//            storePassword RELEASE_STORE_PASSWORD
//            keyAlias RELEASE_KEY_ALIAS
//            keyPassword RELEASE_KEY_PASSWORD
//        }
        release {
            storeFile file(RELEASE_STOREFILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }

    }

    buildTypes {//设置build类型
        release {
            //是否优化zip
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            //启用代码混淆
            minifyEnabled true
            //防止方法数量超过65536导致错误
            multiDexEnabled = true
            //混淆规则配置文件
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //指明签名文件位置
            signingConfig signingConfigs.release
           // buildConfigField "Boolean", "DEBUG_MODE", 'false'
        }
        debug {
            shrinkResources false
            minifyEnabled false
            zipAlignEnabled true
            multiDexEnabled = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
           // buildConfigField "Boolean", "DEBUG_MODE", 'true'
        }
    }

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了android打包应用及配置的参考。具体的怎么生成应用密钥博客很多。这里不多叙述了。本文是选择了一个我个人觉得可取的写法。网上其他人的同样适用,看需求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值