简体   繁体   中英

How can I attach different string resource files during android ant release and debug builds?

I want to attach different string resources files during android ant release and debug builds. I need this to have separate configuration for map api key, remote host etc

I use Eclipse for day to day debug builds, then Ant for release builds. I have debug and release versions of the file holding the Google Maps API key. I've modified the build.xml (I'm on SDK tools R15) to do some copying of the appropriate file before the build and afterwards. I've changed the -pre-build and release targets like so:

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

I define layout.dir in the ant.properties (new name for build.properties post SDK Tools 14) file:

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

You could adapt this to suit your needs, assuming you don't have too many files to swap in and out. I guess you could add a property for the directory holding your strings.xml

Nick, I don't understand why you also rewrite release task. I've added prebuild only and it works for me

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

The two answers are all based on Eclipse ADT . Now Android Studio is the more popular IDE, you can take advantage of gradle build system.

In your build.gradle file, add the following code in android tag:

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"))
                }
            }
        }
    }

You can use BuildConfig.LOG_DEBUG as a boolean value in your Java code. Just remember to import your.package.name.BuildConfig;

You can also use some_key as a string resource. The Java code of using it is: R.string.some_key .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM