Android - 从风味中移除活动

时间:2015-09-25 01:53:45

标签: android android-activity android-gradle android-productflavors

如何从app风格中删除活动?这是一个简化的例子,我有一个应用程序,它有以下两种口味(付费和免费)。该应用程序很小,只有3个活动(MainActivity,ActivityOne和ActivityTwo)。付费应用程序不需要任何限制,因为它将使用完整的代码库。然而,免费应用程序要求用户可以访问MainActivity和ActivityTwo,而不是ActivityOne。如何在编译代码时执行“Manifest Merge”,以便免费版本中不存在ActivityOne?换句话说,应该如何创建 src / free / AndroidManifest.xml ,以便免费应用程序没有ActivityOne?

以下是该应用的 build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.calculator"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        paid {
            applicationId "com.example.paid"
            resValue "string", "app_name", "Paid Calculator"
            versionName "1.0-full"
        }
        free {
            applicationId "com.example.free"
            resValue "string", "app_name", "Free Calculator"
            versionName "1.0-free"
        }
    }
    sourceSets {
        paid {
            manifest.srcFile 'src/paid/AndroidManifest.xml'
        }
        free {
            manifest.srcFile 'src/free/AndroidManifest.xml'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
}

以下是应用的清单文件它位于 src / main / AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calculator">

    <application
        android:name="com.example.calculator.ui.activities.AppController"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ui.activities.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.activities.ActivityOne"
            android:label="@string/title_activity_one"
            android:parentActivityName=".ui.activities.MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ui.activities.MainActivity" />
        </activity>
        <activity
            android:name=".ui.activities.ActivityTwo"
            android:label="@string/title_activity_two"
            android:parentActivityName=".ui.activities.MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ui.activities.MainActivity" />
        </activity>
    </application>
</manifest>

2 个答案:

答案 0 :(得分:7)

您可以使用占位符

设置android:enabled属性

的AndroidManifest.xml:

<activity 
    android:name=".ui.activities.ActivityOne" 
    android:enabled="${isPaidVersion}"/>

build.config:

defaultConfig {
  ...
  manifestPlaceholders = [isPaidVersion: "false"]
}

productFlavors {
    paid {
        ... 
        manifestPlaceholders = [isPaidVersion:"true"]
    }
    free {
        ...
    }
}

答案 1 :(得分:3)

在您的免费风格中,您可以在AndroidManifest中指定:

<activity android:name=".ui.activities.ActivityOne" tools:node="remove"/>

标记工具:node =&#34;删除&#34; 会使合并移除android:name=".ui.activities.ActivityOne"的所有活动