android - 使用gradle修改android构建

标签 android gradle android-build

我们已经修改了我们的 android ant 构建。 build.xml 文件现在如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<!-- This is a modified version of the "dex-helper" macro.  It added the "input-dir" and
     "output-dex-file" required attributes.
     Configurable macro, which allows to pass as parameters input directory,
     output directory, output dex filename and external libraries to dex (optional) -->
<macrodef name="dex-helper-mod">
    <attribute name="input-dir" />
    <attribute name="output-dex-file" />
    <element name="external-libs" optional="yes" />
    <element name="extra-parameters" optional="yes" />
    <attribute name="nolocals" default="false" />
    <sequential>

        <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />
        <!-- set the secondary dx input: the project (and library) jar files
             This has been disabled in order to avoid redundant class definitions in the plugin and the core app
        <if>
            <condition>
                <isreference refid="out.dex.jar.input.ref" />
            </condition>
            <else>
                <path id="out.dex.jar.input.ref">
                    <path refid="project.all.jars.path" />
                </path>
            </else>
        </if> -->

        <echo>Converting compiled files and external libraries into @{output-dex-file}...</echo>
        <dex executable="${dx}" output="@{output-dex-file}" nolocals="@{nolocals}" verbose="${verbose}">
            <path path="@{input-dir}" />
            <!-- <path refid="out.dex.jar.input.ref" /> -->
            <external-libs />
        </dex>
    </sequential>
</macrodef>

<!-- This is a modified version of "-dex" target taken from $SDK/tools/ant/main_rules.xml -->
<!-- Converts this project's .class files into .dex files -->
<target name="-dex" depends="-compile, -post-compile, -obfuscate" unless="do.not.compile">
    <if condition="${manifest.hasCode}">
        <then>

            <mkdir dir="${out.classes.absolute.dir}" />

            <copy todir="${out.classes.absolute.dir}">
                <fileset dir="${out.classes.absolute.dir}">

                </fileset>
            </copy>


            <dex-helper-mod input-dir="${out.classes.absolute.dir}" output-dex-file="${out.absolute.dir}/${dex.file.name}" />
        </then>
        <else>
            <echo>hasCode = false. Skipping...</echo>
        </else>
    </if>
</target>

<!-- version-tag: custom -->
<import file="${sdk.dir}/tools/ant/build.xml" />

现在我们将构建逻辑迁移到 gradle。是否可以像上面那样修改 gradle 构建?

最佳答案

以下 gradle 代码有助于从 ant 脚本迁移:

android {
    buildTypes {
        // prevents dexing of lib classes
        // replaces the modified ant script
        applicationVariants.each { variant ->
            variant.dex.libraries = new ArrayList<?>()
        }
    }
}

关于android - 使用gradle修改android构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044671/

相关文章:

java - 我正在检查 fragment 类中的登录过程,那么当我成功登录后搬到家时如何关闭MainActivity

android - 在 X-Cross 平台库上使用 JsonCpp

android - CollapsingToolbarLayout setTitle 不能立即工作

gradle - 如何在 Mac 上使用/启动 gradle 守护进程?

android - 在 Android Studio 中为 Gradle 添加 AppCompat 依赖

ANT Gradle - Axis WSDL 2 Java : No such property: axis for class: org. gradle.api.internal

android - 如何使用bundletool从AAB生成APK(不是APKS)?

android - 使用 --env.aot 标志时未构建 Nativescript 应用程序

android - 如何将 Gradle 任务添加到 Android 构建

android - 在 Windows 7 上为 iOS 生成配置证书