How to generate BuildConfig.DEBUG? - android

Using an IDE like Eclipse, IntelliJ, Studio, the class BuildConfig is auto generated in the gen folder. But what if you are using makefiles; not using an IDE? Can any of the build tools auto generate this?
When I tried to add android:debuggable attribute to my AndroidManifest.xml
as described in the link below, I got a compiler error that it could not find the field.
http://developer.android.com/tools/publishing/preparing.html
Turn off logging and debugging
Make sure you deactivate logging and disable the debugging option
before you build your application for release. You can deactivate
logging by removing calls to Log methods in your source files. You can
disable debugging by removing the android:debuggable attribute from
the tag in your manifest file, or by setting the
android:debuggable attribute to false in your manifest file. Also,
remove any log files or static test files that were created in your
project.

Yes, the build tools from command can generate BuildConfig.java file for you.
1.create project in command
$ android create project -t android-21 -p . -n AntTest --package com.example.test -a StartupActivity
2.create the makefile
release:
#ant release
debug:
#ant debug
release_install:
#ant installr
debug_install:
#ant installd
3.you can reference BuildConfig in your code, the build tool ant will generate BuildConfig.java for you.
After you invoke any command to build the project, you will see the file is generated gen/com/example/test/BuildConfig.java.
Additional information
BuildConfig.java is handled and generated by ant. We can see it from $ANDROID_SDK_ROOT/tools/ant/build.xml.
<echo level="info">----------</echo>
<echo level="info">Handling BuildConfig class...</echo>
<buildconfig
genFolder="${gen.absolute.dir}"
package="${project.app.package}"
buildType="${build.is.packaging.debug}"
previousBuildType="${build.last.is.packaging.debug}"/>

Related

Ant doesn't pick up file modification when build

I'm trying to build my android project from command line.
ant debug install && adb shell 'am start -n com.test.example/.MainActivity'
I find, ant doesn't recognise modified files sometimes(or most of times) unless I do ant clean
Is there a workaround to make ant recognise file changes while developing?
This is a known issue:
ant clean
ant debug <-- This result is OK.
change some source code
ant debug
RESULT:
-dex:
[dex] No new compiled code. No need to convert bytecode to dalvik format.
...
-package:
[apkbuilder] No changes. No need to create apk.
you can try a few things things:
Workaround for [dex]: include a <delete> just before the call to <dex /> within the macro "dex-helper":
<delete file="${intermediate.dex.file}"/>
Workaround for [apkbuilder]: include a <delete> into the macro "package-helper":
<macrodef name="package-helper">
<element name="extra-jars" optional="yes" />
<sequential>
<delete failonerror="false" file="${out.packaged.file}"/>
<apkbuilder outfolder="${out.abs.dir}" resourcefile="${res.pkg.file.name}"
...
See HERE for these Workarounds and a long, related, discussion.
these may be useful too:
try ant clean debug: call clean, because otherwise the build system will not detect changes in the configuration, and may not recompile the proper classes. , OR
android update project -p <project-path>: will generate any files and folders that are either missing or need to be updated, as needed for the Android project See HERE

Facebook Unity SDK android barfs when project connected to Asset Server

Tricky little issue. A test project with FB plugin worked fine, my project gave me below error. Turns out when i connected FB test project to asset server, and the .meta files got created for each folder, it would barf also. Any way to tell compiler to skip .meta files?
Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details.
/Users/me/android-sdk-macosx/platform-tools/aapt package --auto-add-overlay -v -f -m -J gen -M AndroidManifest.xml -S "res" -I "/Users/me/android-sdk- macosx/platforms/android-17/android.jar" -F bin/resources.ap_ --extra-packages com.facebook.android -S "/Users/me/Unity/FBTest/Assets/Plugins/Android/facebook/res"
stderr[
invalid resource directory name: /Users/me/Unity/FBTest/Assets/Plugins/Android/facebook/res/drawable-hdpi.meta
invalid resource directory name: /Users/me/Unity/FBTest/Assets/Plugins/Android/facebook/res/drawable-ldpi.meta
invalid resource directory name: /Users/me/Unity/FBTest/Assets/Plugins/Android/facebook/res/drawable-mdpi.meta
UPDATE: can't seem to get rid of .meta files, even though i disconnected from asset server :( any ideas?
UPDATE: had to go Edit -> Project Settings -> Editor and disable version control to get this to compile. Would be nice if this could be fixed so meta files are ignored in the packageing of an APK...
May as well mark this as answered. The .meta files added for asset server are breaking the android packaging, so the work around is to disable source control under Edit -> Project Settings -> Editor before doing a build. Be nice if this was fixed so that step was unnecessary though...
UPDATE:
Um, reconnecting to asset server makes it think everything locally has changed, so this is a really, really sucky workaround
Found a stupid workaround because I was having the exact same problem, only tested on OSX since it relies on executing a python script via the hashbang line.
Go find the path where aapt is in your android SDK (android-sdk-macosx/build-tools/17.0.0/aapt for example) and rename it to aapt.real
Make a new file called aapt and put the following in it:
#!/usr/bin/env python
import os
import sys
os.execv(os.path.join(os.path.split(__file__)[0], 'aapt.real'), ['aapt'] + sys.argv[1:] + ['--ignore-assets', '!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~:!*.meta'])
Wonder why Unity doesn't include the --ignore-assets option to fix this problem.
i added the line
<property name="aapt.ignore.assets" value="!*.meta:!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" />
to the build.xml file of the facebook android unity sdk. no need to patch android sdk.
Another workaround that seems to work is to delete all the .meta files from the res folder after starting the build, but before unity starts the packaging process. Unity will not recreate the meta files while it is building the project. This way you don't have to disable and re-enable version control.

Does Eclipse always run Proguard on Android release builds even if project.properties doesn't have a proguard entry?

My app runs differently when run from Eclipse in debug mode, or when run from an exported signed application. I have not been specifying any proguard config in project.properties. Am I still getting some default proguard optimizations and obfuscations? Does proguard always run when exporting a signed binary using the generic config in the tools directory of the sdk?
Nope. You have to enable it. It's not enabled by default. http://developer.android.com/tools/help/proguard.html
To enable ProGuard so that it runs as part of an Ant or Eclipse build,
set the proguard.config property in the
/project.properties file. The path can be an absolute
path or a path relative to the project's root.

Run batch file after each build in eclipse

I have a number of projects that I work on simultaneously.
Every time I build and run one of them, the apk is located as usual in the bin folder.
If I want to copy this apk to some other folder outside the project, I have to do it manually.
I created a single batch file that copies all my projects' apk files to the desired location.
Is there a way to change the output location of the bin folder to somewhere outside the project or more preferably run the batch file after each build?
NOTICE
I am using eclipse with ADT. I have tried to add a builder that executes the batch file. However, when the batch file is run, the apk file is not yet generated. I tried all combinations of options in the builder and all the possible of sequences of builders.
If you are happy just to use Eclipse whilst you are perfecting the build, then switching to the command line for the final build, then with Ant it's really easy to get what you want with very little effort or configuration.
Assumptions
1) Your sources are in an Android workspace and you will end up with two sets of binaries - one made by Eclipse, the other made by Ant will end up outside the workspace as set by a PROPERTIES file
2) You are using SDK14 or 15 (Ant changed in 14)
3) You have Ant installed and in your path - you'll need to have Ant 1.8.2 - this is not the internal one that Eclipse uses, you may have to get it from the Apache site, it's easy to install
Steps
1) Make a sample project from the command line as described in http://developer.android.com/guide/developing/projects/projects-cmdline.html
For example I used:
android create project --target 8 --name Sample15App --path c:\dev
\projects\samples\Sample15 --activity Sample15Activity --package com.me.samplefifteen
This will make a directory and some files which you will use later as a template in your projects
2) Make a sample project in your workspace from Eclipse, I made EclipseSample in one of my workspaces
3) Copy the following files from Sample15App to the root of your EclipseSample project:
build.xml
ant.properties
local.properties
4) Edit ant.properties (which is initially empty) to be like this example:
projectname=EclipseSample
workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
base.dir=${workspace.dir}/${projectname}
outbasebase.dir=/dev/projects/AntBuilds
outbase.dir=${outbasebase.dir}/${projectname}
ant.project.name=${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout
source.dir=${base.dir}/src
From this you can see that my workspace is /dev/projects/EclipseIndigo/AndroidWorkTwo
The eclipse project under this is in directory EclipseSample
I want my apks to end up in /dev/projects/AntBuilds/EclipseSample (i.e outbasebase concatenated with projectname -so for other projects you can use a very similar ant.properties file just change projectname)
5) IMPORTANT - EDIT THE build.xml
Comment out or remove the line :
<project name="Sample15App" default="help">
replace it with just
<project>
This just means it will pick up the project name from ant.properties rather than the build.xml and you can use the same build.xml in all your projects, only ant.properties need change
6) try it with "ant debug" should build the debug apks under /dev/projects/AntBuilds/EclipseSample
7) finally if you want to automate the release build (signing and password entering automatically) add lines like
key.store.password=YourPassword
key.alias.password=YourPassword
key.store=c:/users/you/yourrelease-key.keystore
key.alias=release_alias
to the ant.properties and then you just type "ant release"
If you don't add them it will tell you to sign manually and as no password entries were found in 'build.properties' (- that was what ant.properties used to be called pre SDK 14, they should have corrected this!)
You can add new Builders to your project using Project Properties->Builders->New....
One way to do this is to create an ant build (which copies the file), or just a plain old script or batch file.

Managing android projects from command line

I am managing and running my android app from command line without using ant, I followed these steps:
generate R.java
compile R.java and all .java files in src to .class files
assembling set of class files into dex file using the command below
dx --dex --verbose --output=./bin/classes.dex ./bin
.class files are in bin directory.
But I'm getting the following errors in these steps:
java.lang.Runtime exception:.\bin file not found
at com.android.dx.cf.direct.ClassPathOpener.process
at com.android.dx.command.dexer.Main.processOne
at com.android.dx.command.dexer.Main.processAllFiles
at com.android.dx.command.dexer.Main..run
at com.android.dx.command.dexer.Main.main
at com.android.dx.command.Main.main
Due to this, I'm unable to create the Classes.dex file.
Can someone suggest a solution for this?
[not using eclipse and ant only through command line]
If you need to "manage your Android projects from command line", when you should use Ant build.
Ant's build.xml is a official standardized way to build Android projects. Ant scripts can do anything you may need to build your project.
If you want most modern build tools for Android, you can look at Gradle for Android projects. Note: today it's still in alpha stage.
Try entering the full path instead of the relative path.
Also you must put the class files inside a directory named exactly like it's package name. for example for com.test.me.MyActivity you must use com/test/me/MyActivity.class
And since we are on the topic, remember that dx can only work with class files created using Java6 (or less) so if you are using java7 to compile your code, add "source 1.6 target 1.6" parameters to your command line.

Categories

Resources