Gradle批量打Android渠道包

// 以下為打渠道包方法和任務(wù)

ArrayList<String> getChannelList() {
    ArrayList<String> list = new ArrayList<String>()
    def path = rootProject.projectDir.absolutePath + '/channel/channel.txt'
    file(path).eachLine { channel ->
        list.add(channel)
    }
    return list
}
// 往META-INF里添加空白文件
task doChannelPkg() << {
    def name = rootProject.hasProperty("APK_NAME") ? APK_NAME : ""
    if (name == null || "".equals(name)) {
        throw new RuntimeException("doChannelPkg name should not be null")
    }
    def dir = rootProject.projectDir.absolutePath + '/app/build/outputs/apk/prod/release'

    getChannelList().each { channel ->
        def destName = "${name.replace(".apk", "")}_${channel}.apk"
        copy {
            from "${dir}/${name}"
            into dir
            rename {
                destName
            }
        }

        def channelDir = "${dir}/${channel}"
        file(channelDir).mkdir()
        file("${channelDir}/META-INF").mkdir()
        file("${channelDir}/META-INF/channel_${channel}").createNewFile()
        ant.zip(basedir: channelDir, includes: "META-INF/channel_${channel}", keepcompression: true, update: true,
                destfile: "${dir}/${destName}")
        file(channelDir).deleteDir()

    }
}

// Mac上打包沒通過,待查原因
task doChannelRePkg() << {
    def name = rootProject.hasProperty("APK_NAME") ? APK_NAME : ""
    if (name == null || "".equals(name)) {
        throw new RuntimeException("doChannelPkg name should not be null")
    }

    def rootDir = rootProject.projectDir.absolutePath
    def dir = rootDir + '/app/build/outputs/apk/dev/debug'

    def apktoolJar = 'apktool_2.3.3.jar'
    def apktool = rootDir + "/channel/${apktoolJar}"
    copy {
        from apktool
        into dir
    }

    getChannelList().each { channel ->
        def destName = "${name.replace(".apk", "")}_${channel}.apk"

        copy {
            from "${dir}/${name}"
            into dir
            rename {
                destName
            }
        }

        def channelDir = "${dir}/${channel}"
        file(channelDir).mkdir()
        file("${channelDir}/assets").mkdir()
        def channelFile = file("${channelDir}/assets/channel")
        if (!channelFile.exists()) {
            channelFile.createNewFile()
        }
        def bw = new BufferedWriter(new FileWriter(channelFile))
        bw.write("${channel}")
        bw.close()

        // 解壓縮
        exec {
            workingDir dir
            commandLine 'java', '-jar', apktoolJar, 'd', '-f', destName, '-o', "${channel}/base-apk"
        }

        // 寫入渠道號
        copy {
            from channelFile
            into "${dir}/${channel}/base-apk/assets"
        }

        // 打包
        exec {
            workingDir dir
            commandLine 'java', '-jar', apktooJar, 'b', "${channel}/base-apk", '-o',
                    "${destName.replace(".apk", "")}-unsigned.apk"
        }

        // 簽名
        def keyStore = rootProject.projectDir.absolutePath + '/app/key_store'
        copy {
            from keyStore
            into dir
            rename {
                'key_store'
            }
        }
        def storePass = 'xxx'
        def alias = 'xxx'
        def keyPass = 'xxx'

        exec {
            workingDir dir
            commandLine 'jarsigner',
                    '-sigalg', 'MD5withRSA',
                    '-digestalg', 'SHA1',
                    '-keystore', 'key_store',
                    '-storepass', storePass,
                    '-keypass', keyPass,
                    '-signedjar', "${destName.replace(".apk", "")}_signed.apk",
                    destName, alias
        }

        file(channelDir).deleteDir()

    }

}

ApkTooll 鏈接:https://ibotpeaches.github.io/Apktool/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    wgl0419閱讀 6,597評論 1 9
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,887評論 2 45
  • 靜靜的文閱讀 155評論 0 0
  • 1.放任在處理自我情緒的力量 情緒出來不要想去控制。把自己當(dāng)作旁觀者,靜靜的觀察它的升起和圍繞,直到消失。不去干...
    weeklybright閱讀 335評論 0 3
  • 每個人都有 想要將開心喜悅或煩憂 與人訴說分享的需求 曾經(jīng)只有一個人的時候 就自己默默回味 直到遇見另一個人 以為...
    瑾瑜菇?jīng)?/span>閱讀 362評論 0 0

友情鏈接更多精彩內(nèi)容