我有我自己的Gradle插件,我想添加一個任務,它將從main
文件夾中爲啓動器圖標添加一個任務,併爲每個構建類型/風味文件夾設置輸出圖標根據用戶的擴展配置不同的顏色。使用資源生成任務添加資源時的「重複資源」
我已經添加用於應用變體的任務:
variant.registerResGeneratingTask(tintTask, new File("${project.buildDir}/generated/res/${variant.name}"))
然後在任務我執行上述操作。在此之前一切正常 - 生成源文件並將文件夾標記爲資源文件夾。
問題是,當我嘗試創建一個構建和流程擊中mergeXXXResources
任務(在這種情況下,xxx == debug)。
在這一點上,我得到一個例外,比較main/res
中的mipmap-[dpi]-v4/ic_launcher
與generated/res/debug
中的mipmap-[dpi]-v4/ic_launcher
。
例如:
Execution failed for task ':app:mergeDebugResources'.
[mipmap-hdpi-v4/ic_launcher] /{proj_location}/android_example/app/src/main/res/mipmap-hdpi/ic_launcher.png
[mipmap-hdpi-v4/ic_launcher] /{proj_location}/android_example/app/build/generated/res/debug/mipmap-hdpi/ic_launcher.png:
Error: Duplicate resources
我嘗試不同的位置對我的輸出文件,但我不認爲這有什麼差別。我期望資源合併能夠識別生成的資源並在最終輸出中解析它們,但顯然我正在做一些可怕的錯誤。
我試過使用轉換API,但也許是因爲缺乏文檔和我的理解不夠我的嘗試不是很成功(我找不到類似於我的方式的資源文件在轉換操作期間定位java文件)。
我正在尋找一條關於如何解決我目前問題的建議,或者是一個可以執行我最初着手實現的任務的替代方法。
編輯:的請求,我的任務操作代碼:
@TaskAction
def convertLauncherIcons() {
def android = project.extensions.getByType(AppExtension)
File outputDir = new File("${project.buildDir}/generated/tintLaunchIcons/res/${taskVariant.name}")
android.sourceSets.each {
if ("main".equals(it.name)) {
it.res.srcDirs.each { dirIt ->
dirIt.absoluteFile.list().each { resDir ->
if (resDir.startsWith("mipmap-")) {
def relIconPath = "${resDir}/ic_launcher.png"
File launcherFile = new File(dirIt.absolutePath, relIconPath);
if (launcherFile.exists()) {
BufferedImage img = ImageIO.read(launcherFile);
/* IMAGE MANIPULATION HERE */
File outputFile = new File(outputDir, relIconPath);
if (!outputFile.exists()) {
File parent = outputFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
outputFile.createNewFile();
}
println "writing file ${outputFile.canonicalPath}"
ImageIO.write(img, "png", outputFile);
} ...
您的生成資源的輸出路徑是什麼? – Kelevandos
@Kelevandos是否指項目的合併資源輸出?這是標準的,所以'app/build/intermediates/incremental/mergeDebugResources', 'app/build/intermediates/res/merged/debug'和 'app/build/intermediates/blame/res/debug'。但是,只有在註冊爲resGeneratingTask的任務之後纔會執行此操作。 – zilinx
我認爲這裏的問題是你在構建目錄中的任務。嘗試將'variant.registerResGeneratingTask'的輸出路徑設置爲主目錄,並讓Gradle負責將其合併到構建中。讓我知道它是否有幫助:) – Kelevandos