Is it possible to specify the location of a self created debug keystore when creating debug .apk
\'s (
) with ant
Create a new and empty folder within your Eclipse workspace.
Copy the file "debug.keystore" from its standard location (e.g. "c:/Users/YourAcount/.Android/" on Windows) into that new directory.
Configure Eclipse to use that new directory/file for all Debug APKs (Eclipse --> Window --> Preferences --> Android --> Build --> Custom Debug Keystore".
As far as I can tell, in order to do this you need to override the "-do-debug" target. I'd like to know if there's a more elegant way, though. I added this to my build.xml
<property name="build.is.signing.debug" value="false"/>
<target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package, -post-package">
<!-- only create apk if *not* a library project -->
<do-only-if-not-library elseText="Library project: do not create apk..." >
<sequential>
<property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
<!-- Signs the APK -->
<echo>Signing final apk...</echo>
<signjar
jar="${out.packaged.file}"
signedjar="${out.unaligned.file}"
keystore="${key.store}"
storepass="${key.store.password}"
alias="${key.alias}"
keypass="${key.alias.password}"
verbose="${verbose}" />
<!-- Zip aligns the APK -->
<zipalign-helper
in.package="${out.unaligned.file}"
out.package="${out.final.file}" />
<echo>Debug Package: ${out.final.file}</echo>
</sequential>
</do-only-if-not-library>
<record-build-info />
</target>
<import file="${sdk.dir}/tools/ant/build.xml" />
As far as I've seen so far no existing solution works with the latest ant version.
The only primitive way of doing it is to replace the default debug.keystore its in default folder.
The default location is the .android folder. Usually at C:\Users\USER_NAME.android On windows you can get to it using %USERPROFILE%.android.
What I did is create a powershell script to backup the default file, copy keystore file to the default location, build the signed debug apk, delete my keystore file, restore original file.
Let me know if you need more information here.
I found the another solution..
Add the following in ${PROJECT_HOME}/custom_rules.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules">
<!-- @Override -->
<target name="-set-debug-mode" depends="-setup">
<!-- record the current build target -->
<property name="build.target" value="debug" />
<if>
<condition>
<and>
<istrue value="${project.is.testapp}" />
<istrue value="${emma.enabled}" />
</and>
</condition>
<then>
<property name="build.is.instrumented" value="true" />
</then>
<else>
<property name="build.is.instrumented" value="false" />
</else>
</if>
<!-- whether the build is a debug build. always set. -->
<property name="build.is.packaging.debug" value="true" />
<!-- signing mode: debug -->
<property name="build.is.signing.debug" value="false" />
<!-- Renderscript optimization level: none -->
<property name="renderscript.opt.level" value="${renderscript.debug.opt.level}" />
</target>
<!-- @Override -->
<!-- Builds debug output package -->
<target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package, -post-package">
<property name="out.unsigned.file" location="${out.absolute.dir}/${ant.project.name}-debug-unsigned.apk"/>
<move file="${out.packaged.file}" tofile="${out.unsigned.file}"/>
<property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
<!-- Signs the APK -->
<echo level="info">Signing final apk...</echo>
<signapk
input="${out.unsigned.file}"
output="${out.unaligned.file}"
keystore="${out.absolute.dir}/../debug.keystore" <!-- your debug.keystore path -->
storepass="android"
alias="androiddebugkey"
keypass="android"/>
<!-- only create apk if *not* a library project -->
<do-only-if-not-library elseText="Library project: do not create apk..." >
<sequential>
<zipalign-helper in.package="${out.unaligned.file}" out.package="${out.final.file}" />
<echo level="info">Debug Package: ${out.final.file}</echo>
</sequential>
</do-only-if-not-library>
<record-build-info />
</target>
</project>
You can remove the signature of the final apk and sign it again. It is just a debug build so the zipalign can be avoided (at least I have no problems in my build).
Copy your keystore to a file debug.keystore in the project and add the following in ant.properties
debug.key.store.password=android
debug.key.alias.password=android
debug.key.store=../debug.keystore
debug.key.alias=androiddebugkey
And add the following in your build.xml
<target name="-post-build" if="${build.is.packaging.debug}">
<!-- Remove the signature of the debug build, and sign it again with our own debug keystore -->
<delete dir="tmp" includeemptydirs="true" failonerror="false" />
<mkdir dir="tmp" />
<unzip src="${out.final.file}" dest="tmp" />
<delete dir="tmp/META-INF" includeemptydirs="true" verbose="true" failonerror="true" />
<delete file="${out.final.file}" failonerror="true" />
<zip destfile="${out.final.file}" basedir="tmp" />
<delete dir="tmp" includeemptydirs="true" failonerror="false" />
<echo level="info">Signing final DEBUG apk with a common signature...
signapk
input="${out.final.file}"
output="${out.packaged.file}"
keystore="${debug.key.store}"
storepass="${debug.key.store.password}"
alias="${debug.key.alias}"
keypass="${debug.key.alias.password}"
</echo>
<signapk
input="${out.final.file}"
output="${out.packaged.file}"
keystore="${debug.key.store}"
storepass="${debug.key.store.password}"
alias="${debug.key.alias}"
keypass="${debug.key.alias.password}"/>
<delete file="${out.final.file}" failonerror="true" />
<move file="${out.packaged.file}" tofile="${out.final.file}" failonerror="true" />
</target>
You should be able to specify the keystore to use with these properties
key.store=/path/to/key.keystore
key.alias=alias
key.store.password=pass
key.alias.password=pass
Just pass the properties in to Ant.
[EDIT] From the docs at http://developer.android.com/guide/publishing/app-signing.html#setup
The Android build tools provide a debug signing mode that makes it easier for you to develop and debug your application, while still meeting the Android system requirement for signing your .apk. When using debug mode to build your app, the SDK tools invoke Keytool to automatically create a debug keystore and key. This debug key is then used to automatically sign the .apk, so you do not need to sign the package with your own key.
The SDK tools create the debug keystore/key with predetermined names/passwords:
If necessary, you can change the location/name of the debug keystore/key or supply a custom debug keystore/key to use. However, any custom debug keystore/key must use the same keystore/key names and passwords as the default debug key (as described above). (To do so in Eclipse/ADT, go to Windows > Preferences > Android > Build.)