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

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

 

1、      修改基本配置(包名、版本號等)java

配置module下的build.gradleandroid

添加productFlavors例如:android-studio

productFlavors {app

  fangwei{ide

    applicationId "com.f"gradle

    versionCode 1ui

    versionName "0.1.04.21"debug

    //resValue "string", "app_name", "方維"orm

  }

  shunguoshi{

    applicationId "com.x"

    versionCode 1

    versionName "0.1.04.21"

    //生成res資源文件值,還能夠經過添加文件夾的方式替換

    //resValue "string", "app_name", "順"

    //生成BuildConfig值

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

  }

}

 

2、      修改資源文件(接口地址、app名稱、launcher等)

自定義BuildConfig(接口地址)

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

 

自定義資源(app名稱、顏色等)

兩種方式

一、resValue

//resValue "string", "app_name", "順"

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

 二、爲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']

    }

}

 

3、運行Gradle生成apk

一、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

相關文章
相關標籤/搜索