Android gradle编译常用脚本

这篇博客探讨了如何使用Gradle进行Android项目的编译,包括AAR包的命名和输出,以及APK的命名和归档脚本的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gradle aar命名及输出脚本

 libraryVariants.all { variant ->
 //修改产物输出路径
        variant.getPackageLibrary().destinationDir = new File(project.rootDir.absolutePath + "/output")

//        if (variant.buildType.name == 'debug') {
        variant.outputs.all { output ->
            def outputFile = output.outputFile
//                if (outputFile != null && outputFile.name.endsWith('debug.aar')) {
            if (outputFile != null) {
                outputFileName = "${project.name}_${variant.buildType.name}_${defaultConfig.versionName}.aar"
            }
        }
    }

gradle apk命名及归档脚本

 android.applicationVariants.all { variant ->
        variant.outputs.all {
            if (variant.buildType.name == "debug") {
                outputFileName = "MyApp_debug.apk"
            } else {
                outputFileName = "MyApp.apk"
            }
        }

        //编译完成后将apk复制到指定目录, 这个操作是可选的,如果不需要可以直接将代码删除
        variant.assemble.doLast {
            variant.outputs.all {
                //复制到根目录下的output文件夹 如果不需要可以直接将代码删除
                File desFilePath = new File("${rootDir}/output")
                delete desFilePath
                if (variant.buildType.name == 'release') {
                    copy {
                        from outputFile
                        into desFilePath
                    }
                    releaseApk()
                }
            }
        }
    }

//apk自动发布脚本
def releaseApk() {
    // 加载properties
    def properties = new Properties()
    def inputStream = project.rootProject.file('local.properties').newDataInputStream()
    properties.load(inputStream)

    //apk最终发布路径 (电脑上的真实目录, 配置在local.properties)
    String releaseDir = properties.getProperty("release.dir")
    if (releaseDir == null || releaseDir.isEmpty()) {
        println("没有配置发布目录,请手动发布")
        return
    }

    copy {
        from('../output')
        into("${releaseDir}/apks")
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值