多渠道替换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>

是不是简单的粗暴

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,046评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • 最近在项目中遇到需要实现 Apk 多渠道、定制化打包, Google 、百度查找了一些资料,成功实现了上述功能,在...
    看一季残花落幕阅读 7,317评论 1 8
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,036评论 6 342
  • 目前iOS开发无论是哪种设计模式基本上和Model都脱不开关系,那么怎么快速的写出一个Model呢。下面有几种方法...
    queuey阅读 6,752评论 0 3