Ant Build.xml Packaging should be aware of three-party jar problems and the wording of the confusion R

Source: Internet
Author: User

We're still going to take a look at the previous words: First we get a clear idea of which steps Android will go through in the process of packaging the APK:

The specific process for Android compilation is as follows:


1) Ndk-build compile native code to generate so file


2) AAPT command generates R.JAVA based on res resource file


3) Aidl command parsing. aidl file generation corresponding Java file


4) Javac command to compile Java file as class file


5) DX command to package the compiled class file as a Dex file


6) AAPT command generates RESOURCES.AP_ resource index file according to RES resource file


7) Apkbuilder RESOURCES.AP_ and Dex into an unsigned apk file


8) Jarsinger Signature apk file


9) Zipalign tool optimizer apk read speed

Here I would like to focus on the Javac this phase and AAPT packaging into res file confusion problem.

Here, let's see if our project contains a third-party jar that should be written, we first need to copy our jar package to our libs and then modify the script that compiles here. If this external project has its own R file and res resources, we need to pay attention to these problems when we write the script.

1: When compiling the R file, remember to package the R file for this external dependent project together.

2: When compiling class file, remember to join Lib dependency

3: Remember to add the res of the external file when you package the res file

4: If there is confusion in the project remember not to confuse the external dependent file R file, otherwise cannot find the external R

Here I would like to give an example, I am here for example I use, Pullrefreshlistview this external dependency project, need to pay attention to these steps, I put out the script. First look at the script that compiles r files.

<!-- Generate a R.java file for the project resource--><target name= "GEN" depends= "init" ><echo> generate R.java from a resource file ... </echo><exec Executable= "${aapt}" failonerror= "true" ><arg value= "package"/><span style= "color: #ff0000;" ><arg value= "--auto-add-overlay" ></arg></span><arg value= "-M"/><arg value= "-J"/> <arg value= "${gen}"/><arg value= "-M"/><arg value= "Androidmanifest.xml"/><arg value= "-S"/> <arg value= "res"/><span style= "color: #ff0000;" ><arg value= "-S"/><arg value= "C:\Users\edsheng\Desktop\Android-PullToRefresh-master\library\res"/ ><arg value= "--extra-packages"/> <arg value= "Com.handmark.pulltorefresh.library"/></span> <arg value= "-i"/><arg value= "${androidjar}"/></exec><echo>r.java file generated successfully </echo></ Target> 
See the red part of me? These are the external dependencies of the project when the need to add the script is to take our externally dependent res file to generate R, file.

Then need to pay attention to the second part, compile the class file to remember to add the jar dependencies, we want to use the jar first copied into the Lib, because Pullrefreshlistview this project is a Lib project, We directly copy Lib to our main project's Libs file directory below and then look at the script that Javac compiled class file.

<target name= "Compile" depends= "Aidl" ><echo> start compiling. class file ... </echo><javac fork= "true" Executable= "${javac}" encoding= "${encoding}" debug= "true" extdirs= "" source= "1.5" target= "1.5" destdir= "${classes}"  bootclasspath= "${androidjar}" >            <src path= "${src}"/> <src            path= "${gen}"/>      < Span style= "color: #ff0000;" >      <classpath>                <fileset dir= "${root}/libs" >                    <include name= "*.jar"/>                </ fileset>                </classpath></span>        </javac><echo>.class file compilation complete </echo></ Target>

also to understand this compilation process to find all the jars below the libs, and then we look at the confusion of the code to pay attention to things, one is the Lib file is confused when Lib's Resources R files do not confuse, here I also posted their own confusion of the script.
<target name= "obfuscate" depends= "compile" > <echo> confuses Packaging results ...</echo> <java jar= "${pro Guard_home}/lib/proguard.jar "fork=" true "failonerror=" true "> <jvmarg value="-dmaximum.inlined.code.lengt            H=32 "/> <arg value="-injars ${classes} "/> <arg value="-outjars Obfuscated.jar "/> <arg value= "-libraryjars ${androidjar}"/> <span style= "color: #ff0000;" > <arg value= "-libraryjars C:\Users\edsheng\Desktop\wecheckscore\libs\library.jar"/> <!--if used to External library, please specify--<arg value= "@proguard. cfg"/> <arg value= "-dump ${out}/dump.txt"/><arg valu E= "-printusage ${out}/usage.txt"/><arg value= "-printmapping ${out}/mapping.txt"/><arg value= "- Printseeds ${out}/seeds.txt "/></span> </java> <delete dir=" ${classes} "/> <mkdi R dir= "${classes}"/> <unzip src= "Obfuscated.jar" dest= "${classes} "/> <delete file=" Obfuscated.jar "/> <echo> code obfuscation end </echo> </target> 
the place to note is also my red place, specify the jar of the external confusion, there is a proguard.cfg this file, is the file we configured we still open this file to see, how to configure it?

#-optimizationpasses 7#-optimizations!code/simplification/arithmetic,!field/*,!class/merging/*- Dontoptimize-verbose-dontskipnonpubliclibraryclasses-dontskipnonpubliclibraryclassmembers #- Overloadaggressively<span style= "color: #ff0000;" >-keep public class com.handmark.pulltorefresh.library.r*{*;} Below the </span>#------------------is the exclusion that comes with the Android platform, do not move-----------------keep public class * extends Android.ap P.activity{public <fields>;p ublic <methods>;} -keep public class * extends Android.app.application{public <fields>;p ublic <methods>;} -keep public class * extends Android.app.service-keep public class * extends Android.content.broadcastreceiver-keep Publi C Class * extends Android.content.contentprovider-keep public class * extends Android.app.backup.backupagenthelper-keep    public class * Extends android.preference.preference-keepclassmembers enum * {public static **[] values (); public static * * VALUEOF (java.lang.String);} -keepclasseswithmemBers class * {public <init> (Android.content.Context, android.util.AttributeSet);} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet, int);} -keepattributes *annotation*# changed to this, because it is found that some SDKs are not called by the Java code display, so the native method called in the file will be confused-keepclasseswithmembers class * { Native <methods>;} -keep class * Implements android.os.Parcelable {public static final Android.os.parcelable$creator *;} #------------------Below is a generic exclusion item----------------# method name contains "JNI" character, is determined to be Java Native interface method, automatically exclude # method name contains "Jri" character, recognized The Java Reflection interface method, automatically excludes-keepclasseswithmembers class * {... *jni* (...);} -keepclasseswithmembernames class * {... *jri* (...);} -keep class **jni* {*;}
need to note is also the red part, prompted not to confuse Lib R file, finally is our Res file resource packaging, and then we also see how our script should be written.
Resource files for!--packaged projects--><target name= "Package_res_with_assets" ><echo> packaging resources and asset files ... </echo><exec Executable= "${aapt}" failonerror= "true" ><arg value= "package"/><span style= "color: #ff0000;" > <arg value= "--auto-add-overlay" ></arg></span><arg value= "-F"/><arg value= "-M"/> <arg value= "Androidmanifest.xml"/><arg value= "-S"/><arg value= "res"/><span style= "color:# ff0000; " ><arg value= "-S"/><arg value= "C:\Users\edsheng\Desktop\Android-PullToRefresh-master\library\res"/ > <arg value= "--extra-packages"/> <arg value= "Com.handmark.pulltorefresh.library"/></span> <arg value= "-A"/><arg value= "assets"/><arg value= "-i"/><arg value= "${androidjar}"/><arg Value= "-F"/><arg value= "${OUT}/${FILE_NAME}.AP_"/></exec><echo> packaging resources and asset file completion </echo> </target>
or the Red Place is also designated external resources and LIB such external engineering resources are also packaged in. This can be done either externally dependent or multiple. Then everyone will be happy to buld.

Ant Build.xml Packaging should be aware of three-party jar problems and the wording of the confusion R

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.