在 gradle 中设置一个可以在清单文件中使用的全局变量
Set a global variable in gradle that can use in manifest file
我想创建一个类似于applicationId 的全局变量。
它是在 build.gradle 中设置的值,将在清单中使用。可能吗?
要使用Manifest
中的字符串,直接在strings.xml
中制作即可。
像这样,
<string name="variable_name">value</string>
你可以设置它们,比如我是针对不同的产品口味设置的
productFlavors {
production {
applicationId = "com.myapp.app"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider5435651423234"
}
development {
applicationId = "com.myapp.development"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider2134564533421"
}
qa {
applicationId = "com.myapp.qa"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider29831237981287319"
}
}
然后像这样使用它
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="@string/authority"
android:exported="true" />
虽然 Marko 的回答似乎有效,但目前有一个更好的解决方案,不需要向字符串资源文件添加变量。
manifest merger accepts placeholders:
For custom placeholders replacements, use the following DSL to
configure the placeholders values :
android {
defaultConfig {
manifestPlaceholders = [ activityLabel:"defaultName"]
}
productFlavors {
free {
}
pro {
manifestPlaceholders = [ activityLabel:"proName" ]
}
}
will substitute the placeholder in the following declaration :
<activity android:name=".MainActivity" android:label="${activityLabel}" >
您还可以使用 groovy 函数操作这些字符串。
如果您只想在清单中使用 gradle 中设置的应用程序 ID,您可以简单地使用:
${applicationId}
例如:
<provider
android:authorities="${applicationId}.ShareFileProvider" ... >
...
</provider>
如果您希望自定义变量具有相同的行为,您可以使用 manifestPlaceholders,如下所示:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
}
在您的清单中:
<intent-filter ... >
<data android:scheme="http" android:host="${hostName}" ... />
...
</intent-filter>
有关详细信息,请参阅 https://developer.android.com/studio/build/manifest-build-variables.html。
我想创建一个类似于applicationId 的全局变量。 它是在 build.gradle 中设置的值,将在清单中使用。可能吗?
要使用Manifest
中的字符串,直接在strings.xml
中制作即可。
像这样,
<string name="variable_name">value</string>
你可以设置它们,比如我是针对不同的产品口味设置的
productFlavors {
production {
applicationId = "com.myapp.app"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider5435651423234"
}
development {
applicationId = "com.myapp.development"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider2134564533421"
}
qa {
applicationId = "com.myapp.qa"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider29831237981287319"
}
}
然后像这样使用它
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="@string/authority"
android:exported="true" />
虽然 Marko 的回答似乎有效,但目前有一个更好的解决方案,不需要向字符串资源文件添加变量。
manifest merger accepts placeholders:
For custom placeholders replacements, use the following DSL to configure the placeholders values :
android {
defaultConfig {
manifestPlaceholders = [ activityLabel:"defaultName"]
}
productFlavors {
free {
}
pro {
manifestPlaceholders = [ activityLabel:"proName" ]
}
}
will substitute the placeholder in the following declaration :
<activity android:name=".MainActivity" android:label="${activityLabel}" >
您还可以使用 groovy 函数操作这些字符串。
如果您只想在清单中使用 gradle 中设置的应用程序 ID,您可以简单地使用:
${applicationId}
例如:
<provider
android:authorities="${applicationId}.ShareFileProvider" ... >
...
</provider>
如果您希望自定义变量具有相同的行为,您可以使用 manifestPlaceholders,如下所示:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
}
在您的清单中:
<intent-filter ... >
<data android:scheme="http" android:host="${hostName}" ... />
...
</intent-filter>
有关详细信息,请参阅 https://developer.android.com/studio/build/manifest-build-variables.html。