Building android app via ant won't work from batch file? - android

I'm trying to build my android app via commandline (batch file to be specific). I'm working on a phonegap/cordova app and I find it to be a bit of an overkill to use Eclipse only for compiling.
If I try it via commandline it works. But if I run it in a batch file it looks like ANT is using JRE instead of JDK??
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre7\lib\tools.jar

If you want to use javac in ant then you need tools.jar to work it properly. You can find this answer on apache ant site (When you need JDK functionality (such as for the javac task or the rmic task), then tools.jar must be added. The scripts supplied with Ant, in the bin directory, will add the required JDK classes automatically, if the JAVA_HOME environment variable is set.).

Not sure why this happens. But you would usually find tools.jar in C:\ProgramFiles\Java\JDK\lib
Copying that to the C:\ProgramFiles\Java\jre\lib folder would make it work without any change to the JAVA_HOME

Related

How to make Android Studio STOP generating local.properties?

I have set an environment variable ANDROID_HOME, which points to my android SDK location. However, every time I open my project in Android Studio it places local.properties file to the root of my project and copies my SDK location from ANDROID_HOME to this file. I want Android Studio use ANDROID_HOME directly, without generating local.properties.
Why? I'm on Windows 10 and I use Bash for Windows. I want to run command line using Bash while running Android Studio from Windows. For this I need two ANDROID_HOME environment variables, one for Windows, one for Bash. This is because although they point to the same (windows) android SDK the format is different, for Windows it is C:\Users\me\Android SDK while for Bash it's /mnt/c/Users/me/Android SDK. When Android Studio creates local.properties with windows path Bash becomes broken since apparently local.properties takes advantage over ANDROID_HOME, so I have to delete it manually.
You might find this article useful:
Posix path conversion (mingw.org)
The gradle android plugin "Application" extension
(your regular gradle-android interface) has a property
sdkDirectory. Perhaps this can override it? I can't check.
I think there is a configuration switch for bash on Windows to parse path separators Windows-like, can't seem to find it, it is buried deep inside MinGW or MSYS docs I think.
Last resort - git Hooks. Read the ProGit book chapter on Hooks.
The Android will generate this file always when gradle sync it. But if you delete it and define ANDROID_HOME and ANDROID_NDK_HOME environment variables, every time that generates it will read ANDROID_HOME and ANDROID_NDK_HOME.
If you want to build your project in many computers with different home folders, just keep the local.properties outside the source version control.

Modified PATH environment variable in Gradle script

I am running Gradle script. At the beginning I print PATH environment variable:
println System.getenv( 'PATH' )
The output is:
/usr/bin:/bin:/usr/sbin:/sbin
But the real PATH environment variable is (printed from terminal):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sh........
Question is, why that PATH env variable do not math in Gradle script and in terminal?
Thing is, that I am building Android, which use Gradle as build system. And I have c++ code, which is build by CMake. And in CMake i run some python scripts, which executes some external commands. But when Gradle changes PATH variable, those commands are not found and I am getting an error. It is not an option to use full path, when executing those commands, like:
/usr/local/bin/my_mega_command
When I run CMake or python script directly, then everything works as expected.
Android Studio does not pass its environment down to the Gradle daemon that it spawns. Usually, we see this in the form of missing custom environment variables, but apparently it affects PATH changes as well.
I had the exact same issue where running gradle tasks from the Terminal (inside or outside Android Studio) would correctly take the PATH set in the environment (i.e. via .zprofile or any of the other possible methods), but not when running via the Gradle commands from the Android Studio Toolbar. What ended up working for me was changing the JDK version used from the one that came with Android Studio (JDK 11) to JDK 1.8, and suddenly it worked. I don't know why this changes anything, but now the PATH is taken from the environment set in the system and everything works.

How to build Android project from the command line on Windows

I've got an older Android project (still using Eclipse) that I need to build from the command line. It just builds a library (.jar file), not an application (.apk file). I installed ANT, but since there's no build.xml file it doesn't do anything.
All the examples I've found say to use the "android" command to generate build configurations, but I do not have the "android" command on my PC and searching on "download android command" didn't product any useful results.
Where does one get the "android" command? Do I actually need it in my case, or will a simple generic build.xml file of some kind do the trick?
The answer is simple but not exactly easy to find (IMHO), thus this posting.
By default, the Android SDK adds its "platform-tools" sub-directory to your path but the "android" command is in the "tools" sub-directory. So one just needs to manually edit the PC's PATH environment variable to add the "tools" directory.

Building OpenCV 2.4.5 in android ADT: "ndk-build" not found in PATH

I'm trying to follow this OpenCV document to import OpenCV sample projects into the android ADT, and am getting the now infamous "Program "C:\android\android-ndk-r8d-windows\ndk-build" not found in PATH" error. My OS is Windows 7 and I use cygwin for any Linux-like commands.
Please note: I already downloaded the NDK package, and already set the value of NDKROOT to be my NDK root folder. I setup NDKROOT both as a system-wide environmental variable, and as an ADT's C/C++ Build environment variable, as shown below:
In my NDK root folder, I made identical copies of the file "ndk-build.cmd", and renamed them as "ndk-build" and "ndk-build.sh" respectively. I chmod+x to all three ndk-build* files.
Now what happens is really puzzling: If I specify the C/C++ Build command to be ${NDKROOT}/ndk-build or ${NDKROOT}/ndk-build.sh, I get the error "Program "C:\android\android-ndk-r8d-windows\ndk-build" not found in PATH". However, if I specify it to be ${NDKROOT}/ndk-build.cmd, ADT apparently finds it, but tries to run it as a Windows command, not as a bash script, as shown below:
The only way I can build a project, is to open a cygwin terminal, cd to the sample project folder, and run the script (either ndk-build or ndk-build.sh), like this:
I googled around for a solution. Although there are lots of "ndk-build.cmd not found in PATH" questions, I have not seen a case like mine. What is happening? I might also post my question to the openCV forum.
Apparently, the command that your Eclipse tries to run (when using the ${NDKROOT}/ndk-build.cmd version) is C:\\android\\android-ndk-r8d-windows\\ndk-build.cmd, which is apparently correct.
The problem is that your ndk-build.cmd is not a windows command file. And it should be. I just downloaded the Windows version of the ndk-r8e and the ndk-build.cmd file is copied here.
Try to download the ndk again or to modify your cmd file.
you should add the ${NDKROOT} variable (which is the folder in which the NDK is located) the same way you did with the PATH variable

Jenkins And android Build

I am trying to automate the android build process using Jenkins
I am using the following site to achieve this
Link to site
I am successfully able to Build it on my own machine. Initially i got error saying that build.xml file not found (build using ant requires this file). So i execute "android update project". So it automatically generate all necessary file for ant.
Now my problem begins when i tried to host my repo to remote server and tried to build from another machine. The error it is giving is the sdk.dir is not correct. When i analysed the project folder ther is a file called local.properties which contains the SDK path of my first machine which is wrong for other machines. So i add that file to gitignore. Now that file is not tracking and because of this the build is failing.
So is there any way to automatically generate the files that is necessary for ant after jenkins is cloning project from the remote repo?
From the website you link to, they precisely explain how to configure sdk.dir:
Configuring the environment
When jenkins builds your project with Ant, it needs to know where your
android sdk folder is. To do this, click Advanced on the Ant target
build step you just configured and add the following variable to the
Properties field. sdk.dir=/opt/android-sdk-linux/
Don’t forget to substitute the value of the variable with the correct
location where the Android SDK is installed on your build server.
Job configuration is done. Click Save. Time to test your build.
We also build android apps with Jenkins, and also need to edit the local.properties.
In my case, I have the file updated with the path to SDK by the Jenkins build itself. Just before the build starts.
You can use a simple sed command on linux or echo the content on windows (overwriting content).
Example flow:
SCM - get sources
Edit the local.properties (as suggested before)
Run ant build
Note - if you are using the "Invoke Ant", you should add an "Execute Shell" step before to deal with the editing of the local.properties.
I hope this helps.
Yes.
If you install the Android Emulator Plugin for Jenkins, you can add the "Create Android build files" build step to your job.
This will automatically detect any Android app, test or library projects in your Jenkins workspace and add/update the build.xml and local.properties files as necessary.
Alternatively, if you're using the Ant build step and already have the build.xml in your repository, you can ignore the need to create a local.properties file, by specifying the sdk.dir property yourself in the Advanced Ant options.
You can configure ant properties in jenkins. So you can specify all properties of your local.properties through the jenkins job configuration.
Of course you will have to install the android sdk on the jenkins build server.
Please read this for info on how setting ant properties with jenkins.
It's not a good practice to put the local.properties under source code management since multiple developers and CI will have different values for those properties.
you can edit the local.properties file in the jenkins workspace folder to the correct sdk path
I think you want to add the build step "Create Android build files" to your configuration. Place it before the ant build.
This invokes the android update project and android update lib-project. Make sure you referenced your library dependencies in the project.properties with relative paths. Like this:
# Project target.
target=Google Inc.:Google APIs:19
android.library.reference.1=../external-libs/google-play-services_lib
android.library.reference.2=../external-libs/android-support-v7-appcompat

Categories

Resources