【问题标题】:I want to customize the build process while still using the Android Eclipse ADT plug-in我想在仍然使用 Android Eclipse ADT 插件的同时自定义构建过程
【发布时间】:2011-02-28 04:30:23
【问题描述】:

安装 ADT 插件后,以下构建器可用于 Android 项目:

  • Android 资源管理器
  • Android 预编译器
  • Java 构建器
  • Android 包生成器

查看输出目录,创建了以下工件:

  • resources.ap_(只是一个包含资源且没有代码的 APK/ZIP)
  • gen/R.java(自动生成的资源列表)
  • 带有java字节码的.class文件
  • classes.dex
  • ${项目名称}.apk

对于我的项目,我会自动生成多个代码工件,并且通常需要对构建过程进行更严格的控制。一开始我以为是资源管理器负责创建resources.ap_,预编译器创建R.java,java builder做的很明显,然后Android Package Builder创建classes.dex,然后合并classes.dex和resources.ap_创建 APK 文件。

我禁用了前两个步骤并创建了一个自定义的预构建器,该构建器放置了 resources.ap_ 的副本,我认为这将是等效的。没有这样的运气。

不幸的是,最终的 Android Package Builder 似乎直接从 res/ 中获取资源,并忽略了我的 resources.ap_。事实上,前两个构建步骤除了生成 R.java 之外似乎没有什么作用。

这是真正有问题的地方。如果我禁用最后的构建步骤并放置我自己的 APK 文件(与 exact 相同的名称),我会收到以下错误:

[2011-02-27 20:25:28 - android-premium] ------------------------------
[2011-02-27 20:25:28 - android-premium] Android Launch!
[2011-02-27 20:25:28 - android-premium] adb is running normally.
[2011-02-27 20:25:28 - android-premium] Could not find android-premium.apk!

所以我被困住了:使用 Android Package Builder(它没有可识别的配置),我必须提供单独的 ./res/ 文件。没有它,我无法让项目在设备上启动(不是从 Eclipse 启动)。

有人在这个领域有更好的想法/经验吗?

【问题讨论】:

    标签: android eclipse ant build adt


    【解决方案1】:

    关于 Ant 脚本,以下是我构建 Android 应用程序的 Ant 步骤的一体化模板(这可能对尝试将 Eclipse 构建转换为 Ant 的其他人有用,因为它比使用android工具自动生成的不透明版本):

    <property name="workspace.dir" value="/workspace" />
    <property name="project.dir" location="." />
    <property name="android-platform.name" value="android-1.5" />
    
    
    <!-- Framework Paths -->
    <property name="android-sdk.dir" value="${workspace.dir}/EpicFramework/android-sdk"/>
    <property name="android-tools.dir" value="${android-sdk.dir}/tools" />
    <property name="android-platform.dir" value="${android-sdk.dir}/platforms/${android-platform.name}" />
    <property name="android-platform-tools.dir" value="${android-platform.dir}/tools"/>
    <property name="android-platform-jar" value="${android-platform.dir}/android.jar"/>
    <property name="android-framework-src.dir" value="${workspace.dir}/EpicFramework/EpicAndroid"/>
    
    <!-- Tool Binaries -->
    <property name="adb" value="${android-tools.dir}/adb"/>
    <property name="apkbuilder" value="${android-tools.dir}/apkbuilder"/>
    <property name="aapt" value="${android-platform-tools.dir}/aapt"/>
    <property name="dx" value="${android-platform-tools.dir}/dx"/>
    
    <!-- Project Paths -->
    <property name="src.dir"   value="${project.dir}/src" />
    <property name="gen.dir"   value="${project.dir}/gen" />
    <property name="res.dir"   value="${project.dir}/res" />
    <property name="lib.dir"   value="${project.dir}/lib" />
    <property name="build.dir" value="${project.dir}/bin" />
    <property name="build.classes.dir" value="${build.dir}/classes" />
    
    <property name="resources.file" value="${build.dir}/resources.ap_"/>
    <property name="dex.file" value="${build.dir}/classes.dex" />
    <property name="debug-apk.file" value="${build.dir}/${ant.project.name}-debug.apk"/>
    
    
    <target name="dirs">
        <echo>Creating output directories if needed...</echo>
        <mkdir dir="${res.dir}" />
        <mkdir dir="${gen.dir}" />
        <mkdir dir="${lib.dir}" />
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.classes.dir}" />
    </target>
    
    <target name="android-resource-src" depends="dirs">
        <echo>Generating R.java from the resources...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package" />
            <arg value="-M" />
            <arg path="AndroidManifest.xml" />
            <arg value="-S" />
            <arg path="${res.dir}" />
            <arg value="-I" />
            <arg path="${android-platform-jar}" />
            <arg value="-m" />
            <arg value="-J" />
            <arg path="${gen.dir}" />
        </exec>
    </target>
    
    <target name="android-compile" depends="android-resource-src">
       <echo>Compiling java files into class files...</echo>
        <javac 
          encoding="ascii"
          target="1.5"
          debug="true"
          extdirs=""
          destdir="${build.classes.dir}"
          includeAntRuntime="false"
          bootclasspath="${android-platform-jar}">
            <src path="${android-framework-src.dir}" />
            <src path="${src.dir}" />
            <src path="${gen.dir}" />
            <classpath>
                <fileset dir="${lib.dir}" includes="*.jar"/>
            </classpath>
         </javac>
    </target>
    
    <target name="android-dex" depends="android-compile">
        <echo>Converting compiled files and 3rd party libraries into ${dex.file} ...</echo>
        <apply executable="${dx}" failonerror="true" parallel="true">
            <arg value="--dex" />
            <arg value="--output=${dex.file}" />
            <arg path="${build.classes.dir}" />
            <fileset dir="${lib.dir}" includes="*.jar"/>
        </apply>
    </target>
    
    <target name="android-package-resources">
        <echo>Packaging Android resources into ${resources.file} ...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package" />
            <arg value="-M" />
            <arg path="AndroidManifest.xml" />
            <arg value="-S" />
            <arg path="./res" />
            <arg value="-I" />
            <arg path="${android-platform-jar}" />
            <arg value="-f" />
            <arg value="-F" />
            <arg value="${resources.file}" />
        </exec>
    </target>
    
    <target name="android-debug" depends="android-dex, android-package-resources">
        <echo>Packaging app and signing it with the debug key</echo>
        <exec executable="${apkbuilder}" failonerror="true">
            <arg value="${debug-apk.file}" />
            <arg value="-d" />
            <arg value="-z" />
            <arg value="${resources.file}/" />
            <arg value="-f" />
            <arg value="${dex.file}" />
        </exec>
    </target>
    
    <target name="android-release" depends="android-dex, android-package-resources">
        <echo>Packaging App without signing, so that it can be signed with the official publishing key ...</echo>
        <exec executable="${apkbuilder}" failonerror="true">
            <arg value="${release-apk.file}" />
            <arg value="-u" />
            <arg value="-d" />
            <arg value="-z" />
            <arg value="${resources.file}/" />
            <arg value="-f" />
            <arg value="${dex.file}" />
        </exec>
        <echo>All generated packages need to be signed with jarsigner before they are published.</echo>
    </target>
    
    <target name="android-install" depends="android-debug">
        <echo>Removing all com.epicapplications APK files from default emulator ...</echo>
        <exec executable="${adb}" inputstring="echo hi; rm -f /data/app/com.epicapplications.*; exit;
        ">
            <arg value="shell"/>
        </exec>
        <echo>Installing ${debug-apk.file} onto default emulator...</echo>
        <exec executable="${adb}" failonerror="true">
            <arg value="install" />
            <arg value="-r" />
            <arg path="${debug-apk.file}" />
        </exec>
    </target>
    

    我正在寻找的是能够使用此 Ant 构建脚本的全部或片段,但仍具有用于启动和调试应用程序的 Eclipse 集成。

    【讨论】:

    • 哇,讽刺。我创建了一个新项目,但无法挖掘我的构建脚本......所以我用谷歌搜索......找到了这个......只有当我深究时我才意识到我已经写了它.
    • 如果我只想获取你的 android-dex 目标并运行它来代替常规的 dex 目标,我该怎么做?
    【解决方案2】:

    我们使用 Ant 脚本来完成 android 项目的构建过程,其他人也使用 Maven。这些帮助您可以编写脚本,使其每天作为夜间构建的一部分运行,作为 CI 构建系统的一部分。在高级级别上,您可以编写脚本来完成包含发布标签、预编译和编译后 AndroidManifest.xml 修改和签名的发布构建。

    【讨论】:

    • 是的,我也有一个 Ant 构建脚本,它在命令行中运行良好。但是,我正在寻找的工作流程是:1)在 java 文件中更改几行,2)运行(CTL-Shift-F11),3)应用程序在设备上启动。我可以从 Ant 构建所有东西。事实上,将 Ant 脚本集成到 Eclipse 的构建链中非常容易。问题是 ADT 拒绝在不运行最后一个整体构建步骤的情况下启动我的应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多