在Android中使用特定于构建的aaptOptions(用于排除特定的Assets文件/文件夹) [英] Using Build-specific aaptOptions in Android (for excluding specific Assets files/folders)

查看:858
本文介绍了在Android中使用特定于构建的aaptOptions(用于排除特定的Assets文件/文件夹)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于从事Android Webview应用程序的开发,我才刚刚开始在其中使用node_modules文件夹,所以自然地我对如何排除它进行了一些研究,最终得到了

Been working on an Android Webview app and I've only just now started to use a node_modules folder in there, so naturally I did some research on how to exclude it and I ended up at this question here

尝试了一些答案,而实际上最适合我的答案是关于aaptOptions的答案

Tried some of the answers and the one that actually worked the best for me was the one about aaptOptions

因此,当然,我自然会在尝试使用它,试图找出可行的方法,然后我成功地从调试apk中排除了一些文件夹.

So of course naturally I'm playing around with it, trying to figure out what works, and I succeed at excluding a few folders from the debug apk.

            aaptOptions {
                ignoreAssetsPattern '!node_modules:!jsunmin:!.idea:!jade:!css-scss:'
            }

我确实可以确认这些文件夹没有包含在Android Studio的最终APK中!成功!

And I can indeed confirm that those folders aren't included in the final APK in Android studio! Success!

因此,我意识到我可以做一些聪明的事情:当我运行我的应用程序,在终端上对其进行测试,对其进行调试时,我希望在某些位置自动放置某些凭据-我这样做javascript-但显然我不希望这些凭据包含在我可能发送给全世界的APK中-即使我已经对其进行了编码,除非进行调试,否则它们不会自动输入凭据,因此凭据仍然实际上是在javascript文件中,并且大概有人可以在那里查看并看到它们!

So then I realize that I can do something a bit clever: when I'm running my app, testing it on my end, debugging it, I like to have certain credentials in certain places automatically put in - I do this with javascript - but obviously I don't want these credentials included in the APKs I might send out into the world -- even though I've already coded it to not input the credentials automatically unless I'm debugging, the credentials are still actually in the javascript files, and presumably someone could look in there and see them!

所以我的想法是创建一个文件"example-creds.js",并使用aaptOptions不包含该文件,仅在发行版本中使用,所以我想出了类似以下内容(删除了更多详细信息) ):

So my idea was to create a file, 'example-creds.js', and use aaptOptions to not include that file, ONLY on release builds, so I came up with something that looks approximately like this (extra details stripped out):

android {
    buildTypes {
        release {
            aaptOptions {
                ignoreAssetsPattern '!node_modules:!jsunmin:!.idea:!jade:!css-scss:!example-creds.js:'
            }
        }

        debug {
            aaptOptions {
                ignoreAssetsPattern '!node_modules:!jsunmin:!.idea:!jade:!css-scss:'
            }
        }
    }
}

但是不起作用!我已经对其进行了测试,无论最后定义的aaptOptions是什么,它都可以运行,而与构建类型无关.如果我将版本放到调试后,则在任何一个版本中都不会得到示例示例.如果我在发布后进行调试,则两者都将获得示例代码.

BUT IT DOESN'T WORK! I've tested it and it seems to run whatever the last-defined aaptOptions is, regardless of the build type. If I put release after debug, I get no example-creds in either build. If I put debug after release, I get example-creds in both.

我如何得到想要的东西?

How can I get what I'm looking for?

推荐答案

通常,应该可以配置

Generally it should be possible to configure AaptOptions alike any other configuration block. Try to run that script afterEvaluate (which is after those aaptOptions had been evaluated):

task afterEvaluate {
    doLast {
        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def pattern = "!node_modules:!jsunmin:!.idea:!jade:!css-scss:"
                if(variant.getBuildType().getName() == 'release') {
                    pattern = pattern + "!example-creds.js:"
                }
                aaptOptions.ignoreAssetsPattern = pattern
            }
        }
    }
}

这篇关于在Android中使用特定于构建的aaptOptions(用于排除特定的Assets文件/文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆