是否可以从AndroidManifest.xml访问BuildConfig值?
在我的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
在我的gradle.properties文件中定义:
# Facebook identifier (app ID)
FACEBOOK_APP_ID=XXXXXXXXXX
要在我的应用中使用Facebook连接,我必须将此行添加到我的AndroidManifest.xml:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/applicationId"/>
我想用gradle中定义的BuildConfig字段@string/applicationId
替换FACEBOOK_APP_ID
,如下所示:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="FACEBOOK_APP_ID"/>
使用BuildConfig可以吗?如果没有,我怎么能实现这个目标?
答案 0 :(得分:164)
替换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
带
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
然后rebuild your project(Android Studio - &gt; Build - &gt;重建项目)。
这两个命令都会生成生成的值 - 在第一种情况下包含Java常量,在第二种情况下包含第二种Android资源 - 在项目构建期间,但第二种方法将生成可以使用的string
资源值@string/FACEBOOK_APP_ID
语法。这意味着它可以在清单和代码中使用。
答案 1 :(得分:75)
从AndroidManifest.xml访问Gradle Build Config值的另一种方法是通过这样的占位符:
<div class="header">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Sign up</a></li>
<li><a href="#">Login</a></li>
</ul>
</div>
<div class="shout">
<a href="#">Start</a>
</div>
然后在你的清单中:
android {
defaultConfig {
manifestPlaceholders = [ facebookAppId:"someId..."]
}
productFlavors {
flavor1 {
}
flavor2 {
manifestPlaceholders = [ facebookAppId:"anotherId..." ]
}
}
}
在此处查看更多详情:https://developer.android.com/studio/build/manifest-build-variables.html
(旧链接仅供参考:http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger)
答案 2 :(得分:29)
注意:当您使用resValue
时,字符串资源文件可能会意外覆盖该值(例如,对于另一种语言)
要获得可在清单和java代码中使用的真正常量值,请同时使用manifestPlaceholders
和buildConfigField
:例如
android {
defaultConfig {
def addConstant = {constantName, constantValue ->
manifestPlaceholders += [ (constantName):constantValue]
buildConfigField "String", "${constantName}", "\"${constantValue}\""
}
addConstant("FACEBOOK_APP_ID", "xxxxx")
}
在清单文件中访问:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${FACEBOOK_APP_ID}"/>
来自java的:
BuildConfig.FACEBOOK_APP_ID
如果常量值需要特定于buildType,则需要调整帮助器addConstant
(以使用groovy闭包语义),例如,
buildTypes {
def addConstantTo = {target, constantName, constantValue ->
target.manifestPlaceholders += [ (constantName):constantValue]
target.buildConfigField "String", "${constantName}", "\"${constantValue}\""
}
debug {
addConstantTo(owner,"FACEBOOK_APP_ID", "xxxxx-debug")
}
release {
addConstantTo(owner,"FACEBOOK_APP_ID", "xxxxx-release")
}
答案 3 :(得分:6)
访问清单中的build.gradle属性,如下例所示:
例如,您的build.gradle中有一个属性“applicationId”,并且您希望在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>
类似地,我们可以为其他常量创建字符串资源,并在代码文件中访问它们,简单如下:
context.getString(R.string.GCM_SENDER_ID);
答案 4 :(得分:2)
Another option: use a different string resource file to replace all Flavor-dependent values:
Step 1: Create a new folder in the "src" folder with the name of your flavor, im my case "stage"
Step 2: Create resource files for all files that are dependent on the flavor for example:
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:
Step 4: Use the string resources in your project and relax:
答案 5 :(得分:1)
@stkent很好,但忘了添加你之后需要重建你的项目
替换
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
与
resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
然后
Android Studio -> Build -> Rebuild Project
这将允许android生成可通过
访问的字符串资源R.string.FACEBOOK_APP_ID
答案 6 :(得分:0)
您可以使用如下所示的长值
buildConfigField'long','FLAVOR_LONG','11500L'