使用public.xml來自定義資源id,支持最新gradle 5.4.1

來源: https://blog.csdn.net/m0_37165550/article/details/102716420node

總結: 瞭解 aapt 是Android Asset Packaging Tool的縮寫,是編譯和打包資源的工具。而aapt2是在aapt上作了優化android

 

熟悉aapt的運行過程,能夠在中間實現本身想要的功能api

新建:app

App\src\main\res\values\public.xmlide

內容相似以下:工具

<resources>
    <public type="dimen" name="status_bar_height" id="0x7f0600e9"  />
</resources>

在project根部放置腳本public-xml.gradlegradle

import org.gradle.util.GFileUtils
apply plugin: PublicPlugin

class PublicPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.afterEvaluate {
            if (project.plugins.hasPlugin("com.android.application")) {
                def android = project.extensions.getByName("android")
                def aaptOptions = android.aaptOptions
                android.applicationVariants.all {def variant ->
                    project.logger.error "xxxxx ${variant.name.capitalize()}"
//       def processResourcesTask = project.tasks.getByName("process${variant.name.capitalize()}Resources")
                    project.logger.error "xxxxx ${aaptOptions}"
                    if (aaptOptions) {
//                        def aaptOptions = processResourcesTask.aaptOptions
                        File publicTxtFile = project.rootProject.file('generate_public.txt')
                        //public文件存在,則應用,不存在則生成
                        if (publicTxtFile.exists()) {
                            project.logger.error "${publicTxtFile} exists, apply it."
                            //aapt2添加--stable-ids參數應用
                            aaptOptions.additionalParameters("--stable-ids", "${publicTxtFile}")
                        } else {
                            project.logger.info "${publicTxtFile} not exists, generate it."
                            //aapt2添加--emit-ids參數生成
                            aaptOptions.additionalParameters("--emit-ids", "${publicTxtFile}")
                        }
                    }

                }
            }
        }
    }
}

task convertPublicXmlToPublicTxt(){

    project.logger.error "start convertPublicXmlToPublicTxt"
//源public.xml
    File publicXmlFile = project.rootProject.file('pluginApp/src/main/res/values/public.xml')
//目標public.txt
    File publicTxtFile = project.rootProject.file('generate_public.txt')
//包名
    String applicationId = "com.geniatech.oobe"
    GFileUtils.deleteQuietly(publicTxtFile)
    GFileUtils.touch(publicTxtFile)
    def nodes = new XmlParser().parse(publicXmlFile)
//        Pattern drawableGeneratePattern = Pattern.compile('^(.*?_)([0-9]{0,})$')
    nodes.each {
        project.logger.error "${it}"
/*            if ("drawable".equalsIgnoreCase("${it.@type}")) {
//以'_數字'結尾的drawable資源,此類資源是aapt編譯時生成的nested資源,如avd_hide_password_1, avd_hide_password_2
//可是可能會有其餘資源摻雜,如abc_btn_check_to_on_mtrl_000, abc_btn_check_to_on_mtrl_015
//爲了將此類資源過濾掉,將正則匹配到的數字轉成int,對比原始數字部分匹配字符串,若是一致,則是aapt生成
//重要:爲了不此類nested資源生成順序發生改變,應該禁止修改此類資源
//aapt生成的是如下表1開始,aapt2是如下標0開始,所以轉換的過程須要-1
                Matcher matcher = drawableGeneratePattern.matcher(it.@name)
                if (matcher.matches() && matcher.groupCount() == 2) {
                    String number = matcher.group(2)
                    if (number.equalsIgnoreCase(Integer.parseInt(number).toString())) {
                        String prefixName = matcher.group(1)
                        publicTxtFile.append("${applicationId}:${it.@type}/\$${prefixName}_${Integer.parseInt(number) - 1} = ${it.@id}\n")
                        return
                    }
                }
            }*/
        publicTxtFile.append( "${applicationId}:${it.@type}/${it.@name} = ${it.@id}\n")
    }
    doFirst{
        project.logger.error 'convertPublicXmlToPublicTxt doFirst'
    }
    doLast {
        project.logger.error 'convertPublicXmlToPublicTxt doLast'
    }
}

 

在根部的build.gradle中聲明優化

頂部聲明ui

apply from: 'public-xml.gradle'

 

底部聲明this

this.afterEvaluate { Project project ->
    project.logger.error "start afterEvaluate"
    tasks.matching {
        project.logger.error "test task ${it.name}"
        it.name.startsWith('wrapper')
    }.each { task ->
        task.dependsOn(convertPublicXmlToPublicTxt)
    }
}
相關文章
相關標籤/搜索