[英]XML getting values from another XML file
I am trying to build an android application that uses the Google Map API and I want to put this project on GIT.我正在尝试构建一个使用 Google Map API 的 android 应用程序,我想将此项目放在 GIT 上。 So I have an API key and this information is inside AndroidManifest.xml like this (I have replaced my actual API key with
*key*
here所以我有一个 API 密钥,这个信息在 AndroidManifest.xml 中是这样的(我在这里用
*key*
替换了我的实际 API 密钥
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value= *key* />
I don't want this sensitive information pushed on git, but I can't gitignore this file.我不想在 git 上推送这些敏感信息,但我不能 gitignore 这个文件。 Is it possible to make another XML file that has the secret key and make the AndroidManfiest.xml get it from there (so that I can gitignore this one)?
是否可以制作另一个具有密钥的 XML 文件并使 AndroidManfiest.xml 从那里获取它(以便我可以忽略这个文件)?
AndroidManifest.xml has this bit of code AndroidManifest.xml 有这段代码
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"
android:launchMode="singleTop" />
And I noticed that the label property gets its value from another XML file that's located in res/values/strings.xml so:我注意到 label 属性从位于 res/values/strings.xml 中的另一个 XML 文件中获取其值,因此:
label
label
的值@
mean? @
是什么意思?res
folder?res
文件夹中?To avoid uploading your key:为避免上传您的密钥:
Create the key in a properties file and add the file in .gitignore在属性文件中创建密钥并将文件添加到 .gitignore
API_KEY=XXXXXXXXXXX
inside .gitignore
, add the path在
.gitignore
,添加路径
secretKeys.properties
Read the file in app/build.gradle
and create a string resource variable using buildConfigField
读取
app/build.gradle
的文件并使用buildConfigField
创建一个字符串资源变量
def apikeyPropertiesFile = rootProject.file("apikey.properties") def apikeyProperties = new Properties() apikeyProperties.load(new FileInputStream(apikeyPropertiesFile)) android { defaultConfig { buildConfigField("String", "API_KEY", apikeyProperties['API_KEY']) } }
Use the created variable!使用创建的变量!
Follow the blog for complete details.请关注博客以获取完整详细信息。
how does AndroidManifest.xml know where to find the value for the label
AndroidManifest.xml 如何知道在哪里可以找到标签的值
Android build generates an R.java
file for all the XML contants and maps all the occurrence of XML contents with the value from the R.Java
file. Android 构建为所有 XML 内容生成一个
R.java
文件,并将所有出现的 XML 内容映射到R.Java
文件中的值。
what does the @ mean?
@是什么意思?
This is a special symbol, used by the parser to parse specific string resources .这是一个特殊的符号, 解析器用来解析特定的字符串资源。 For example,
@+id/root
means, parse this as an id
resource string to replace value from R.java
.例如,
@+id/root
表示将其解析为id
资源字符串以替换来自R.java
值。
can I put an XML file (called Config.xml) anywhere?
我可以将 XML 文件(称为 Config.xml)放在任何地方吗? or does it have to be in the > res folder?
还是必须在 > res 文件夹中?
Yes, it has to be in a res
folder.是的,它必须在
res
文件夹中。
You should be able to put the secret string in its own XML file and add that file to your gitignore.您应该能够将秘密字符串放在其自己的 XML 文件中并将该文件添加到您的 gitignore。 You can then reference the string in
AndroidManifest.xml
.然后,您可以在
AndroidManifest.xml
引用该字符串。
Example:例子:
app/src/main/res/values/secrets.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="google_api_key">abcdefg-12345</string>
</resources>
app/src/main/AndroidManifest.xml
<manifest ...>
<application ...>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
...
</application>
</manifest>
To answer your questions: Any Android XML file (like AndroidManifest.xml, layout, and other files) interprets the value of an attribute as a raw string unless it starts with @
.回答您的问题:任何 Android XML 文件(如 AndroidManifest.xml、布局和其他文件)都将属性的值解释为原始字符串,除非它以
@
开头。 The @
prefix tells it to look up a resource. @
前缀告诉它查找资源。 @string
indicates a string resource, @layout
means a layout, @drawable
means a drawable, @id
means an ID, and etc. Each of these resources are typically defined in their own file, but not always. @string
表示字符串资源, @layout
手段布局, @drawable
手段绘制, @id
手段的ID,等这些资源,各自在自己的文件通常定义,但并非总是如此。 No matter what, the file is always within a sub-directory of res
.无论如何,该文件始终位于
res
的子目录中。 For your case, strings are defined in res/values/some_file.xml
.对于您的情况,字符串在
res/values/some_file.xml
中定义。 You can make any number of files within res/values
and the contents of all these files gets merged together during the AAPT compile step, so you can reference any of them by their defined name, regardless of the file name.您可以在
res/values
创建任意数量的文件,并且所有这些文件的内容在 AAPT 编译步骤期间合并在一起,因此您可以通过定义的名称引用它们中的任何一个,而不管文件名如何。
Check out https://developer.android.com/guide/topics/manifest/meta-data-element for more information on the <meta-data>
tag and https://developer.android.com/guide/topics/resources/string-resource for info on string resources.查看https://developer.android.com/guide/topics/manifest/meta-data-element有关
<meta-data>
标签和https://developer.android.com/guide/topics/resources 的更多信息/string-resource获取有关字符串资源的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.