gpt4 book ai didi

android - "Duplicate resources"使用资源生成任务添加资源时

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:14 28 4
gpt4 key购买 nike

我有自己的 Gradle 插件,我想在其中添加一个任务,该任务将从 main 文件夹中获取启动器图标,并根据用户的不同为每个构建类型/ flavor 文件夹输出图标扩展配置。

我已经为应用程序变体添加了任务:

variant.registerResGeneratingTask(tintTask, new File("${project.buildDir}/generated/res/${variant.name}"))

然后在任务中我执行上面描述的操作。直到这里一切都很好 - 源已生成并且文件夹被标记为资源文件夹。

问题是当我尝试创建一个构建时,流程遇到了 mergeXXXResources 任务(在本例中为 xxx == debug)。

在这一点上,我将 main/res 中的 mipmap-[dpi]-v4/ic_launchergenerated/res/中的进行比较时出现异常调试

例如:

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

我为我的输出文件尝试了不同的位置,但我认为这没有任何区别。我希望资源合并能够识别生成的资源并在最终输出中解析它们,但显然我做错了一些可怕的事情。

我已经尝试使用 Transform 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);
} ...

最佳答案

好的,总结一下我们在上面的评论讨论中得出的结论:


这里的问题很可能是您试图在 gradle 脚本将 main 目录与当前风格合并之前修改文件。我的建议是您应该在合并完成后尝试触发您的任务,如下所示:

variant.mergeResources.doLast { //fire your task here }

这应该会更简单一些,并且会节省您大量研究 Android gradle 插件实际如何处理这些东西的时间:-)

关于android - "Duplicate resources"使用资源生成任务添加资源时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912445/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com