我有一个应用程序,仅具有用户无法直接访问的服务、接收器和活动(没有启动器活动)。
但现在我必须添加一个活动用作启动器活动BUT仅当应用程序在构建期间设置了某些特定变量时,才必须存在此启动器活动。
所以基本上,当调用 gradle build 时,我设置了一个变量HAS_LAUNCHER=1
并在我的构建.gradle我有类似的东西:
defaultConfig {
...
def hasLauncher = System.getenv("HAS_LAUNCHER")
if (hasLauncher != null && hasLauncher == "1") {
// Something here to include the activity tag in the AndroidManifest.xml
}
}
在我的 AndroidManifest 中我必须添加<activity>
标记何时if
条件为真:
<activity
android:name=".LauncherActivity"
android:label="Launcher"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
How can I accomplish that without using a new dimension of productFlavors?
(the app already has a dimension with 3 flavors and 2 buildTypes, so I don't want to make even more outputs)
可能为时已晚,但经过很多天寻找解决方案后,我已经这样做了:
gradle默认配置
defaultConfig {
resValue "bool", "showActivity", "true"
...
在构建类型中
buildTypes {
release {
resValue "bool", "showActivity", "false"
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
在清单中
<activity
android:name="xxx.xx.xxx"
android:enabled="@bool/showActivity"
android:label="@string/myActivity"
android:icon="@android:drawable/ic_menu_preferences">
...
这样您就可以从 gradle 控制活动可见性。请注意,您还可以使用in code,只需创建一个 bool 资源显示活动,它将被 gradle 值替换,您将能够读取它
context.getResources().getBoolean(R.bool.showActivity)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)