Is it possible to access a BuildConfig value from AndroidManifest.xml?
是否可以从AndroidManifest.xml访问BuildConfig值?
In my build.gradle file, I have:
在我的build.gradle文件中,我有:
defaultConfig {
applicationId "com.compagny.product"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
// Facebook app id
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
}
FACEBOOK_APP_ID
is defined in my gradle.properties files:
FACEBOOK_APP_ID在我的gradle.properties文件中定义:
# Facebook identifier (app ID)
FACEBOOK_APP_ID=XXXXXXXXXX
To use Facebook connect in my app, I must add this line to my AndroidManifest.xml:
要在我的应用中使用Facebook连接,我必须将此行添加到我的AndroidManifest.xml:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/applicationId"/>
I want to replace @string/applicationId
by the BuildConfig field FACEBOOK_APP_ID
defined in gradle, like this:
我想用gradle中定义的BuildConfig字段FACEBOOK_APP_ID替换@ string / applicationId,如下所示:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="FACEBOOK_APP_ID"/>
Is that possible using BuildConfig? If not, how can I achieve this?
使用BuildConfig可以吗?如果没有,我怎么能实现这个目标?
6 个解决方案
#1
126
Replace
更换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
with
同
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
then rebuild your project (Android Studio -> Build -> Rebuild Project).
然后重建你的项目(Android Studio - > Build - > Rebuild Project)。
The two commands both produce generated values - consisting of Java constants in the first case, and Android resources in the second - during project builds, but the second method will generate a string
resource value that can be accessed using the @string/FACEBOOK_APP_ID
syntax. This means it can be used in the manifest as well as in code.
这两个命令都会在项目构建期间生成生成的值 - 第一种情况下包含Java常量,第二种情况包含Android资源,但第二种方法将生成可使用@ string / FACEBOOK_APP_ID语法访问的字符串资源值。这意味着它可以在清单和代码中使用。
#2
64
Another way to access Gradle Build Config values from your AndroidManifest.xml is through placeholders like this:
从AndroidManifest.xml访问Gradle Build Config值的另一种方法是通过这样的占位符:
android {
defaultConfig {
manifestPlaceholders = [ facebookAppId:"someId..."]
}
productFlavors {
flavor1 {
}
flavor2 {
manifestPlaceholders = [ facebookAppId:"anotherId..." ]
}
}
}
and then in your manifest:
然后在你的清单中:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${facebookAppId}"/>
See more details here: https://developer.android.com/studio/build/manifest-build-variables.html
在此处查看更多详细信息:https://developer.android.com/studio/build/manifest-build-variables.html
(Old link just for reference: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger)
(旧链接仅供参考:http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger)
#3
11
note: when you use resValue
the value can accidentally be overridden by the strings resource file (e.g. for another language)
注意:当您使用resValue时,字符串资源文件可能会意外地覆盖该值(例如,对于另一种语言)
To get a true constant value that you can use in the manifest and in java-code, use both manifestPlaceholders
and buildConfigField
: e.g.
要获得可以在清单和java代码中使用的真正常量值,请使用manifestPlaceholders和buildConfigField:例如:
android {
defaultConfig {
def addConstant = {constantName, constantValue ->
manifestPlaceholders += [ (constantName):constantValue]
buildConfigField "String", "${constantName}", "\"${constantValue}\""
}
addConstant("FACEBOOK_APP_ID", "xxxxx")
}
access in the manifest file:
访问清单文件:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${FACEBOOK_APP_ID}"/>
from java:
来自java:
BuildConfig.FACEBOOK_APP_ID
#4
5
Access build.gradle properties in your manifest as in following example:
访问清单中的build.gradle属性,如下例所示:
For example you have a property "applicationId" in your build.gradle and you want to access that in your AndroidManifest:
例如,build.gradle中有一个属性“applicationId”,并且您希望在AndroidManifest中访问该属性:
Access "applicationId" in AndroidManifest:
在AndroidManifest中访问“applicationId”:
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
Similarly, we can create string resources for other constants and access them in code files as simple as:
同样,我们可以为其他常量创建字符串资源,并在代码文件中访问它们,如下所示:
context.getString(R.string.GCM_SENDER_ID);
#5
2
Another option: use a different string resource file to replace all Flavor-dependent values:
另一种选择:使用不同的字符串资源文件来替换所有与Flavor相关的值:
Step 1: Create a new folder in the "src" folder with the name of your flavor, im my case "stage"
第1步:在“src”文件夹中创建一个新文件夹,其中包含您的风味名称,我的情况是“舞台”
Step 2: Create resource files for all files that are dependent on the flavor for example:
步骤2:为所有依赖于flavor的文件创建资源文件,例如:
Step 3: I am also using different icons, so you see the mipmap folders as well. For this quetion, only the "strings.xml" is important. Now you can overwrite all important string resources. You only need to include the ones you want to override, all others will be used from the main "strings.xml", it will show up in Android Studio like this:
第3步:我也使用不同的图标,因此您也可以看到mipmap文件夹。对于此命令,只有“strings.xml”很重要。现在您可以覆盖所有重要的字符串资源。您只需要包含要覆盖的那些,所有其他的将使用主“strings.xml”,它将在Android Studio中显示如下:
Step 4: Use the string resources in your project and relax:
第4步:使用项目中的字符串资源并放松:
#6
0
@stkent is good but forgets to add that you need to rebuild your project afterwards
@stkent很好,但忘了添加你之后需要重建你的项目
Replace
更换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
with
同
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
then
然后
Android Studio -> Build -> Rebuild Project
This will allow android generate the string resource accessible via
这将允许android生成可访问的字符串资源
R.string.FACEBOOK_APP_ID
#1
126
Replace
更换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
with
同
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
then rebuild your project (Android Studio -> Build -> Rebuild Project).
然后重建你的项目(Android Studio - > Build - > Rebuild Project)。
The two commands both produce generated values - consisting of Java constants in the first case, and Android resources in the second - during project builds, but the second method will generate a string
resource value that can be accessed using the @string/FACEBOOK_APP_ID
syntax. This means it can be used in the manifest as well as in code.
这两个命令都会在项目构建期间生成生成的值 - 第一种情况下包含Java常量,第二种情况包含Android资源,但第二种方法将生成可使用@ string / FACEBOOK_APP_ID语法访问的字符串资源值。这意味着它可以在清单和代码中使用。
#2
64
Another way to access Gradle Build Config values from your AndroidManifest.xml is through placeholders like this:
从AndroidManifest.xml访问Gradle Build Config值的另一种方法是通过这样的占位符:
android {
defaultConfig {
manifestPlaceholders = [ facebookAppId:"someId..."]
}
productFlavors {
flavor1 {
}
flavor2 {
manifestPlaceholders = [ facebookAppId:"anotherId..." ]
}
}
}
and then in your manifest:
然后在你的清单中:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${facebookAppId}"/>
See more details here: https://developer.android.com/studio/build/manifest-build-variables.html
在此处查看更多详细信息:https://developer.android.com/studio/build/manifest-build-variables.html
(Old link just for reference: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger)
(旧链接仅供参考:http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger)
#3
11
note: when you use resValue
the value can accidentally be overridden by the strings resource file (e.g. for another language)
注意:当您使用resValue时,字符串资源文件可能会意外地覆盖该值(例如,对于另一种语言)
To get a true constant value that you can use in the manifest and in java-code, use both manifestPlaceholders
and buildConfigField
: e.g.
要获得可以在清单和java代码中使用的真正常量值,请使用manifestPlaceholders和buildConfigField:例如:
android {
defaultConfig {
def addConstant = {constantName, constantValue ->
manifestPlaceholders += [ (constantName):constantValue]
buildConfigField "String", "${constantName}", "\"${constantValue}\""
}
addConstant("FACEBOOK_APP_ID", "xxxxx")
}
access in the manifest file:
访问清单文件:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${FACEBOOK_APP_ID}"/>
from java:
来自java:
BuildConfig.FACEBOOK_APP_ID
#4
5
Access build.gradle properties in your manifest as in following example:
访问清单中的build.gradle属性,如下例所示:
For example you have a property "applicationId" in your build.gradle and you want to access that in your AndroidManifest:
例如,build.gradle中有一个属性“applicationId”,并且您希望在AndroidManifest中访问该属性:
Access "applicationId" in AndroidManifest:
在AndroidManifest中访问“applicationId”:
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
Similarly, we can create string resources for other constants and access them in code files as simple as:
同样,我们可以为其他常量创建字符串资源,并在代码文件中访问它们,如下所示:
context.getString(R.string.GCM_SENDER_ID);
#5
2
Another option: use a different string resource file to replace all Flavor-dependent values:
另一种选择:使用不同的字符串资源文件来替换所有与Flavor相关的值:
Step 1: Create a new folder in the "src" folder with the name of your flavor, im my case "stage"
第1步:在“src”文件夹中创建一个新文件夹,其中包含您的风味名称,我的情况是“舞台”
Step 2: Create resource files for all files that are dependent on the flavor for example:
步骤2:为所有依赖于flavor的文件创建资源文件,例如:
Step 3: I am also using different icons, so you see the mipmap folders as well. For this quetion, only the "strings.xml" is important. Now you can overwrite all important string resources. You only need to include the ones you want to override, all others will be used from the main "strings.xml", it will show up in Android Studio like this:
第3步:我也使用不同的图标,因此您也可以看到mipmap文件夹。对于此命令,只有“strings.xml”很重要。现在您可以覆盖所有重要的字符串资源。您只需要包含要覆盖的那些,所有其他的将使用主“strings.xml”,它将在Android Studio中显示如下:
Step 4: Use the string resources in your project and relax:
第4步:使用项目中的字符串资源并放松:
#6
0
@stkent is good but forgets to add that you need to rebuild your project afterwards
@stkent很好,但忘了添加你之后需要重建你的项目
Replace
更换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
with
同
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
then
然后
Android Studio -> Build -> Rebuild Project
This will allow android generate the string resource accessible via
这将允许android生成可访问的字符串资源
R.string.FACEBOOK_APP_ID