如何使用 Ant 使用 Prebop 生成 Android apk
我正在开发一个 Android 项目,该项目需要来自同一代码库(例如 Lite/Pro)的多个版本,但 Java 不允许预处理指令。 Google 上的搜索只会抛出此 这个关于 Prebop 预处理器的教程。问题是该教程不适用于 Android,我不知道如何在给定特定版本的编译后生成 apk。这是 ant 代码:
<?xml version="1.0"?>
<project name="Hello" default="compile" basedir=".">
<property file="local.properties" />
<property file="ant.properties" />
<loadproperties srcFile="project.properties" />
<property name="android-home" value="../android-sdks" />
<property name="android-version" value="3" />
<property name="android-platform-specific" value="${android-home}/platforms/android-${android-version}" />
<property name="android-jar" value="${android-platform-specific}/android.jar" />
<property name="android-tools" value="${android-home}/platform-tools" />
<property name="resource-folder" value="res" />
<property name="gen-folder" value="gen" />
<property name="src" location="src" />
<property name="tmpfolder" value="/tmp/Hello" />
<property name="src_lite" value="${tmpfolder}/src_lite"/>
<property name="src_pro" value="${tmpfolder}/src_pro"/>
<property name="build" location="bin" />
<property name="build_lite" value="${build}/lite"/>
<property name="build_pro" value="${build}/pro"/>
<property name="out-folder" value="${build_lite}" />
<property name="dex-file" value="classes.dex" />
<property name="intermediate-dex" value="${out-folder}/${dex-file}" />
<property name="external-libs-folder" value="bin/lite" />
<property name="out-classes" value="${out-folder}/classes" />
<property name="out-classes-location" value="./${out-classes}"/>
<target name="clean">
<delete dir="${tmpfolder}" />
<delete dir="${build}" />
</target>
<target name="makedir">
<mkdir dir="${src_lite}" />
<mkdir dir="${src_pro}" />
<mkdir dir="${build_lite}" />
<mkdir dir="${build_pro}" />
</target>
<target name="resource-src" depends="makedir">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${android-tools}/aapt.exe" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg path="${gen-folder}" />
<arg value="-M" />
<arg path="AndroidManifest.xml" />
<arg value="-S" />
<arg path="${resource-folder}" />
<arg value="-I" />
<arg path="${android-jar}" />
<arg value="--custom-package" />
<arg value="com.pack.name" />
</exec>
</target>
<target name="copysource">
<copy todir="${src_lite}"><fileset dir="src" /></copy>
<copy todir="${src_pro}"><fileset dir="src" /></copy>
</target>
<target name="splitsource" description="preprocessor">
<preprocess indir="${src}" outdir="${src_lite}" out="replace">
<var name="pro" value="false"/>
<filetype commentend="*/" commentbegin="/*" extensions="java"/>
</preprocess>
<preprocess indir="${src}" outdir="${src_pro}" out="replace">
<var name="pro" value="true"/>
<filetype commentend="*/" commentbegin="/*" extensions="java"/>
</preprocess>
</target>
<target name="compile" depends="makedir, copysource">
<javac srcdir="${src_lite}" destdir="${build_lite}" includeantruntime="false">
<classpath>
<path id="application" location="${android-jar}"/>
<path id="application" location="src" />
<path id="application" location="res" />
</classpath>
</javac>
<javac srcdir="${src_pro}" destdir="${build_pro}" includeantruntime="false">
<classpath>
<path id="application" location="${android-jar}"/>
<path id="application" location="src" />
<path id="application" location="res" />
</classpath>
</javac>
</target>
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
我希望有人能给我指导,因为在 Android 上使用多个 API 进行开发是相当令人沮丧的。
I'm working on an Android project that need multiple versions from the same codebase (e.g. Lite/Pro) but Java doesn't allow preprocessing directives. A search on Google only throws this this tutorial about the Prebop preprocessor. The problem is that the tutorial is not for Android and I don't know how to generate the apk after compile given an specific version. This is the ant code:
<?xml version="1.0"?>
<project name="Hello" default="compile" basedir=".">
<property file="local.properties" />
<property file="ant.properties" />
<loadproperties srcFile="project.properties" />
<property name="android-home" value="../android-sdks" />
<property name="android-version" value="3" />
<property name="android-platform-specific" value="${android-home}/platforms/android-${android-version}" />
<property name="android-jar" value="${android-platform-specific}/android.jar" />
<property name="android-tools" value="${android-home}/platform-tools" />
<property name="resource-folder" value="res" />
<property name="gen-folder" value="gen" />
<property name="src" location="src" />
<property name="tmpfolder" value="/tmp/Hello" />
<property name="src_lite" value="${tmpfolder}/src_lite"/>
<property name="src_pro" value="${tmpfolder}/src_pro"/>
<property name="build" location="bin" />
<property name="build_lite" value="${build}/lite"/>
<property name="build_pro" value="${build}/pro"/>
<property name="out-folder" value="${build_lite}" />
<property name="dex-file" value="classes.dex" />
<property name="intermediate-dex" value="${out-folder}/${dex-file}" />
<property name="external-libs-folder" value="bin/lite" />
<property name="out-classes" value="${out-folder}/classes" />
<property name="out-classes-location" value="./${out-classes}"/>
<target name="clean">
<delete dir="${tmpfolder}" />
<delete dir="${build}" />
</target>
<target name="makedir">
<mkdir dir="${src_lite}" />
<mkdir dir="${src_pro}" />
<mkdir dir="${build_lite}" />
<mkdir dir="${build_pro}" />
</target>
<target name="resource-src" depends="makedir">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${android-tools}/aapt.exe" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg path="${gen-folder}" />
<arg value="-M" />
<arg path="AndroidManifest.xml" />
<arg value="-S" />
<arg path="${resource-folder}" />
<arg value="-I" />
<arg path="${android-jar}" />
<arg value="--custom-package" />
<arg value="com.pack.name" />
</exec>
</target>
<target name="copysource">
<copy todir="${src_lite}"><fileset dir="src" /></copy>
<copy todir="${src_pro}"><fileset dir="src" /></copy>
</target>
<target name="splitsource" description="preprocessor">
<preprocess indir="${src}" outdir="${src_lite}" out="replace">
<var name="pro" value="false"/>
<filetype commentend="*/" commentbegin="/*" extensions="java"/>
</preprocess>
<preprocess indir="${src}" outdir="${src_pro}" out="replace">
<var name="pro" value="true"/>
<filetype commentend="*/" commentbegin="/*" extensions="java"/>
</preprocess>
</target>
<target name="compile" depends="makedir, copysource">
<javac srcdir="${src_lite}" destdir="${build_lite}" includeantruntime="false">
<classpath>
<path id="application" location="${android-jar}"/>
<path id="application" location="src" />
<path id="application" location="res" />
</classpath>
</javac>
<javac srcdir="${src_pro}" destdir="${build_pro}" includeantruntime="false">
<classpath>
<path id="application" location="${android-jar}"/>
<path id="application" location="src" />
<path id="application" location="res" />
</classpath>
</javac>
</target>
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
I hope someone can give me directions because is quite frustrating develop with multiple API's on Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该(至少)将“splitsource”依赖项添加到“compile”目标而不是“copysource”,以便使用预处理的java文件生成类文件。
You should (at least) add "splitsource" dependency to "compile" target instead "copysource", so class files are generated with pre-processed java ones.
为了在命令行中构建 android,您应该遵循以下步骤;
首先创建一个android项目;
NAME:您的项目名称
PATH:你的项目路径
PACKAGE:您的项目包(例如 com.example )
活动:您的主要活动
TARGET_ID:您的目标 SDK 的 id
然后您可以使用 ant 进行构建,如下所示;
或者
(注意:您可以在 build.xml 中进行您想要的安排)
如果您的项目没有错误,您的项目应该已成功构建。
现在,您可以在 bin/ 目录中看到调试 apk 文件。
如果您进行发布构建,您应该使用 jarsigner 来签署您的项目,如下所示;
然后验证你的.apk文件;
最后,你就可以制作最终发布的apk文件了;
您的 apk 已准备好在此处发布;
PROJECT_PATH/bin/PROJECT_NAME-release.apk
In order to build android in command-line, you should follow below steps;
First create an android project;
NAME: your project name
PATH: your project path
PACKAGE: your project package ( e.g. com.example )
ACTIVITY: your main activity
TARGET_ID: your target SDK 's id
Then you can build with ant like below;
or
( NOTE: you can make arrangement you want in build.xml )
Your project should be builded successfully if your project doesn't have an error.
Now, you can see the debug apk files in the bin/ directory.
If you make release build, you should use jarsigner to sign your project like below;
Then verify your .apk file;
Finally, you are able to make final release apk file;
Your apk is ready to release here;
PROJECT_PATH/bin/PROJECT_NAME-release.apk