当前位置: 代码迷 >> Android >> gradle封装android (实现外部导入签名文件、多渠道打包、导入ant脚本)
  详细解决方案

gradle封装android (实现外部导入签名文件、多渠道打包、导入ant脚本)

热度:44   发布时间:2016-04-27 23:50:02.0
gradle打包android (实现外部导入签名文件、多渠道打包、导入ant脚本)

       最近一直在做android自动打包,之前已经完成了用纯命令行的形式打包、原生态ant脚本打包,和基于android的SDK的打包,并且实现了多渠道打包,后来同事推荐了gradle,网上的资料说gradle各种好,自己也感兴趣是实现一下,其实一般来说由于android对eclipse的支持减弱,大部分的人都是用gradle与android studio融合,这样面的例子也会比较多,但笔者所在的项目还是在eclipse的比较多,由于开发人员在移植过程中发现报错比较多所以一直没有完全移植(好吧,其实早晚会移植),所以笔者是用eclipse的IDE做的实验,下面先贴几个基础知识

     首先是新建一个android项目,然后用自带的IDE生成gradle文件,具体参考http://blog.csdn.net/x605940745/article/details/41242687,有兴趣的同志们可以采取纯手写的方式,这种方式可以不依赖IDE,所以会比较好

  这个是原始的build.gradle

buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:0.8.+'    }}apply plugin: 'android'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')}android {    compileSdkVersion 19    buildToolsVersion "19.0.3"    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')    }}

下面我先附上自己的build.gradle

buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:0.12.+'    }}apply plugin: 'android'ant.importBuild 'build.xml'  //这里导入了ant的脚本dependencies {    compile fileTree(dir: 'libs', include: '*.jar')}android {    compileSdkVersion 19    buildToolsVersion "20.0.0"             signingConfigs {       //签名,这里的文件名和密码是错的,后面还会有从外部导入的语句   		myConfig {        //绝对路径和相对路径都可以        	storeFile file("E:\\keystore\\mydemo.keystore")  //签名文件        	storePassword "276021750"                       //密码        	keyAlias "mydemo.keystore"                              		keyPassword "111"  	 	} 	 }             buildTypes{      release {           //这里就是用来生成apk的步骤,具体看备注      //1.添加签名      signingConfig  signingConfigs.myConfig      //2.runProguard 执行混淆代码      runProguard true      //混淆规则文件       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'      }	  	 }              productFlavors {      //这里是多渠道的地方,AndroidManifest.xml文件里面要有标示,下面会贴文件        yingyongbao {              manifestPlaceholders = [ CHANNEL_NAME:"YINGYONGBAO"]          }          umeng {              manifestPlaceholders = [ CHANNEL_NAME:"UMENG" ]          }          wandoujia {              manifestPlaceholders = [ CHANNEL_NAME:"WANDOUJIA" ]  	        }      }          allprojects {       //在这里是外部导入文件,然后更改本身文件的属性,这里只改了签名,还可以改其他的    afterEvaluate { project ->        def propsFile = rootProject.file('E:\\keystore\\keystore.properties')        def configName = 'myConfig'        if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {            def props = new Properties()            props.load(new FileInputStream(propsFile))            android.signingConfigs[configName].storeFile = file(props['storeFile'])            android.signingConfigs[configName].storePassword = props['storePassword']            android.signingConfigs[configName].keyAlias = props['keyAlias']            android.signingConfigs[configName].keyPassword = props['keyPassword']        }    }}            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')    }}
这里的具体功能看备注就可以了,暴力的贴上去就没问题,下面要贴几个文件,一个是AndroidManifest.xml文件,里面就是加了一行

 <meta-data            android:name="UMENG_CHANNEL"            android:value="${CHANNEL_NAME}" />
build.xml添加了一个target,用cmd命令写gradle deploy就可以执行里面的内容了
	<target name="deploy">       <!-- 	<replaceregexp flags="g" byline="true">           	 <regexp pattern="public static final Host host = Host.Development;" />           	 substitution expression 中是替换的值,替换的值都定义在相对应的配置文件中          	 <substitution expression="public static final Host host = Host.Test;" />           	fileset 属性中的 dir 用来指定被替换文件所在的目录            	includes 用来指定要替换哪个文件。            	<fileset dir="./src/net/xtion/crm/base" includes="CrmAppContext.java" />       	</replaceregexp> -->     	<replace encoding="utf-8" dir="./src/com/example/learn723">			<include name="MainActivity.java" />			<replacefilter token="public static final String host = "Host.Test";" value="public static final String host = "Host.abc";" />		</replace>	</target>  
还有一个签名文件
storeFile=E:\\keystore\\mydemo.keystorestorePassword=276021750keyPassword=276021750keyAlias=mydemo.keystore

具体demo可以在这下载

http://download.csdn.net/detail/killer1989/8927225


这样的好处我们可以看到,用ant专职改文件的信息,这里专门做多渠道的打包,可以分开,其实要做到全自动还有两步,一个是从svn自动获取,这个网上有很多的版本可以学习,另一个是用脚本修改build.gradle文件,动态变换签名(其实也可以做批量修改)和动态增加这些功能的代码,可以用shell,可以用python,各位用需要可以试一下

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案