Android-maven Configuring Multidex Packaging

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/goldenfish1919/article/details/42006649

After the project is big, inevitably there will be a limit of more than 65536 methods, fortunately, the new Android provides a multi-dex packaging to solve the problem. Let's look at how to use Android-maven-plugin to play multi-dex packages.

First look at the engineering structure:


is not a standard MAVEN project, but can be packaged by changing the configuration. The Code of the project is very simple, mainactivity has a button, click the button to jump to secondactivity,secondactivity referenced Fastjson did an object to string operation, and then pop up the toast prompt. There is demomultidexapplication inherited the multidexapplication. If your application has inherited another class, you can also rewrite Attachbasecontext () to implement it.

public class Demomultidexapplication extends Multidexapplication {/* If you have inherited another class, you can override the following method @overrideprotected void Attachbasecontext (Context base) {Super.attachbasecontext (base); Multidex.install (this);} */}
Focus on Pom:

<?xml version= "1.0" encoding= "Utf-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/maven-v4_0_0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Com.example.test</groupid> <artifactId>multidexdemo</artifactId> <version>1.0.0</        version> <packaging>apk</packaging> <name>multidexdemo</name> <properties> <sign.keystore>${project.basedir}/debug.keystore</sign.keystore> <sign.alias>androiddebugkey& lt;/sign.alias> <sign.storepass>android</sign.storepass> </properties> <dependencies&        Gt <dependency> <groupId>com.google.android</groupId> <artifactid>android</a        Rtifactid> <version>4.3.0</version>    <scope>provided</scope> </dependency> <dependency> <groupid>com .alibaba</groupid> <artifactId>fastjson</artifactId> <version>1.1.45</ver sion> </dependency> <dependency> <groupId>com.android.support</groupId> <art Ifactid>multidex</artifactid> <version>20</version></dependency> </dependencies > <build> <finalName>${project.artifactId}-${project.version}</finalName> <source Directory>src</sourcedirectory> <plugins> <!--compilation Plugin--<plugin><groupid >org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>< version>3.2</version><configuration><source>1.6</source><target>1.6</ Target><encoding>utf-8</encoding></configuration></plugin><!--Resource Plug-in--><plugin> <groupid>org.apache.maven.plugins</groupid > <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> < configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!--signature plugin--&G    T <plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-jarsigner-plugin </artifactid><version>1.2</version><executions><execution><id>signing</ Id><goals><goal>sign</goal><goal>verify</goal></goals><phase> Package</phase><inherited>true</inherited><configuration><removeexistingsignatures >true</removeexistingsignatures><archivedirectory/><includes><include>target/${ Project.artifactid}-${project.version}.apk</include></includes><keystore>${sign.keystore} </keystorE><alias>${sign.alias}</alias><storepass>${sign.storepass}</storepass><verbose >true</verbose></configuration></execution></executions></plugin><!-- Android-maven-plugin--> <plugin> <groupid>com.simpligility.maven.plugins</grou Pid> <artifactId>android-maven-plugin</artifactId> <version>4.0.0</v ersion> <configuration> <source>1.6</source> &lt ;target>1.6</target> <encoding>UTF-8</encoding> <!--settings Androi Dmanifest,assets,libs,res path, if the standard MAVEN project does not need to be set--<androidmanifestfile>${project.basedir}/and Roidmanifest.xml</androidmanifestfile> <assetsdirectory>${project.basedir}/assets</asset Sdirectory> &LT;NATIVELIBRARIESDIRectory>${project.basedir}/libs</nativelibrariesdirectory> <resourcedirectory>${project. Basedir}/res</resourcedirectory> <failonduplicatepackages>false</failonduplicatepackages > <sdk> <path>${env.                    Android_home}</path> <platform>19</platform> </sdk> <!--confused-<proguard><skip>false</skip><config>${project.ba sedir}/proguard.cfg</config><proguardjarpath>${project.basedir}/tools/proguard.jar</ proguardjarpath><jvmarguments><jvmargument>-xms256m</jvmargument><jvmargument>- Xmx512m</jvmargument></jvmarguments></proguard> <dex> &L              T;jvmarguments> <jvmArgument>-Xms2048m</jvmArgument>              <jvmArgument>-Xmx2048m</jvmArgument> </jvmArguments> <!--using multi-dex Package--<multi-dex>true</multi-dex> <minimal-main-dex& Gt;true</minimal-main-dex> <dex-arguments>--set-max-idx-number=65536</dex-arguments> <!                    -Class--> <main-dex-list>${project.basedir}/MainDexList.txt</main-dex-list> in main dex </dex> <!--Disable the default debug signature--<sign> < debug>false</debug> </sign> </configuration> <ext ensions>true</extensions> </plugin> </plugins> </build></project>


MainDexList.txt:

android/support/multidex/multidex.classandroid/support/multidex/multidex$v4.classandroid/support/multidex/ multidex$v14.classandroid/support/multidex/multidex$v19.classandroid/support/multidex/ multidexapplication.classandroid/support/multidex/multidexextractor.classandroid/support/multidex/ multidexextractor$1.classandroid/support/multidex/ziputil.classandroid/support/multidex/ziputil$ Centraldirectory.classcom/example/hello/demomultidexapplication.class
This lists the classes that are placed in the main Dex.


Proguard.cfg:

-dontpreverify-repackageclasses '-allowaccessmodification-optimizations!code/simplification/ Arithmetic-keepattributes *annotation*-keep public class * extends Android.app.activity-keep public class * extends Andro Id.app.application-keep public class * extends Android.app.service-keep public class * extends Android.content.broadcastreceiver-keep public class * extends Android.content.contentprovider-keep public class *    Extends Android.view.View {public <init> (android.content.Context);    Public <init> (Android.content.Context, Android.util.AttributeSet);    Public <init> (Android.content.Context, Android.util.AttributeSet, int.); public void set* (...);} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet);} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet, int);} -keepclassmembers class * Implements android.os.Parcelable {static android.os.ParcelablE$creator Creator;} -keepclassmembers class * *. r$* {public static <fields>;} -keep class com.example.hello.person{*;} -keep class android.support.multidex.*{*;} -dontwarn Com.alibaba.fastjson.**-keep class com.alibaba.fastjson.**{*;} -keep class Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter {*;} -keep class Com.alibaba.fastjson.support.spring.FastJsonJsonView {*;} -dontwarn Com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter-dontwarn Com.alibaba.fastjson.support.spring.FastJsonJsonView

The signature is signed by using the default KeyStore.


The MAVEN environment used:


That's all you have to do.

In addition, MAVEN's package is probably not starting up, you can use the ADB logcat to crawl the Logcat log.


Complete engineering in this: http://download.csdn.net/detail/goldenfish1919/8274385


Plugin for copying files:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-resources-plugin </artifactId> <version>2.5</version> <executions> <execution> <id >copy-resources</id> <phase>validate</phase> <goals> <g oal>copy-resources</goal> </goals> <configuration> <outputdir Ectory>${basedir}/res/drawable/</outputdirectory> <resources> &LT;RESOURC E> <directory>${basedir}/securityjpg/</directory> <filteri Ng>true</filtering> <includes> <include>*.jpg</i             nclude> </includes> </resource> </resources> </configuratioN> </execution> </executions></plugin> 

Multiple source directories:
 <build> <finalName>${project.artifactId}-${project.version}</finalName> <sourcedirecto ry>src</sourcedirectory> <resources> <resource> <targetpath>${ Basedir}/src/</targetpath> <directory>${basedir}/src1/</directory> <inc ludes> <include>**</include> </includes> <filteri ng>true</filtering> </resource> <resource> <targetpath>${b Asedir}/src/</targetpath> <directory>${basedir}/src2/</directory> <incl udes> <include>**</include> </includes> <filterin g>true</filtering> </resource> <resource> <targetpath>${ba Sedir}/src/</targetpaTh> <directory>${basedir}/src3/</directory> <includes> <include>**</include> </includes> &LT;FILTERING&GT;TRUE&LT;/FILTERING&G            T </resource> </resources> </build>


Reference:

Http://www.infoq.com/cn/news/2014/11/android-multidex

Https://github.com/simpligility/android-maven-plugin

Http://stackoverflow.com/questions/26530654/android-multidex-support-library-using-eclipse

Https://github.com/lsuski/maven-multidex-sample

Http://simpligility.github.io/android-maven-plugin/dex-mojo.html


Android-maven Configuring Multidex Packaging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.