Android Gradle多版本打包

项目需求:一个项目相同的业务逻辑,要求打出不同版本的包,每个包的包名不相同。
经过资料查询,需要在Gradle中完成一些配置即可。
1、在src目录下添加不同版本对应的文件,以及在各个文件下面配置不同的资源文件。
这里写图片描述


2、在gradle中进行相关配置。
在productFlavors下配置不同需求的包,在src下配置的文件和productFlavors下的文件需要一一对应。

productFlavors{ 
    flavors_dev{//此处的名字和productFlavors下的文件名对应 
    applicationId "com.yongnuo.mcmsdev"//需要的不同包名 
    manifestPlaceholders = [                 JPUSH_APPKEY_VALUE:"bb7d0e0d04",BAIDU_APPKEY_VALUE:"firw23RSrwAuvO"]//此处为极光推送和百度    地图对应的key 
    buildConfigField "String", "URL", "\"http://192.168.……\""//不同项目需要对应的URL地址 

} }

3、AndroidManiFest.xml中配置极光和百度key的引用

<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY_VALUE}" />//极光推送 

<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="${BAIDU_APPKEY_VALUE}" />//百度地图

4、gradle配置的一些解释说明

apply plugin: 'com.android.application'

def releaseTime(){
    return new Date().format("YYYY-MM-dd",TimeZone.getTimeZone("GMT+8"));//生成当前时间
}
//基础配置
android {
    compileSdkVersion 24//SDK编译版本版本
    buildToolsVersion "24.0.3"//SDK编译工具版本
    defaultConfig {
        applicationId "com.yongnuo.mccms"//包名,和AndroidManiFest.xml中的包名配置相同
        minSdkVersion 15//最小支持版本
        targetSdkVersion 21//最大支持版本
        versionCode 1//版本号
        versionName "1.0.0"//版本名
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        aaptOptions.cruncherEnabled = false
        aaptOptions.useNewCruncher = false
        buildConfigField "String", "RELEASETIME", "\"${releaseTime()}\""//打包时生成的时间
    }
    //资源文件位置配置
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
            assets.srcDirs = ['assets']
        }
    }
    //编译类型
    buildTypes {
        release {//release版本
            minifyEnabled false//不混淆代码
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug{//debug版本
            minifyEnabled false//不混淆代码
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //不同需求的包配置
    productFlavors {
        flavors_dev {
            applicationId "com.yongnuo.mccmsdev"//不同需求的包名
            manifestPlaceholders = [JPUSH_APPKEY_VALUE: "bb7dasdfd6a6c404", BAIDU_APPKEY_VALUE: "0FpzYafLyb27ZAdYiHYVj"]//第三方秘钥配置
            buildConfigField "String", "URL", "\"http://192.168.……\""//URL地址
        }
        flavors_prod {
            applicationId "com.yongnuo.mccmsprod"
            manifestPlaceholders = [JPUSH_APPKEY_VALUE: "1e63a8753131b78c", BAIDU_APPKEY_VALUE: "RlrA400bi0KUi"]
            buildConfigField "String", "URL", "\"http://192.34.……\""
        }
    }
    //关闭一些编译时的监测,防止报错
    lintOptions {
        abortOnError false
        checkReleaseBuilds false
        disable 'MissingTranslation'
    }
}
//jar包的依赖配置
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'io.reactivex:rxjava:1.2.3'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.alibaba:fastjson:1.2.21'
    compile 'com.jakewharton:butterknife:8.4.0'
    compile 'com.jakewharton:butterknife-compiler:8.4.0'
    compile files('libs/ynksoap2.jar')
    compile files('libs/BaiduLBS_Android.jar')
    compile files('libs/tiandituapi2.2.2.jar')
    compile files('libs/universal-image-loader-1.9.3.jar')
    compile files('libs/hellocharts-library-1.5.8.jar')
    compile project(':MPChartLib')
    compile 'com.android.support:design:24.2.1'
}

猜你喜欢

转载自blog.csdn.net/mawei7510/article/details/88222238