如何使用 Ant 使用 Prebop 生成 Android apk

发布于 2025-01-04 08:06:20 字数 4898 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2025-01-11 08:06:20

您应该(至少)将“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.

沉鱼一梦 2025-01-11 08:06:20

为了在命令行中构建 android,您应该遵循以下步骤;

首先创建一个android项目;

PATH_TO_SDK/android create project -n NAME -p PATH -k PACKAGE -a ACTIVITY -t TARGET_ID

NAME:您的项目名称
PATH:你的项目路径
PACKAGE:您的项目包(例如 com.example )
活动:您的主要活动
TARGET_ID:您的目标 SDK 的 id

然后您可以使用 ant 进行构建,如下所示;

ant release

或者

ant PATH/build.xml release

(注意:您可以在 build.xml 中进行您想要的安排)

如果您的项目没有错误,您的项目应该已成功构建。

现在,您可以在 bin/ 目录中看到调试 apk 文件。

如果您进行发布构建,您应该使用 jarsigner 来签署您的项目,如下所示;

jarsigner -verbose -storepass PASS -keypass KEY_PASS -keystore KEY_STORE_PATH PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk YOUR_KEY_STORE_FILE

然后验证你的.apk文件;

jarsigner -verbose -verify PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk

最后,你就可以制作最终发布的apk文件了;

ANDROID_SDK_PATH/zipalign 4 PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk PROJECT_PATH/bin/PROJECT_NAME-release.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;

PATH_TO_SDK/android create project -n NAME -p PATH -k PACKAGE -a ACTIVITY -t TARGET_ID

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;

ant release

or

ant PATH/build.xml release

( 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;

jarsigner -verbose -storepass PASS -keypass KEY_PASS -keystore KEY_STORE_PATH PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk YOUR_KEY_STORE_FILE

Then verify your .apk file;

jarsigner -verbose -verify PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk

Finally, you are able to make final release apk file;

ANDROID_SDK_PATH/zipalign 4 PROJECT_PATH/bin/PROJECT_NAME-release-unsigned.apk PROJECT_PATH/bin/PROJECT_NAME-release.apk

Your apk is ready to release here;

PROJECT_PATH/bin/PROJECT_NAME-release.apk

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文