本文介绍了有条件地添加< activity>使用Gradle在AndroidManifest.xml上添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,只有用户不能直接访问的服务,接收器和活动(没有启动器活动).

I have an application that only have Services, Receivers and Activities that are not directly accessed by the use (there is no launcher activity).

但是现在我必须添加一个活动以用作启动器活动 BUT ,只有在应用程序在构建期间设置了某些特定变量时,该启动器活动才必须存在.

But now I have to add an activity to be used as launcher activity BUT this launcher activity must be present only when the app has some specific variables set during the BUILD.

所以基本上,当调用gradle build时,我设置了一个变量HAS_LAUNCHER=1,在我的 build.gradle 中,我有类似的东西:

So basically, when calling the gradle build I set a variable HAS_LAUNCHER=1 and in my build.gradle I have something like:

defaultConfig {
    ...

    def hasLauncher = System.getenv("HAS_LAUNCHER")
    if (hasLauncher != null && hasLauncher == "1") {
        // Something here to include the activity tag in the AndroidManifest.xml
    }
}

在我的AndroidManifest中,当该if条件为true时,我必须添加<activity>标签:

And in my AndroidManifest I have to add the <activity> tag when that if condition is true:

<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>


如何在不使用productFlavors的新维度的情况下完成此任务? (该应用程序已经具有3种口味和2种buildTypes的尺寸,所以我不想产生更多输出)


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)

推荐答案

可能为时已晚,但是经过许多天的搜索,我已经以这种方式完成了操作:

may be too late, but after many days searching for a solution I have done in this way :

gradle defaultConfig

gradle defaultConfig

defaultConfig {
    resValue "bool", "showActivity", "true"
...

在builTypes中

In the builTypes

 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中控制活动可见性.请注意,您还可以在代码中使用 ,只需创建一个布尔资源 showActivity ,它将被gradle值替换,您将能够读取它

This way you can control activity visibility from gradle. Note that you can also use in code , just create a bool resource showActivity , it will be replaced by gradle value and you'll be able to read it

context.getResources().getBoolean(R.bool.showActivity)

这篇关于有条件地添加&lt; activity&gt;使用Gradle在AndroidManifest.xml上添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:36