[Project Practice-advanced] what can ant + groovy be used in Android projects?

Source: Internet
Author: User
Tags groovy script
Raise questions:

Use ant + groovy to compile and generate APK packages in different environments (for example, prod, UAT, QA, Dev)

Problem description:The project package is "com. vmn. app. CRM ", we need to generate the package as" com. vmn. app. CRM. UAT "," COM. vmn. app. CRM. QA "," com. vmn. app. CRM. dev "APK, so that we can install the APK in four environments: prod, UAT, QA, and Dev on the same test machine.

Problem Analysis:To implement the problem above, we need to dynamically modify androidmanifest before compiling. the Package Value in XML is changed, so the path of the r generated after compilation is changed, so we need to modify it dynamically *. the package path of R in the Java file. At the same time, in order to distinguish the APK of different environments, we also need to dynamically modify Res/values/strings. app_name value in XML.

Note:In fact, we only dynamically modified androidmanifest. the Package Value in XML, but in fact the package in the project is not changed, so in androidmanifest. in the XML file, when we specify application, activity, provider, service, etc., the relative path (relative to androidmanifest. the Package Value in XML), and the absolute path must be used.

Use ant + groovy to determine the application log display level and debug level based on the project configuration file information during compilation.

Problem description:Item, we may introduce config. XML file, which contains project-related configuration information, such as the Log Level and running environment ENV (Prod/UAT/QA/Dev) of the project) supported features and other information. during compilation, we determine the application log display level based on the log level information and env to determine whether the application can debug.

Problem Analysis:You can customize a logger class. The log level in config. XML is used to control the logger display level. Based on ENV, We can dynamically modify the Android: debuggable attribute value of <Application> In androidmanifest. xml before compilation.

Library is dynamically introduced according to different environments during compilation using ant + groovy

Problem description:The chart we use in the project uses Adobe AIR technology. Because Adobe AIR runs in an external running environment, that is, when running the chart of our project, the device must have an Adobe AIR running environment.

Problem Analysis:For example, we provide two solutions: 1. Package the running environment of Adobe AIR in our project; 2. Do not package the running environment of Adobe AIR in our project, prompt users to download and install the Adobe AIR runtime environment. There are many reasons for the proposal of the second solution. For example, after the Adobe AIR package is in our project, the project size becomes very large, and the copyright issue may be involved.

Project Practice:

Based on the question above, I will illustrate how ant + groovy is implemented from the code perspective. This article does not cover ant and groovy details. If you do not understand anything, you can get help from the official website.

Project preparation:

ACRM
|-Res
|-Values
|-Strings. xml
|-XML
|-Config. xml
|-Androidmanifest. xml
|-Build. xml
|-Env-process.groovy

The content of each file is as follows:

Strings. xml

<string name="app_name">ACRM</string>

Config. xml

<vmness>    <!-- VERBOSE|DEBUG|INFO|WARN|ERROR|FATAL -->    <log level="DEBUG"/>    <env type="prod"/>    <supported-features>        <feature name="chart"/>    </supported-features></vmness>

Androidmanifest. xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.vmn.app.crm"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="9"        android:targetSdkVersion="17" />    <application        android:name="com.vmn.app.crm.CrmAPP"        android:debuggable="true"        android:icon="@drawable/crm_app_icon"        android:label="@string/crm_app_title" >        <activity            android:name="com.vmn.app.crm.activity.MainViewActivity"                android:label="@string/cv_string_app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Code display (core code)

Call the env-process.groovy script in the build. xml script

<target name="replace-android-resources" depends="resolve">  <groovy src="${basedir}/env-process.groovy"></groovy></target>

Env-process.groovy

View code

 

Related Article

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.