Eclipse下Gradle使用不同资源打包多版本应用

来源:互联网 发布:怎么在淘宝上刷销量 编辑:程序博客网 时间:2024/05/16 06:48

Eclipse下Gradle使用不同资源打包多版本应用

导语

最近项目遇到打包多版本安装包,并使用不同资源的的需求。现将配置过程记录如下,使用环境Eclipse。

一、使用Gradle进行打包


1、创建Android工程;

2、右键工程->Export;

Image

3、选择Gengrate Gradle build files,根据提示选择项目;

Image

Image

4、完成后会在工程目录生成如下目录及文件,打开gradle\wrapper\gradle-wrapper.properties文件;

Image

#Fri Nov 13 17:58:11 CST 2015distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https://services.gradle.org/distributions/gradle-1.12-all.zip

5、通过地址 https://services.gradle.org/distributions/gradle-1.12-all.zip 下载gradle包并解压,并在系统环境变量中配置GRADLE_USER_HOME,为解压路径;

Image

6、配置成功后,在工程目录的build.gradle文件中稍作修改就可以进行进行gradle打包了;

分别在appcompat_v7\build.gradle、TestGradle\build.gradle增加

lintOptions {    abortOnError false}

appcompat_v7\build.gradle:

apply plugin: 'com.android.library'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')}android {    compileSdkVersion 22    buildToolsVersion "23.0.0 rc2"    sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        // Move the tests to tests/java, tests/res, etc...        instrumentTest.setRoot('tests')        // Move the build types to build-types/<type>        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...        // This moves them out of them default location under src/<type>/... which would        // conflict with src/ being used by the main source set.        // Adding new build types or product flavors should be accompanied        // by a similar customization.        debug.setRoot('build-types/debug')        release.setRoot('build-types/release')    }    lintOptions {        abortOnError false    }}

TestGradle\build.gradle:

apply plugin: 'com.android.application'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')    compile project(':appcompat_v7')}android {    compileSdkVersion 22    buildToolsVersion "23.0.0 rc2"    sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        // Move the tests to tests/java, tests/res, etc...        instrumentTest.setRoot('tests')        // Move the build types to build-types/<type>        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...        // This moves them out of them default location under src/<type>/... which would        // conflict with src/ being used by the main source set.        // Adding new build types or product flavors should be accompanied        // by a similar customization.        debug.setRoot('build-types/debug')        release.setRoot('build-types/release')    }    lintOptions {        abortOnError false    }}

7、在工程目录执行gradle build进行打包,提示BUILD SUCCESSFUL打包成功;

Image

8、打包成功后,在TestGradle\build\outputs目录生成打包好的apk文件。

二、使用不同资源打包多版本

1、在TestGradle\product下创建test1、test2两个目录,并在两个目录中创建需要使用的资源目录结构,使用不同的主界面图标ic_launcher.png,字符串资源strings.xml;

Image

TestGradle\res\values\strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestGradle</string>    <string name="hello_world">default version</string>    <string name="action_settings">Settings</string></resources>

TestGradle\product\test1\res\values\strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestGradle1</string>    <string name="hello_world">test1</string>    <string name="action_settings">Settings1</string></resources>

TestGradle\product\test2\res\values\strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestGradle2</string>    <string name="hello_world">test2</string>    <string name="action_settings">Settings2</string></resources>

2、在TestGradle\build.gradle中增加多版本配置;

定义包名、versionCode、versionName:

productFlavors {       test1 {                 applicationId 'com.example.testgradle1'        versionCode 1        versionName "1.0.1"    }      test2 {          applicationId 'com.example.testgradle2'        versionCode 1        versionName "1.0.1"    }} 

定义使用资源路径:

test1.setRoot('product/test1')test2.setRoot('product/test2')

完整文件:

apply plugin: 'com.android.application'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')    compile project(':appcompat_v7')}android {    compileSdkVersion 22    buildToolsVersion "23.0.0 rc2"    productFlavors {           test1 {                     applicationId 'com.example.testgradle1'            versionCode 1            versionName "1.0.1"        }          test2 {              applicationId 'com.example.testgradle2'            versionCode 1            versionName "1.0.1"        }    }     sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        // Move the tests to tests/java, tests/res, etc...        instrumentTest.setRoot('tests')        // Move the build types to build-types/<type>        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...        // This moves them out of them default location under src/<type>/... which would        // conflict with src/ being used by the main source set.        // Adding new build types or product flavors should be accompanied        // by a similar customization.        debug.setRoot('build-types/debug')        release.setRoot('build-types/release')        test1.setRoot('product/test1')        test2.setRoot('product/test2')    }    lintOptions {        abortOnError false    }}

3、执行打包,生成对应版本文件;

Image

4、附上安装完成后效果图。

Image

ImageImageImage

示例下载

0 0
原创粉丝点击