外包采用Gradle生成多套app打包

目的:可修改app名称、icon、包名、接口地址及其它

 

一、      修改基本配置(包名、版本号等)

配置module下的build.gradle

添加productFlavors例如:

productFlavors {

  fangwei{

    applicationId "com.f"

    versionCode 1

    versionName "0.1.04.21"

    //resValue "string", "app_name", "方维"

  }

  shunguoshi{

    applicationId "com.x"

    versionCode 1

    versionName "0.1.04.21"

    //生成res资源文件值,还可以通过添加文件夹的方式替换

    //resValue "string", "app_name", "顺"

    //生成BuildConfig值

    buildConfigField 'String','DOMAIN_WWW','"s.com"'

  }

}

 

二、      修改资源文件(接口地址、app名称、launcher等)

自定义BuildConfig(接口地址)

buildConfigField 'String','DOMAIN_WWW','"s.com"'

 

自定义资源(app名称、颜色等)

两种方式

1、resValue

//resValue "string", "app_name", "顺"

resValue ‘boolean’,’isDebug’,’true’

 2、为flavor建立对应资源文件

在app->src文件夹下面建立对应flavor如

“fangwei”->res->values->strings.xml

                 drawable-hdpi->ic_launcher.png

                 drawable-xhdpi->ic_launcher.png

                 drawable-xxhdpi->ic_launcher.png

                 drawable-mdpi->ic_launcher.png

 

配置应用签名

signingConfigs {

    config {

        keyAlias 'alias'

        keyPassword 'pwd2'

        storeFile file('key.keystore')

        storePassword 'pwd1'

    }

}

 

AndroidManifest占位符(用于多渠道打包、包名等)

Manifest文件中的内容

<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>

 

build.gradle的内容

android {

    defaultConfig {

        manifestPlaceholders = [UMENG_CHANNEL_VALUE: 'dev']

    }

}

 

三、运行Gradle生成apk

1、chmod 777 gradlew

 

然后再执行进行编译

./gradlew  build

 

./gradlew tasks  //查看android gradle的所有任务

./gradlew assembleFangweiRelease   //生成apk,在build/outputs/apk文件夹下

 

 

附整份gradle配置

apply plugin: 'android'

 

dependencies {

    compile 'com.android.support:support-v4:19.0.0+'

    compile fileTree(dir: 'libs', include: '*.jar')

    compile project(':ZYLibrary')

    compile project(':library')

}

 

//def buildTime() {

//    def date = new Date()

//    def formattedDate = date.format('yyyyMMdd')

//    return formattedDate

//}

 

android {

//    buildTypes {

//        release {

//            applicationVariants.all { variant ->

//                variant.outputs.each { output ->

//                    if (output.outputFile != null && output.outputFile.name.endsWith('.apk')

//                            &&'release'.equals(variant.buildType.name)) {

//                        def apkFile = new File(

//                                output.outputFile.getParent(),

//                                "yizan_${variant.flavorName}_v${variant.versionName}_${buildTime()}.apk")

//                        output.outputFile = apkFile

//                    }

//                }

//            }

//        }

//    }

    signingConfigs {

        config {

            keyAlias 'o'

            keyPassword 'y

            storeFile file('o2o.keystore')

            storePassword 'y'

        }

    }

    compileSdkVersion 22

    buildToolsVersion "22.0.1"

    defaultConfig {

        applicationId "com.x"

        minSdkVersion 14

        targetSdkVersion 15

        versionCode 1

        versionName "0.1.04.21"

        // Enabling multidex support.

        multiDexEnabled true

//        resValue "string", "app_name", "方"

        signingConfig signingConfigs.config

        buildConfigField 'String','DOMAIN_WWW','"j.com"'

    }

    productFlavors {

        fangwei{

            applicationId "com.y"

            versionCode 1

            versionName "0.1.04.21"

//            resValue "string", "app_name", "维"

        }

        shunguoshi{

            applicationId "com.s"

            versionCode 1

            versionName "0.1.04.21"

//            resValue "string", "app_name", "顺"

            buildConfigField 'String','DOMAIN_WWW','"s.com"'

        }

 

    }

    sourceSets {

        main {

            manifest.srcFile 'AndroidManifest.xml'

            java.srcDirs = ['src']

            resources.srcDirs = ['src']

            aidl.srcDirs = ['src']

            renderscript.srcDirs = ['src']

            res.srcDirs = ['res']

            assets.srcDirs = ['assets']

            jniLibs.srcDirs = ['libs']

        }

 

        debug.setRoot('build-types/debug')

        release.setRoot('build-types/release')

    }

}

 

 

参考:

http://tech.meituan.com/mt-apk-adaptation.html

http://spencer-dev.com/blog/2015/android-xiang-mu-li-yong-android-studio-he-gradle-da-bao-duo-ban-ben-apk.html/

http://www.flysnow.org/2015/03/30/manage-your-android-project-with-gradle.html

posted @ 2015-09-01 17:32  Atlas's blog  阅读(3389)  评论(0编辑  收藏  举报