使用maven构建android项目(转)


为什么引入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

参考文献


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM