当前位置: 移动技术网 > 移动技术>移动开发>Android > Android Studio apk打包自定义包名

Android Studio apk打包自定义包名

2019年04月18日  | 移动技术网移动技术  | 我要评论
android studio apk打包自定义包名,直接上代码: android { ...... android.applicationvariants

android studio apk打包自定义包名,直接上代码:

android {
    ......
    android.applicationvariants.all { variant ->
        variant.outputs.all { output ->
            def outputfile = output.outputfile
            if (outputfile != null && outputfile.name.endswith('.apk')) {
                outputfilename = defaultconfig.applicationid.subsequence(defaultconfig.applicationid.lastindexof(".") + 1, defaultconfig.applicationid.length()) + "-v" + defaultconfig.versioncode + "-" + releasetime() + "-" + output.basename + ".apk"
            }
        }
    }
}
def releasetime() {
    return new date().format("yyyymmddhhmmss", timezone.gettimezone("utc"))
}

生产的包名为:项目名-版本号-打包时间-编译类型.apk
该代码只能在3.0.0及以上使用,variant.outputs.all 方法只在3.0.0才出现的
如果你想在3.0.0以下使用你可以试试这个:

android {
    ......
    variant.outputs.each { output ->
            if ("release".equals(variant.buildtype.name)) {
                println("releasebasename:"+variant.buildtype.name)
                def string = defaultconfig.applicationid.subsequence(defaultconfig.applicationid.lastindexof(".") + 1,
                        defaultconfig.applicationid.length()) + "-v" + defaultconfig.versioncode + "-" + releasetime() + "-" + output.basename + ".apk";
                output.outputfile = new file(output.outputfile.parent, string)
            }else if ("debug".equals(variant.buildtype.name)) {
                println("debugbasename:"+variant.buildtype.name)
                def string = defaultconfig.applicationid.subsequence(defaultconfig.applicationid.lastindexof(".") + 1,
                        defaultconfig.applicationid.length()) + "-v" + defaultconfig.versioncode + "-" + releasetime() + "-" + output.basename + ".apk";
                output.outputfile = new file(output.outputfile.parent, string)
            }
        }
}
def releasetime() {
    return new date().format("yyyymmddhhmmss", timezone.gettimezone("utc"))
}

生产的包名为:项目名-版本号-打包时间-编译类型.apk
这个有个bug就是在打包时能正常生产apk包,但是直接运行安装到手机时会提示找不到包名,初步分析是因为包名中含有时间戳,去掉分秒的时间就可以了:”yyyymmddhh”。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网