简体   繁体   English

如何在我的Android项目中将来自/ res / values的xml文件添加到gitignore中?

[英]How can I add a xml file from /res/values to my gitignore in my Android project?

I added a separate xml file to my /res/values folder which contains a secret api key that my app uses. 我在/ res / values文件夹中添加了一个单独的xml文件,其中包含我的应用程序使用的api密钥。

api_key.xml api_key.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="api_key" type="string">my_key</item>
</resources>

Now I would like to exclude it from git. 现在,我想将其从git中排除。 How can I do that? 我怎样才能做到这一点?

I added this line to the .gitignore file in my project root 我将此行添加到项目根目录下的.gitignore文件中

/app/src/main/res/values/api_key.xml

but it does not work. 但它不起作用。 The file still appears on Github after pushing the project content. 推送项目内容后,该文件仍显示在Github上。

Git's ignore system only applies to files that aren't yet tracked. Git的忽略系统仅适用于尚未跟踪的文件。 Because you've already committed the file, ignoring it won't do anything. 因为您已经提交了文件,所以忽略它不会做任何事情。

  1. First, and most importantly, invalidate the API secret key. 首先,也是最重要的是,使API密钥无效。

    You've published it, and there is nothing you can do to make that key secure again. 您已经发布了它,您无法采取任何措施再次确保该密钥的安全。 Generate a new key to use going forward. 生成一个新密钥以供日后使用。 Do not skip this step. 不要跳过此步骤。 There are bots whose purpose in life is to look for leaked API keys on GitHub. 有些机器人的目的是在GitHub上查找泄漏的API密钥。

  2. Remove the file from Git, probably using git rm --cached app/src/main/res/values/api_key.xml from the root of your repository. 从存储库的根目录使用git rm --cached app/src/main/res/values/api_key.xml从Git删除文件。 This will remove the file from your local repository, but leave it in your local working copy. 这将从本地存储库中删除文件,但将其保留在本地工作副本中。 Because the file is no longer tracked, your ignore will now take effect. 由于不再跟踪文件,因此您的忽略现在生效。

    Update the file with your new API key from step 1. 从步骤1开始,使用新的API密钥更新文件。

  3. Optionally, add a placeholder file, eg app/src/main/res/values/api_key.xml.sample , to your repository. (可选)将占位符文件(例如app/src/main/res/values/api_key.xml.sample )添加到存储库中。 This file should not include the API key, but rather should be a skeleton of the file you need, eg 该文件不应包含API密钥,而应是所需文件的框架,例如

     <?xml version="1.0" encoding="utf-8"?> <resources> <item name="api_key" type="string">DUMMY_API_KEY</item> </resources> 

    Developers who work on the project should be encouraged to copy this file to to api_key.xml and add a proper API key. 应鼓励从事该项目的开发人员将此文件复制到api_key.xml并添加适当的API密钥。

  4. Push to GitHub. 推送至GitHub。 The existing file will be removed, and its history will remain in the repository. 现有文件将被删除,其历史记录将保留在存储库中。 That's okay, because you've invalidated the leaked API key. 没关系,因为您已使泄漏的API密钥无效。

You will have to delete it to remove it from github. 您必须删除它才能将其从github中删除。 The ignore just says "don't include any changes to things that match ..." 忽略只是说“不包括对匹配项的任何更改……”

This should do it: 应该这样做:

git rm --cache api_key.xml

This command says to remove the file from cache, but leave the file in place. 此命令说从高速缓存中删除文件,但将文件保留在原位。 I'm pretty sure this will tell Github to remove it from the current head. 我很确定这会告诉Github将其从当前头删除。 However, it will not remove the change history. 但是,它不会删除更改历史记录。

Back it up if there is any doubt. 如有任何疑问,请备份。 I'm not responsible ... (lol) 我不负责...(哈哈)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从手机的应用程序目录中将更新的xml文件加载到项目解决方案中 - How i can load the updated xml file in my project solution from app directory that is from my phone 我可以添加自己的.xml文件并将其用作Android中的资源吗 - Can I add my own .xml file and use it as a resource in Android 如何在intelij IDEA中将可绘制对象添加到我的res文件夹中? - How can I add drawables to my res folder in intelij IDEA? 如何在Android项目中添加“添加外部Jar” - How can i add “Add External Jar” to my project in android 在 Android 工作室中构建我的项目时,如何生成 lint-results.xml 文件? - How can I generate a lint-results.xml file when building my project in Android studio? 我如何使用按钮在项目中打开xml文件 - How can i use a button to open an xml file in my project 如何将整数从Java文件发送到XML文件夹? (Android Studio) - How can I send a integer from my Java file to my XML folder? (Android Studio) Android如何在res / values中添加自定义xml文件以及如何在系统中注册customvalues.xml - Android How to add a custom xml file in res/values and how to register the customvalues.xml with the system 如何从Android的Activity类中引用另一个项目的layout / xml文件? - How can I refer to layout/xml files of another project from my Activity class in Android? 如何从Droid项目中删除.xml文件? - How may I delete a .xml file from my Droid Project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM