Android Studio 3.x 自动生成多渠道包

Android Studio 3.x  自动生成多渠道包
1)AndroidManifest清单文件中添加
    <meta-data
        android:name="MAT_CHANNEL"
        android:value="${MAT_CHANNEL_VALUE}"/>

完整如下:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="MAT_CHANNEL"
        android:value="${MAT_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>

2)module 的build.gradle中添加渠道及打包方法

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.demo.activityfragmentlifescyle"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        flavorDimensions "versionCode"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def outputFile = output.outputFile
                def fileName
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    if (variant.buildType.name.equals('release')) {//如果是release包
                        fileName = "cbx_release_v${defaultConfig.versionName}.apk"
                    } else if (variant.buildType.name.equals('debug')) {//如果是debug包
                        fileName = "cbx_debug_v${defaultConfig.versionName}.apk"
                    }
                    outputFileName = fileName
                }
            }
        }
    }

    productFlavors{
        baidu{}
        _91{}
        xiaomi{}
        huawei{}
    }

    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [MAT_CHANNEL_VALUE: name]
    }

}
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}



 
 

猜你喜欢

转载自www.cnblogs.com/zhou2016/p/12922258.html
今日推荐