美文网首页程序员
Android Studio中如何给项目配置多渠道打包

Android Studio中如何给项目配置多渠道打包

作者: 骑着金刚的猿 | 来源:发表于2019-04-04 15:07 被阅读0次

简介

在同一个项目中当有如下需求时:
1、不同的apk图标
2、不同的服务器域名
3、不同的包名
4、不同的app名称等
5、不同的环境配置

一、解决方法:

1、在当前app的build.gradle文件中android{}中使用productFlavors进行多渠道的相关配置:

productFlavors {
        //开发环境
        dev {
            manifestPlaceholders = [APP_NAME : "MyDemo_Dev"]
            buildConfigField "String", "BASE_URL", "http://xxxx.xx.com"
            applicationId "开发环境包名"
        }
 
        //测试环境
        uat {
            manifestPlaceholders = [APP_NAME : "MyDemo_UAT"]
            buildConfigField "String", "BASE_URL", "http://xxxx.xx.com"
            applicationId "测试环境包名"
        }
 
        //正式环境
        prod {
            manifestPlaceholders = [APP_NAME : "MyDemo"]
            buildConfigField "String", "BASE_URL", "http://xxxx.xx.com"
            applicationId "正式环境包名"
        }
 
    }

2、app的名称在清单文件中进行如下配置即可使用:


image.png

3、此时在Android studio的Build Variants中就可根据自己的需要选择不同的渠道进行打包了:


image.png
4、选择不同的渠道会自动生成相应的BuildConfig文件:
image.png

5、服务器地址的使用:

BuildConfig.BASE_URL

6、在此基础上如果不同环境下想使用不同的资源文件,可进行如下配置:


image.png

二、我的完整的配置,仅供参考

隐私信息都用xxx替换了
app目录下的build.gradle

apply plugin: 'com.android.application'
 
android {
    signingConfigs {
        release {
            keyAlias 'my'
            keyPassword 'xxxxx'
            storeFile file('../signing/my.jks')
            storePassword 'xxxxx'
        }
    }
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.gyf.watermark"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        flavorDimensions "versionCode"
    }
    buildTypes {
        release {
            //是否混淆
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            //是否移除无用资源
            zipAlignEnabled true
        }
        applicationVariants.all { variant ->
            variant.outputs.all {
                outputFileName = "水印app_${defaultConfig.versionName}.apk"
            }
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
    productFlavors {
        //开发环境
        dev {
            manifestPlaceholders = [APP_NAME          : "水印Dev",
                                    BAIDUMAP_KEY      : "xxxxxxxxxxxxx",//百度地图key
                                    JIGUANG_KEY       : "xxxxxxxxxxxxx",//极光推送key
                                    RONG_CLOUD_APP_KEY: "xxxxx",//融云key
                                    HUAWEI_KEY        : "xxxxx",//华为推送key
                                    GAODE_KEY         : "xxxxx"]
            buildConfigField "String", "BASE_URL", "\"http://xxx.xx.com/\""
            buildConfigField "boolean", "isProdDebug", "false"
            applicationId "com.gyf.watermark.dev"
        }
        //测试环境
        uat {
            manifestPlaceholders = [APP_NAME          : "水印UAT",
                                    BAIDUMAP_KEY      : "xxxxxxxxxxxxx",//百度地图key
                                    JIGUANG_KEY       : "xxxxxxxxxxxxx",//极光推送key
                                    RONG_CLOUD_APP_KEY: "xxxxx",//融云key
                                    HUAWEI_KEY        : "xxxxx",//华为推送key
                                    GAODE_KEY         : "xxxxx"]
            buildConfigField "String", "BASE_URL", "\"http://xxx.xx.com/\""
            buildConfigField "boolean", "isProdDebug", "false"
            applicationId "com.gyf.watermark.uat"
        }
 
        //正式环境
        prod {
            manifestPlaceholders = [APP_NAME          : "水印",
                                    BAIDUMAP_KEY      : "xxxxxxxxxxxxx",//百度地图key
                                    JIGUANG_KEY       : "xxxxxxxxxxxxx",//极光推送key
                                    RONG_CLOUD_APP_KEY: "xxxxx",//融云key
                                    HUAWEI_KEY        : "xxxxx",//华为推送key
                                    GAODE_KEY         : "xxxxx"]
            buildConfigField "String", "BASE_URL", "\"http://xxx.xx.com/\""
            buildConfigField "boolean", "isProdDebug", "false"
            applicationId "com.gyf.watermark"
        }
 
    }
}
 
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

三、注意:

不同环境中清单文件中的相关配置如app的名称、极光推送的key、百度或高德地图的key的配置名要在清单文件中进行相应的引用,如下所示:

        <!-- 百度地图begin -->
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="${BAIDUMAP_KEY}" />
 
        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" />
        <!-- 百度地图end -->

四、结束

好了,多渠道打包的相关配置到这里就结束了,希望会对大家有所帮助,有错误的地方还请大家指正,谢谢

相关文章

网友评论

    本文标题:Android Studio中如何给项目配置多渠道打包

    本文链接:https://www.haomeiwen.com/subject/ehnfiqtx.html