【问题标题】:How can I attach different string resource files during android ant release and debug builds?如何在 android ant 发布和调试构建期间附加不同的字符串资源文件?
【发布时间】:2011-11-26 12:03:22
【问题描述】:

我想在 android ant 发布和调试构建期间附加不同的字符串资源文件。我需要这个单独配置地图 api 密钥、远程主机等

【问题讨论】:

    标签: java android ant


    【解决方案1】:

    我使用 Eclipse 进行日常调试构建,然后使用 Ant 进行发布构建。我有保存 Google Maps API 密钥的文件的调试和发布版本。我已经修改了 build.xml(我在 SDK 工具 R15 上),以便在构建之前和之后复制一些适当的文件。我已经改变了 -pre-build 和 release 目标,如下所示:

        <target name="-pre-build">
            <echo message="In pre build-----------------------------------" />
            <echo message="build target          ${build.target}" />
            <if condition="${build.is.packaging.debug}">
                <then>
                    <echo>Copying debug api key************************************</echo>
                    <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
                </then>
                <else>
                    <echo>Copying RELEASE api key************************************</echo>
                    <copy file="${layout.dir}/googlemaprelease.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
                </else>
            </if>
        </target>
    
    
    
    
    <target name="release"
                depends="clean, -set-release-mode, -release-obfuscation-check, -package, -release-prompt-for-password, -release-nosign"
            ............. LINES OMITTED
            ............. 
                <!-- Zip aligns the APK -->
                <zipalign-helper in.package="${out.unaligned.file}"
                                           out.package="${out.final.file}" />
                <echo>Release Package: ${out.final.file}</echo>
    
                <echo>Always copy DEBUG MAPS API file back for Eclipse   **********************</echo>
                <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>
    

    我在 ant.properties(SDK Tools 14 后 build.properties 的新名称)文件中定义了 layout.dir:

    projectname=MyProject
    workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
    base.dir=${workspace.dir}/${projectname}
    layout.dir=${base.dir}/res/layout
    

    假设您没有太多文件需要换入和换出,您可以根据自己的需要进行调整。我想你可以为保存你的 strings.xml 的目录添加一个属性

    【讨论】:

    • 尼克,我不明白你为什么还要重写发布任务。我只添加了预构建,它对我有用
    • 那是因为我通常每天都在使用 Eclipse。所以下次我用 Eclipse 构建时,我希望它使用地图 API xml 文件的调试版本。如果我不更改发布目标,则将使用文件的发布版本,因为我主要使用 Ant 构建来制作发布版本。
    【解决方案2】:

    尼克,我不明白你为什么还要重写发布任务。我只添加了预构建,它对我有用

    <target name="-pre-build">
        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>Copying debug config</echo>
                <copy file="./config/debug.xml" tofile="./res/values/config.xml" overwrite="true"/>
            </then>
            <else>
                <echo>Copying release config</echo>
                <copy file="./config/release.xml" tofile="./res/values/config.xml" overwrite="true"/>
            </else>
        </if>
    </target>
    

    【讨论】:

      【解决方案3】:

      这两个答案都是基于Eclipse ADT。现在Android Studio是比较流行的IDE,你可以利用gradle构建系统。

      在您的build.gradle 文件中,在android 标签中添加以下代码:

      buildTypes {
              release {
                  buildConfigField "boolean", "LOG_DEBUG", "false"
                  resValue "string", "some_key", "AAAAAAA"
              }
              debug {
                  buildConfigField "boolean", "LOG_DEBUG", "true"
                  resValue "string", "some_key", "BBBBBBB"
      
                  applicationVariants.all { variant ->
                      variant.outputs.each { output ->
                          output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                      }
                  }
              }
          }
      

      您可以在 Java 代码中使用 BuildConfig.LOG_DEBUG 作为 boolean 值。只要记得import your.package.name.BuildConfig;

      您也可以使用some_key 作为字符串资源。使用它的Java代码是:R.string.some_key

      【讨论】:

      • 是否可以有两个不同的资源文件?我想要release.preferences.xmldebug.preferences.xml
      • @orian 我还没有尝试过两个 res 文件。我认为使用 gradle 脚本非常简单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 2011-02-07
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 2012-12-26
      相关资源
      最近更新 更多