当前位置: 移动技术网 > IT编程>移动开发>Android > 【Android】Android多渠道打包--Gradle打包

【Android】Android多渠道打包--Gradle打包

2019年08月15日  | 移动技术网IT编程  | 我要评论

赤峰中学发生车祸,遵义梦网,gravatar

android多渠道打包--gradle打包

前言

由于app一般都会在多个应用市场上架,为了分析app在每个不同渠道的具体的数据,一般都会对不同渠道打包不同的app。多渠道打包有多种方式,这里只介绍利用gradle进行多渠道打包。

步骤

1、在androidmanifest.xml中增加配置

<meta-data
            android:name="app_channel"
            android:value="${app_channel_value}" />

其中,app_channel为配置的属性名。app_channel_value为在build.gradle中配置的变量。

2、在build.gradle中增加productflavors配置

productflavors {
        baidu {
            manifestplaceholders = [app_channel_value: "baidu"]
        }
        qq {
            manifestplaceholders = [app_channel_value: "qq"]
        }
    }

在这里,增加了两个渠道配置信息:baidu和qq。在manifestplaceholders的配置中,app_channel_value即为在androidmanifest用到的变量。

注意:gradle3.0以上版本,需要在defaultconfig节点中增加以下配置:

flavordimensions "default"

3、渠道名称获取

private string getmetadata(context context, string key) {
        try {
            packagemanager packagemanager = context.getpackagemanager();
            applicationinfo applicationinfo = packagemanager.getapplicationinfo(context
                    .getpackagename(), packagemanager.get_meta_data);
            return applicationinfo.metadata.getstring(key);
        } catch (exception e) {
            e.printstacktrace();
        }
        return "";
    }

4、修改应用名称

productflavors {
        baidu {
            manifestplaceholders = [app_name: "@string/app_name_baidu", app_channel_value: "baidu"]
        }
        qq {
            manifestplaceholders = [app_name: "@string/app_name_qq",app_channel_value: "qq"]
        }
    }

在manifestplaceholders中增加app_name的配置,在androidmanifest.xml中引用:

<application
        android:allowbackup="true"
        android:icon="${app_icon}"
        android:label="${app_name}"
        android:roundicon="${app_icon}"
        android:supportsrtl="true"
        android:theme="@style/apptheme">

        <meta-data
            android:name="app_channel"
            android:value="${app_channel_value}" />

        //省略其它内容
    </application>

5、修改应用图标

productflavors {
        baidu {
            manifestplaceholders = [app_name: "@string/app_name_baidu",app_icon:"@mipmap/icon_baidu", app_channel_value: "baidu"]
        }
        qq {
            manifestplaceholders = [app_name: "@string/app_name_qq", app_icon:"@mipmap/icon_qq",app_channel_value: "qq"]
        }
    }

在manifestplaceholders中增加app_icon的配置,在androidmanifest.xml中引用:

<application
        android:allowbackup="true"
        android:icon="${app_icon}"
        android:label="${app_name}"
        android:roundicon="${app_icon}"
        android:supportsrtl="true"
        android:theme="@style/apptheme">

        <meta-data
            android:name="app_channel"
            android:value="${app_channel_value}" />

        //省略其它内容
    </application>

6、修改应用包名

productflavors {
        baidu {
            applicationidsuffix ".baidu"
            manifestplaceholders = [app_name: "@string/app_name_baidu",app_icon:"@mipmap/icon_baidu", app_channel_value: "baidu"]
        }
        qq {
            applicationidsuffix ".qq"
            manifestplaceholders = [app_name: "@string/app_name_qq", app_icon:"@mipmap/icon_qq",app_channel_value: "qq"]
        }
    }

增加applicationidsuffix属性。

7、java中调用gradle中的变量

buildtypes {
        debug {
            minifyenabled false
            proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
            buildconfigfield "string","field_test","\"field_test\""
        }
        release {
            minifyenabled false
            proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
            buildconfigfield "string","field_test","\"field_test\""
        }
    }

增加buildconfigfield。同步后,通过buildconfig.field_test能获取到新增的变量值。

8、完整的gradle

apply plugin: 'com.android.application'

android {
    compilesdkversion 28
    defaultconfig {
        applicationid "com.wangyz.multichannel"
        minsdkversion 21
        targetsdkversion 28
        versioncode 1
        versionname "1.0"
        testinstrumentationrunner "android.support.test.runner.androidjunitrunner"
        flavordimensions "default"
    }
    buildtypes {
        debug {
            minifyenabled false
            proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
            buildconfigfield "string","field_test","\"field_test\""
        }
        release {
            minifyenabled false
            proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
            buildconfigfield "string","field_test","\"field_test\""
        }
    }
    productflavors {
        baidu {
            applicationidsuffix ".baidu"
            manifestplaceholders = [app_name: "@string/app_name_baidu",app_icon:"@mipmap/icon_baidu", app_channel_value: "baidu"]
        }
        qq {
            applicationidsuffix ".qq"
            manifestplaceholders = [app_name: "@string/app_name_qq", app_icon:"@mipmap/icon_qq",app_channel_value: "qq"]
        }
    }
}

dependencies {
    implementation filetree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.+'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testimplementation 'junit:junit:4.12'
    androidtestimplementation 'com.android.support.test:runner:1.0.1'
    androidtestimplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

9、完整的androidmanifest.xml

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

    <application
        android:allowbackup="true"
        android:icon="${app_icon}"
        android:label="${app_name}"
        android:roundicon="${app_icon}"
        android:supportsrtl="true"
        android:theme="@style/apptheme">

        <meta-data
            android:name="app_channel"
            android:value="${app_channel_value}" />

        <activity android:name=".mainactivity">
            <intent-filter>
                <action android:name="android.intent.action.main" />

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

</manifest>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网