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‘) } }
<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,各位用須要能夠試一下
gradle打包android (實現外部導入簽名文件、多渠道打包、導入ant腳本)