Use Ant to pack Android apps

Source: Internet
Author: User

Plan to write a complete series of articles using ant packaged Android apps, three articles.    The first article details the process of using ant packaged Android applications, lists some of the customization issues and their solutions, the second introduces my understanding of ant packaging ideas and basic concepts and use, the last one describes the APK package generation process. Implementation of the Environment: operating system: Ubuntu 64bit 14.04 Android SDK Build tools:22.0.1 Android SDK tools:24.3.3 ant:1.9.4proguard:5.2.1jdk:1.7 a simple application of the package process here the package flow includes compilation, code obfuscation, apk signature. For simple Android apps, a few simple steps can be done: 1. Generate ant compilation required build.xml under tools in the SDK directory, call Android Update project build, and use the specific commands to view reference 1.    For example: Switch to your project Catalog Android update Project--name own project name-P. The point after the-p parameter is the build.xml that the current directory generates ant compilation required in the project directory, runs Ant debug directly, compiles and builds the debug version of the APK package in the bin directory. A few questions to note are: (1) The auto-generated build.xml generates several Android-related target: You can view the help instructions by entering Ant directly (2) encounteredbuild failed How to solve: you can see the build failed above the log to see where to go, and to see the clue on which line in the Build.xml problem, where the build.xml refers to under the Sdk/tools/ant, for example:
[AAPT] Invalid resource Directory name: Project Catalog/bin/res crunchbuild failedsdk/tools/ant/build.xml:649:the following error o ccurred while executing this line:build.xml:694:null returned:1
generally seeRes Crunch, should be your project in eclipse automatically compiled files and ant generated files conflict, as long as the automatic compilation of Eclipse, clean under the project to re-run ant. (3) You can watch Project.Properties and local.properties .2. Add code obfuscationThere are comments in the project's Project.Properties file explaining how to open code obfuscation#proguard. Config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txtYou can remove the # number andProguard-project.txt Increase the configuration you need for special handling
After you modify it, then execute ant release, which you will see in the output
-obfuscate:[mkdir] Created dir: Project catalog/bin/proguard[jar] Building jar: Project Catalog/bin/proguard/original.jar[proguard] Proguard, Version 5.2.1 ...

You can see the version of Proguard I used, but with ant debug there is no Proguard, because only the release version needs to be confused, for specific reasons subsequent articles will be described in detail.    The obfuscated mapping file is in the bin directory. Note: If Android:debuggable=true is configured in the Androidmanifest.xml of the project, then even ant release will not confuse code 3. Signing the APK signature first requires KeyStore,    The specific generation is not detailed here, simply say that the project is selected in Eclipse, right-click Android Tools, export Android application with build options The second is to create a new Ant.properties file in the project directory, which configures your signature information for example: path to Key.store=kestore file
key.store.password= Password
key.alias.password= Password
Key.alias= alias at this time to execute ant release, in the bin directory can be generated "project name-release.apk", you end up with the release version of the APK
In this case, the simple Android project of the ant compilation package completed, very convenient.
Ii. adding customizations 1. Ant compilation that joins the so library Android app will not compile the JNI project by default, it will only process the library files in the Libs. If you have a compiled so in your libs, the packaged apk seems to work. In a well-configured JNI project, the ndk-build of the NDK can be compiled as long as it is running. Therefore, as long as the Custom_rules.xml file is added to the Build.xml directory, the contents are as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><project name= "project name" default= "Help" ><target name= "-pre-compile" >    <echo >ndk build.............................</echo>    <exec executable= "${ndk.dir}/ Ndk-build ">    </exec></target></project>
Add a row to the local.properties in the same directory, specify your NDK directory ndk.dir=ndk path to execute ant release again to see if your so library has been generated

If your JNI project exists as a library project, the method is the same.    The main project relies on your library project, also generates Build.xml in the library project, and then follows the above method. 2. Call the Hidden API, join Android.jar Many projects are based on Android native code modification, or just want to invoke the system hidden part of the API, It will be used to compile the Android.jar, or Framework.jar and other names of the library files, and will be required to load the order before you choose the Android compiled version. In the Java build path->libraries of Eclipse, the order of loading is adjusted, so how does the ant compilation change? or using the Custom_rules.xml written earlier, the following:
<?xml version= "1.0" encoding= "UTF-8"? ><project name= "Hooktest" default= "Help" ><property name= " Project.target.android.jar "value=" ${sdk.dir}/platforms/${target}/android.jar "/><target name="-pre-compile ">    <echo >set Project target class path.............................</echo>    <path id=" Project.target.class.path ">    <pathelement location=" Framework.jar "/>    <pathelement location=" $ {Project.target.android.jar} "/>    </path>    <property name=" My.project.target.class.path "refid= "Project.target.class.path"/>    <echo message= "Project.target.class.path:${my.project.target.class.path } "/></target></project>
3. A multi-source directory in a project if more than one source directory is set up in a project, the less appropriate method is to first copy the other source directory to the default SRC, after the compilation is completed and then deleted. In fact, the way to see the file copy and delete, in the multi-source directory project also established Custom_rules.xml, the main content is as follows:
<target name= "-pre-build" >    <echo >copy file.............................</echo>    < Copy todir= "${source.absolute.dir}" >     <fileset dir= "Other source directory" >        <include name= "**/*.java"/>        <include name= "**/*.aidl"/>    </fileset>    </copy></target><target name= " Src-clean ">    <echo >delete file.............................</echo>    <delete dir=" ${ source.absolute.dir}/copy of the source directory "/></target><target name="-post-build "depends=" Src-clean "></ Target><target name= "-pre-clean" depends= "Src-clean" ></target>
The code above will also perform the source operation of the delete copy when executing ant clean, and copy the source code before compiling, including the Aidl file, then delete the copied source directory after the package is complete.
A project in the source code as far as possible in a directory it. Other issues are like resource references between library projects, and if using the default R file directly is not a problem, but if you add a subproject using build Java build Path->project in Eclipse, and you also use the R file to apply the resource, Then ant's packing will have to go on.
End: Here are just a few of the basic processes and possible problems of using Ant's packaged Android app, as well as the use and packaging of ant for both reference articles and subsequent supplemental articles.
Reference: 1.android command details http://developer.android.com/tools/help/android.html2.ant reference manual https://ant.apache.org/manual/ 3.ant Pack apk detailed procedure to read the Sdk/tools/ant/build.xml file

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Ant to pack Android apps

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.