Android Gradle脚本(一)打包重命名

使用Androidstudio开发app,一般打包会默认生成app-debug.apk、app-release.apk、app-release-unsigned.apk等包,我们如果有定制化的需求,比如在包名上体现出版本号等,这时我们就需要适当了解gradle脚本。

  • 在app module中配置多个工程包
//根据项目工程配置出不同的包
    flavorDimensions "app"
    productFlavors {
        comone {
            applicationId "com.example.one"
            dimension "app"
            versionCode 1
            versionName "1.0"
            signingConfig signingConfigs.comone
        }
        comontwo {
            applicationId "com.example.two"
            dimension "app"
            versionCode 1
            versionName "2.0"
            signingConfig signingConfigs.comtwo
        }
    }
  • 然后是其对应的签名配置 comone、comtwo
signingConfigs {
        comone {
            keyAlias 'hsemploy'
            keyPassword '*****'
            storeFile file('/Users/zhanglei/Desktop/employ')
            storePassword '*****'
        }
        comtwo {
            keyAlias 'hsemploy'
            keyPassword '*****'
            storeFile file('/Users/zhanglei/Desktop/employ')
            storePassword '*****'
        }
    }
  • 如果对于特定的包需要重命名移动到其他目录(如release包)
//定义一个装apk文件路径的数组
    def fileArray = []
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.contains('release')) {
                //获取每个打包产物
                def variantProd = variant.productFlavors[0]
                def fileName = "example_${variantProd.versionName}_${variantProd.versionCode}" + "_${variantProd.name}.apk"
                println "自定义输出apk的名字:" + fileName;
                outputFileName = fileName;
                println "输出apk地址:" + outputFile.parentFile.absolutePath + java.io.File.separator + fileName
                fileArray.add(outputFile.parentFile.absolutePath + java.io.File.separator + fileName);
            }

        }
    }
  • 我们在build构建完成之后才会开始执行文件移动改名的操作,在android{ } 里面写一个apk的复制重命名的任务(task)
build {
        doLast() {
            println "任务1编译打包完成后需要复制apk的数量:" + fileArray.size()
            forEachFile(fileArray)
        }
    }
  • 对于具体如何执行的,定义一个执行复制命名的方法,我们放在android目录外面
def forEachFile(fileArray) {
    fileArray.forEach { file ->
        //遍历进行文件操作
        println "任务3遍历apk文件"
        rename_and_moveout_apk(file)
    }
}


def rename_and_moveout_apk(orignalFile) {
    def intoFile = rootDir.parentFile.getAbsolutePath() + File.separator + "apk"
    copy {
        from orignalFile
        into intoFile
        println "任务4复制apk到指定位置:" + intoFile
        rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "")
        println "任务5修改apk的命名"
    }
}
  • 最后演示输出效果,可以在terminal中输入./gradlew build或者是gradle build(后面这个需要系统环境)
    在这里插入图片描述
  • 最后关于这个的全部gradle脚本代码
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    signingConfigs {
        comone {
            keyAlias 'hsemploy'
            keyPassword 'hsemploy'
            storeFile file('/Users/zhanglei/Desktop/employ')
            storePassword 'hsemploy'
        }
        comtwo {
            keyAlias 'hsemploy'
            keyPassword 'hsemploy'
            storeFile file('/Users/zhanglei/Desktop/employ')
            storePassword 'hsemploy'
        }
    }

    //根据项目工程配置出不同的包
    flavorDimensions "app"
    productFlavors {
        comone {
            applicationId "com.example.one"
            dimension "app"
            versionCode 1
            versionName "1.0"
            signingConfig signingConfigs.comone
        }
        comontwo {
            applicationId "com.example.two"
            dimension "app"
            versionCode 1
            versionName "2.0"
            signingConfig signingConfigs.comtwo
        }
    }

    //定义一个装apk文件路径的数组
    def fileArray = []
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.contains('release')) {
                //获取每个打包产物
                def variantProd = variant.productFlavors[0]
                def fileName = "example_${variantProd.versionName}_${variantProd.versionCode}" + "_${variantProd.name}.apk"
                println "自定义输出apk的名字:" + fileName;
                outputFileName = fileName;
                println "输出apk地址:" + outputFile.parentFile.absolutePath + java.io.File.separator + fileName
                fileArray.add(outputFile.parentFile.absolutePath + java.io.File.separator + fileName);
            }

        }
    }
    build {
        doLast() {
            println "任务1编译打包完成后需要复制apk的数量:" + fileArray.size()
            forEachFile(fileArray)
        }
    }
}
def forEachFile(fileArray) {
    fileArray.forEach { file ->
        //遍历进行文件操作
        println "任务3遍历apk文件"
        rename_and_moveout_apk(file)
    }
}


def rename_and_moveout_apk(orignalFile) {
    def intoFile = rootDir.parentFile.getAbsolutePath() + File.separator + "apk"
    copy {
        from orignalFile
        into intoFile
        println "任务4复制apk到指定位置:" + intoFile
        rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "")
        println "任务5修改apk的命名"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流星雨在线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值