多渠道替换Laucher Activity的方法

在渠道打包中,常常有这种需要不同渠道使用不同的启动Activity的需求
这里有两种方案,其中第二种是工作中使用的最佳方案

第一种方案

先说明下出现的问题,如果在flavor中再建一个目录,配置Manifest.xml,设置为下面的话

 <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

测试发现,安装一个app会出现两个启动Activity在桌面(用户也是一下安装两个不同App哎)

但是我们只需要一个,且合并时候是根据Activity的name合并的,flavor和main定义同包同类 工具会报错,定义不同类合并后又出现了两个,而我试过 tools:replace又是各种不行,最后只能想到用此方法,虽然name里面配置占位符 工具显示红叉,但是不影响运行。

 <activity
            android:name="${XX}"
            android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"


    signingConfigs {
        debug {
            storeFile file("../syntc-debug.jks")
            storePassword 'syntcstudio'
            keyAlias 'ruulaitv'
            keyPassword 'ruulaitv'
        }
        release {
            storeFile file("../syntc-debug.jks")
            storePassword 'syntcstudio'
            keyAlias 'ruulaitv'
            keyPassword 'ruulaitv'
        }
    }




    defaultConfig {
        applicationId "com.example.ruulai.test"
        minSdkVersion 11
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"

        manifestPlaceholders = [
                UMENG_CHANNEL_VALUE: "abcd",
                XX                   : "com.example.ruulai.test.MainActivity"
        ]


    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }

        debug {
            signingConfig signingConfigs.debug
        }


    }

//    lintOptions {
//        checkReleaseBuilds false
//        // Or, if you prefer, you can continue to check for errors in release builds,
//        // but continue the build even when errors are found:
//        abortOnError false
//    }

//    productFlavors {
//
//        wandoujia {}
//        baidu {}
//        c360 {}
//        uc {}
//
//        productFlavors.all { flavor ->
//            flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
//        }
//
//    }
    productFlavors {

        wandoujia {
            applicationIdSuffix ".wandoujia"
            manifestPlaceholders = [
                    UMENG_CHANNEL_VALUE: "wandoujia",
                    XX                 : "com.example.ruulai.test.MainActivity"
            ]
        }

        baidu {
            applicationIdSuffix ".baidu"
            manifestPlaceholders = [
                    UMENG_CHANNEL_VALUE: "baidu",
                    XX                 : "com.ext.ruu.BaiduActivity"
            ]
        }

        uc {
            applicationIdSuffix ".uc"
            manifestPlaceholders = [
                    UMENG_CHANNEL_VALUE: "uc",
                    XX                 : "com.ext.ruu.BaiduActivity"
            ]


        }

    }


}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.packetzoom:pz-okhttp3-interceptor:3.2.7'
}

第二种方案(给个赞哦)

<activity
android:name=”.activity.MainActivity”
android:screenOrientation=”landscape”
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
tools:node=”merge”>
<intent-filter tools:node=”remove”>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
<category android:name=”android.intent.category.LEANBACK_LAUNCHER” />
</intent-filter>
</activity>

然后使用新的Activity作为 启动Activity(类名随意)

    <activity android:name=".NewActicity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

是不是简单的粗暴

    原文作者:编程之乐
    原文地址: https://www.jianshu.com/p/0ffad0b81d29
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞