您的位置:首页 > 编程语言 > Java开发

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

2015-11-13 19:30 399 查看

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

导语

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

一、使用Gradle进行打包

1、创建Android工程;

2、右键工程->Export;



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





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



#Fri Nov 13 17:58:11 CST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=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,为解压路径;



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打包成功;



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

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

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



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、执行打包,生成对应版本文件;



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









示例下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse android gradle