標籤:apk mave rcfile efi 自己的 net 知識 屬性 基礎
近期一直在做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.keystorestorePassword=276021750keyPassword=276021750keyAlias=mydemo.keystore
詳細demo能夠在這下載
http://download.csdn.net/detail/killer1989/8927225
這種優點我們能夠看到。用ant專職改檔案的資訊。這裡專門做多渠道的打包,能夠分開,事實上要做到全自己主動還有兩步,一個是從svn自己主動擷取。這個網上有非常多的版本號碼能夠學習,還有一個是用指令碼改動build.gradle檔案,動態變換籤名(事實上也能夠做批量改動)和動態添加這些功能的代碼,能夠用shell,能夠用python,各位用須要能夠試一下
gradle打包android (實現外部匯入簽名檔案、多渠道打包、匯入ant指令碼)