zoukankan      html  css  js  c++  java
  • 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 {       //签名,这里的文件名称和password是错的,后面还会有从外部导入的语句
       		myConfig {
            //绝对路径和相对路径都能够
            	storeFile file("E:\keystore\mydemo.keystore")  //签名文件
            	storePassword "276021750"                       //password
            	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.keystore
    storePassword=276021750
    keyPassword=276021750
    keyAlias=mydemo.keystore

    详细demo能够在这下载

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


    这种优点我们能够看到。用ant专职改文件的信息。这里专门做多渠道的打包,能够分开,事实上要做到全自己主动还有两步,一个是从svn自己主动获取。这个网上有非常多的版本号能够学习,还有一个是用脚本改动build.gradle文件,动态变换签名(事实上也能够做批量改动)和动态添加这些功能的代码,能够用shell,能够用python,各位用须要能够试一下

  • 相关阅读:
    Kubernetes部署高可用MongoDB集群
    C博客作业00—我的第一篇博客
    小发现,关于windows窗口中的F10快捷键
    C#小程序——从百度摘取搜索结果。
    elasticsearch 基础语句
    html嵌套html解决办法(<object></object>)
    websocket 前后台代码实例
    css3轮播图实现代码
    validate使用案例
    SiteMap Editor for Microsoft Dynamics CRM 2011
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6938614.html
Copyright © 2011-2022 走看看