為什么引入maven構建方式
做過java后台開發的人員應該都知道,maven使用解決依賴包管理問題的,同時優化測試,打包,部署等流程的.
在android里,
- maven可以管理你的依賴包
- 打包成apklib,管理自己的組件庫
- 動態配置你的發布渠道(此點非常方便)
- 簽名,打包,混淆一條龍服務.
開始使用maven
引入pom.xml
-
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 5 6 <modelVersion>4.0.0</modelVersion> 7 <groupId>com.test</groupId> 8 <artifactId>apkDeplorer</artifactId> 9 <version>1.0.0</version> 10 <packaging>apk</packaging> 11 <name>apkDeplorer</name> 12 13 <dependencies> 14 <dependency> 15 <groupId>com.google.android</groupId> 16 <artifactId>android</artifactId> 17 <version>4.1.1.4</version> 18 <scope>provided</scope> 19 </dependency> 20 <dependency> 21 <groupId>com.google.android</groupId> 22 <artifactId>support-v4</artifactId> 23 <version>r7</version> 24 </dependency> 25 </dependencies> 26 27 <properties> 28 <keystore.filename>apkDeplorer.keystore</keystore.filename> 29 <keystore.storepass>kison.chen@zaozao</keystore.storepass> 30 <keystore.keypass>kison.chen@zaozao</keystore.keypass> 31 <keystore.alias>kison-android-app</keystore.alias> 32 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 33 </properties> 34 35 <build> 36 <finalName>${project.artifactId}-${project.version}-${manifest.metadata.id}</finalName> 37 <sourceDirectory>src/main/java</sourceDirectory> 38 <resources> 39 <resource> 40 <directory>.</directory> 41 <filtering>true</filtering> 42 <targetPath>../filtered-resources</targetPath> 43 <includes> 44 <include>AndroidManifest.xml</include> 45 </includes> 46 </resource> 47 <resource> 48 <directory>src/main/resources</directory> 49 <filtering>true</filtering> 50 <includes> 51 <include>**/*</include> 52 </includes> 53 <excludes> 54 <exclude>**/env-*.properties</exclude> 55 </excludes> 56 </resource> 57 </resources> 58 <pluginManagement> 59 <plugins> 60 <plugin> 61 <groupId>com.jayway.maven.plugins.android.generation2</groupId> 62 <artifactId>android-maven-plugin</artifactId> 63 <version>3.6.0</version> 64 <extensions>true</extensions> 65 <executions> 66 <execution> 67 <id>run</id> 68 <goals> 69 <goal>deploy</goal> 70 <goal>run</goal> 71 </goals> 72 <phase>install</phase> 73 </execution> 74 </executions> 75 <configuration> 76 <proguardConfig>proguard-project.txt</proguardConfig> 77 <proguardSkip>${project.build.proguardSkip}</proguardSkip> 78 <manifestDebuggable>${manifest.debuggable}</manifestDebuggable> 79 <androidManifestFile>target/filtered-resources/AndroidManifest.xml 80 </androidManifestFile> 81 <release>${project.build.release}</release> 82 <run> 83 <debug>${project.build.debug}</debug> 84 </run> 85 <runDebug>${project.build.runDebug}</runDebug> 86 <sign> 87 <debug>${project.build.sign.debug}</debug> 88 </sign> 89 <undeployBeforeDeploy>true</undeployBeforeDeploy> 90 </configuration> 91 </plugin> 92 </plugins> 93 </pluginManagement> 94 <plugins> 95 <plugin> 96 <groupId>com.jayway.maven.plugins.android.generation2</groupId> 97 <artifactId>android-maven-plugin</artifactId> 98 <version>3.8.0</version> 99 <configuration> 100 <sdk> 101 <platform>15</platform> 102 </sdk> 103 </configuration> 104 </plugin> 105 <plugin> 106 <groupId>org.codehaus.mojo</groupId> 107 <artifactId>keytool-maven-plugin</artifactId> 108 <version>1.2</version> 109 <configuration> 110 <keystore>${keystore.filename}</keystore> 111 <storepass>${keystore.storepass}</storepass> 112 <keypass>${keystore.keypass}</keypass> 113 <alias>${keystore.alias}</alias> 114 <dname>CN=iKoding, OU=iKoding, O=iKoding, C=CN</dname> 115 <sigalg>SHA1withDSA</sigalg> 116 <validity>10000</validity> 117 <keyalg>DSA</keyalg> 118 <keysize>1024</keysize> 119 </configuration> 120 </plugin> 121 <plugin> 122 <groupId>org.apache.maven.plugins</groupId> 123 <artifactId>maven-compiler-plugin</artifactId> 124 <configuration> 125 <source>1.6</source> 126 <target>1.6</target> 127 <encoding>UTF8</encoding> 128 </configuration> 129 </plugin> 130 <plugin> 131 <groupId>org.apache.maven.plugins</groupId> 132 <artifactId>maven-resources-plugin</artifactId> 133 <version>2.6</version> 134 <executions> 135 <execution> 136 <phase>initialize</phase> 137 <goals> 138 <goal>resources</goal> 139 </goals> 140 </execution> 141 </executions> 142 </plugin> 143 </plugins> 144 </build> 145 146 <profiles> 147 <profile> 148 <id>debug</id> 149 <activation> 150 <activeByDefault>true</activeByDefault> 151 </activation> 152 <build> 153 <filters> 154 <filter>src/main/resources/env-debug.properties</filter> 155 </filters> 156 </build> 157 <properties> 158 <project.build.debug>true</project.build.debug> 159 <project.build.runDebug>false</project.build.runDebug> 160 <project.build.proguardSkip>true</project.build.proguardSkip> 161 <project.build.release>false</project.build.release> 162 <project.build.sign.debug>true</project.build.sign.debug> 163 <manifest.debuggable>true</manifest.debuggable> 164 </properties> 165 </profile> 166 <profile> 167 <id>release</id> 168 <properties> 169 <project.build.debug>false</project.build.debug> 170 <project.build.runDebug>false</project.build.runDebug> 171 <project.build.proguardSkip>false</project.build.proguardSkip> 172 <project.build.release>true</project.build.release> 173 <project.build.sign.debug>false</project.build.sign.debug> 174 <manifest.debuggable>false</manifest.debuggable> 175 </properties> 176 <build> 177 <filters> 178 <filter>src/main/resources/env-release.properties</filter> 179 </filters> 180 <plugins> 181 <plugin> 182 <groupId>org.apache.maven.plugins</groupId> 183 <artifactId>maven-jarsigner-plugin</artifactId> 184 <version>1.2</version> 185 <executions> 186 <execution> 187 <id>sign</id> 188 <goals> 189 <goal>sign</goal> 190 </goals> 191 <phase>package</phase> 192 <inherited>true</inherited> 193 <configuration> 194 <includes> 195 <include>${project.build.outputDirectory}/*.apk</include> 196 </includes> 197 <keystore>${keystore.filename}</keystore> 198 <storepass>${keystore.storepass}</storepass> 199 <keypass>${keystore.keypass}</keypass> 200 <alias>${keystore.alias}</alias> 201 </configuration> 202 </execution> 203 </executions> 204 </plugin> 205 </plugins> 206 </build> 207 </profile> 208 <!-- 渠道profiles --> 209 <profile> 210 <id>channel-test</id> 211 <activation> 212 <activeByDefault>true</activeByDefault> 213 </activation> 214 <properties> 215 <manifest.metadata.id>test</manifest.metadata.id> 216 <manifest.metadata.channel>test</manifest.metadata.channel> 217 </properties> 218 </profile> 219 <profile> 220 <id>channel-91</id> 221 <properties> 222 <manifest.metadata.id>91-market</manifest.metadata.id> 223 <manifest.metadata.channel>91 market</manifest.metadata.channel> 224 </properties> 225 </profile> 226 <profile> 227 <id>channel-yingyonghui</id> 228 <properties> 229 <manifest.metadata.id>yingyonghui-market</manifest.metadata.id> 230 <manifest.metadata.channel>yingyonghui market</manifest.metadata.channel> 231 </properties> 232 </profile> 233 <profile> 234 <id>channel-tongbutui</id> 235 <properties> 236 <manifest.metadata.id>tongbutui-market</manifest.metadata.id> 237 <manifest.metadata.channel>tongbutui market</manifest.metadata.channel> 238 </properties> 239 </profile> 240 <profile> 241 <id>channel-tengxun</id> 242 <properties> 243 <manifest.metadata.id>tengxun-market</manifest.metadata.id> 244 <manifest.metadata.channel>tengxun market</manifest.metadata.channel> 245 </properties> 246 </profile> 247 <profile> 248 <id>channel-anzhi</id> 249 <properties> 250 <manifest.metadata.id>anzhi-market</manifest.metadata.id> 251 <manifest.metadata.channel>anzhi market</manifest.metadata.channel> 252 </properties> 253 </profile> 254 <profile> 255 <id>channel-gfan</id> 256 <properties> 257 <manifest.metadata.id>gfan</manifest.metadata.id> 258 <manifest.metadata.channel>gfan</manifest.metadata.channel> 259 </properties> 260 </profile> 261 </profiles> 262 </project>
安裝本地依賴包
由於國內的第三方庫多未以maven形式打包,故我們要手動將jar包安裝到本地maven庫.(高德地圖為例)
mvn install:install-file -DgroupId=com.autonavi -DartifactId=libamapv3 -Dversion=v3 -Dfile=/Users/keepcleargas/Downloads/AMapSDKV2Demo/libs/armeabi/libamapv3.so -Dpackaging=so -DgeneratePom=true -Dclassifier=armeabi
mvn install:install-file -DgroupId=com.autonavi -DartifactId=location -Dversion=2.0.0 -Dfile=/Users/keepcleargas/Downloads/AMapSDKV2Demo/libs/MapApiLocation.jar -Dpackaging=jar -DgeneratePom=true
添加依賴包到pom.xml
1 <dependency> 2 <groupId>com.autonavi</groupId> 3 <artifactId>libamapv3</artifactId> 4 <version>v3</version> 5 <classifier>armeabi</classifier> 6 <scope>runtime</scope> 7 <type>so</type> 8 </dependency> 9 <dependency> 10 <groupId>com.autonavi</groupId> 11 <artifactId>map</artifactId> 12 <version>2.0.0</version> 13 </dependency> 14 <dependency> 15 <groupId>com.autonavi</groupId> 16 <artifactId>ApiLocation</artifactId> 17 <version>2.0.0</version> 18 </dependency> 19 <dependency> 20 <groupId>com.autonavi</groupId> 21 <artifactId>ApiSearch</artifactId> 22 <version>2.0.0</version> 23 </dependency>
android的maven版本構建
由於在maven central中 android版本只有4.1.1.4
我們需要一個工具來安裝新版的android sdk. Maven Android SDK Deployer.
根據Maven Android SDK Deployer的wiki 文案,mvn install -P 4.4
在pom.xml導入 4.4.2的安卓包.
<dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>4.4.2_r3</version> <scope>provided</scope> </dependency>
構建中可能出現的問題
- maven版本問題 使用過程中可能會出現版本問題,筆者這里用的是maven 3.1.1,
com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.2
- .9圖片問題
ERROR: 9-patch image Project/res/drawable- hdpi/input_03_mid.9.png malformed.
[INFO] Must have one-pixel frame that is either transparent or white.
[INFO] ERROR: Failure processing PNG image Project/res/drawable-hdpi/input_03_mid.9.png
將.9圖片標准化 即可
命令執行
mvn clean package
打包,但不部署。
mvn clean install
打包,部署並運行。
mvn clean package android:redeploy android:run
這個命令通常用於手機上已經安裝了要部署的應用,但簽名不同,所以我們打包的同時使用redeploy命令將現有應用刪除並重新部署,最后使用run命令運行應用。
mvn android:redeploy android:run
不打包,將已生成的包重新部署並運行。
mvn android:deploy android:run
部署並運行已生成的包,與redeploy不同的是,deploy不會刪除已有部署和應用數據。
mvn clean install -P release,channel-91
打包簽名,的渠道為channel-91的apk