I have migrated my project from eclipse to android studio successfully and a default build.gradle file has been generated. The project builds correctly and I can deploy to debug etc.
The real problem though is building release APK files (from the command line) for which I used to have a custom ant-build (called via command line, not out of eclipse).
My custom build.xml imported the standard sdk build.xml file from the SDK folder via:
<import file="${sdk.dir}/tools/ant/build.xml" />
So all I had to do in my build.xml was to override targets or hook into them via "depends".
An example for an override:
<target name="-set-release-mode" depends="-set-mode-check">
<!--Here I rename my output apk files to something like: myapp-versionFromAndroidManifest.apk -->
</target
And an example for adding dependency:
<target name="-pre-build" depends="clean">
<!-- my custom code where I copy resource files, process command line arguments
use xpath to extract things like the version from the AndroidManifest.xml and
write them into custom xml files etc... -->
</target
So overall the whole affair was really simple using ant. But now when it comes to migrating to gradle I am at a total loss, how to accomplish the same that I previously did in ant, specifically:
For the build.xml there is a default build.xml that I imported in my ant build - does a standard build.gradle file exist somewhere so I can check out the defined tasks?
Is it possible to override gradle tasks and if yes: how?
I am pretty certain the depends="..." from ant can be mimicked in gradle but I have no idea how and that would also require an answer to question 1.
I googled a bunch of tutorials, also migration tutorials like (which unfortunately didn't address my issues):
http://keepsafe-engineering.tumblr.com/post/87818767721/migrating-android-app-from-ant-to-gradle
http://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/
I also read this chapter from the gradle docs which didn't really help at all, because it does not answer question 1 or 2 or 3 for that matter:
http://www.gradle.org/docs/current/userguide/ant.html
The following gave some interesting pointers though:
http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/
I also tried simply copying the default build.xml from the sdk folder into my own build.xml and then simply importing that ant build file in gradle and executing the ant task that kicks of the build:
<target name="release"
depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
description="Builds the application in release mode.">
</target>
but I started running into problems that suggested this is not an option anymore, since the default build.xml started throwing errors, that seemed related to the SDK version. And I had to mess around a lot with the original sdk build.xml because of the new android studio project structure, so files were not where they were expected etc...
First, I would suggest you import the legacy project to android
studio. It will create the appropriate build.gradle script for you.
And then, you can list the task list by executing gradlew tasks at
the root of your project.
To depend on some tasks, you can use command such as init.dependsOn anotherTask.
Just for the record: I dumped the idea of using my previous ant-build and completely migrated to gradle. Most of the stuff I needed to do could be achieved just by the directory structure. You can find details on how I did that in my other question:
Android Studio: Gradle Product Flavors: Define custom properties
Now the second part was to call my java code that did some stuff I really did not want to migrate to groovy/gradle. Thus I figured out how to execute my java class (albeit I actually had to make a jar file out of it) from the gradle script:
Android Studio Gradle: Execute static Java Method (Migration from ANT to Gradle)
If anyone has questions, I'll be happy to try my best and answer them.
Related
I would like to know if there is a faster way than the following steps to test Unity app with Android (gradle export):
Change something in unity
Build settings & Run --> Export (new Gradle)
Now the old gradle in the Android project is replaced, so i need to copy paste from a backup
The Res folder is also replaced, so I need to copy paste the backup one
Finally Build and Run the APK on the device
Steps 3 and 4 are obviously what makes this process painful (slow), I found the manifest.xml of the Unity android plugin, so this one stays the same (edited it directly in Unity). So am I doing something wrong ? Is there a way to update only parts of the Android project (like, if I only changed one line of a C# Script, just rebuild this one for Android)
Tell me if I'm not clear enough with my explanation :)
EDIT: I Already posted this in Unity forum but I didn't get any answer yet after a day
You can create your own Gradle file.
Put it in Plugins/Android folder and call it mainTemplate.gradle
It will use that.
As far as I know, there is no way to rebuild just part of it.
Unity does not create java files from the c# code. It creates its own modules and uses them directly.
BTW, why are you exporting at all? Why not compile directly to your device?
I just wanted an additional answer for this question even though it is a bit old (just in case anyone else in the future is wondering about this like me). I personally need to make use of both Android Studio and Unity so making a plugin wasn't really an option for me.
Now I have a custom gradle file as explained in the accepted answer. However I have also added an additional resource folder (Explained well by Android here: https://developer.android.com/studio/write/add-resources.html).
You basically just have to edit your custom gradle file to add paths to both resource directories. Add the resources you don't want Unity to overwrite to the other resource folder.
Now every time you rebuild to the project the resources remain and you don't have to copy them back in.
You can export a formal Android project first, using this project to build your final apk.
Then You could write an EditorUtil script in you Unity project, The script does following things:
1.export an temporary Android project to another folder.
2.copy assets folder and jnilib folder or any other folders you want to replace in your formal Android project.
Everytime you want export your project, just trigger the export function in you Unity Editor, It will copy and paste folders automaticlly, then your formal project will be ready for building.
For different purpose, I need to specify another gradle file (not default build.gradle file) to build android project in android studio standard structure. When using eclipse with gradle, I know how to build project with specified name gradle file. Since the structure is a little different. How to make it work? Does anybody know it? Thanks
More details to be explained.
I have an Android Studio standard project structure as below.
->Root
-->app
-->settings.gradle
-->build.gradle
-->Project1 (library)
-->Project2 (library)
-->build.gradle
-->settings.gradle
I want to create a new gradle file to build project to generate apk file. This gradle file has some tasks than the default one (build.gradle).
So I can choose one of them to use for different purpose.
For example, for A purpose, I can execute 'gradle assembledebug' to generate apk file.
for B purpose, I can execute 'gradle assembledebug -b b.gradle' to generate apk file.
I have done some research on this, but failed.
I have just set-up my local jenkins installation on my mac...
Now i have tried to create an ant built which builds my app as soon as i check-in a new change. The ant script is working great as long as i only use it in the project...
cd into the folder
ant release
It is able to find all library projects i'Ve included (ActionBarSherlock, ViewPagerIndicator, NineOldAndroids, HoloEveryWhere, PocketChange)
As soon as i put this on my jenkins installation everything breaks.
My main folder-organisation is like this:
Projects/Project
Projects/ProjectLibrarys/Library
Eclipse referes to those libraries like this: android.library.reference.2=../Project Librarys/ActionBarSherlock/library
I can't use absolute links (because i think they will fix the issue) but eclipse or ant doesn't like them.
I have really no idea how to fix it?
Will i have to edit the build.xml (i've added it into the folder ext/commonbuild/commonbuild.xml so i have the same android build file for all my projects)
If you need more code let me know.
Thanks in advance...
Ok i was able to solve my issue by adding a customproject.properties file with a seperate link for the build.
There is still one issue. It seems that ant doesn't use the customproject.properties for my library project i've added. This library project includes another library project and as soon as i build it has a wrong relative url.
Does someone know how to use the customproject.properties even for the project libraries?
As bluszcz mentioned, I checked out two modules and i was able to build my apk via Jenkins without having two project.properties or customproject.properties file.
On Jenkins Configure Screen, Under Source Code Management - Subversion Modules,
Give the url for your android project (say, SampleAndroidProject)
https://goxxx.com/svn/repository/projects/trunk/SampleAndroidProject
Provide local module directory as 'SampleAndroidProject'
Provide the svn url for your library project (say, google-play-services_lib)
https://goxxx.com/svn/repository/projects/trunk/google-play-services_lib
Provide local module directory as 'google-play-services_lib'
Save and build again.
I am running the standard ant script build.xml which gets created when you run the android create project command. In order to verify my local.properties is set correctly, I added a task at the beginning of the build.xml script to run the command:
android update project -p .
I now get the following message each time I run the ant script, which clobbers my build.xml file and creates a proguard.cfg file!
File build.xml is too old and needs to be updated
So, I moved the ant script to a different file that won't get clobbered.
Is there a way to run the command android update project -p . that doesn't clobber build.xml and create proguard.cfg?
I think this is a bug. I have registered a new bug with Google here:
http://code.google.com/p/android/issues/detail?id=17825
If anyone else thinks it's a bug, then please star it and perhaps contribute your thoughts.
Workaround for this. Add the following code in build.xml.
<!-- The following will prevent for 'android' tool to overwrite this file.
(until sdk r12)
classname="com.android.ant.SetupTask"
(since sdk r13 FIXME)
version-tag:custom
-->
With the piece of comment, 'android update project' will not complaint or overwrite the build.xml.
As of now, there might be chance for changes in r13.
sdk r13 not yet released
it's based on aosp/master branch
updateProject() in package com.android.sdklib.internal.project.ProjectCreator determine to update the build.xml or not.
(The code from sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectCreator.java)
The build.xml generated by android has the following comment:
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
so, it looks like Atham is right.
I am following the instructions to build Scala app for Android at:
scala-to-android
Basically, I installed Scala and changed the ant build.properties to point to Scala and changed build.xml to include build-scala.xml.
ant scala-compile
is successful, However,
ant debug
generates the following build errors:
scala-compile:
-shrink-if-test:
[echo] Checking if Scala libraries are installed on emulator or device...
[adb] BOOTCLASSPATH=/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar
-shrink-config:
[taskdef] Could not load definitions from resource scala/tools/ant/task.properties. It could not be found.
BUILD FAILED
/home/salil/Programs/android-projects/FirstScala/build-scala.xml:82: Problem: failed to create task or type invoked
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
I am a beginner in ant, Android and Scala. Any help would be appreciated. Thanks so much in advance!
I faced the same issue with android dev on Scala. Actually the build script looks for a file named task.properties which is located in scala/tools/ant/task.properties. This is packed inside the jar ant-invoked.jar. I hope you would have downloaded the android-sdk project zip archive from that site. Just copy the folder "configs" from the android-sdk root into your project root folder, this will work.
This is were it searches for the file in scala-build.xml
<!-- Project settings -->
property name="configs.dir" value="${basedir}/configs" />
<property name="ant-invoked.jar" value="${configs.dir}/ant/ant-invoked.jar" />
and the target shrink-config
<target name="-shrink-config"
description="Generate ProGuard configuration file">
<taskdef resource="scala/tools/ant/task.properties"
classpath="${ant-invoked.jar}" />
Currently, the most stable way to use Scala with android is to use the Simple Build Tool (SBT) and the Android Plugin
It basically "just works", handling all the steps for you and shrinking with Proguard.
Hi I have a blog here that documents steps to make scala work on android. It makes use of android ant process and sticks close to the android way. You can see the tutorial here
http://saadstechblog.blogspot.com/2011/09/scandroid-scala-android-tutorial.html. There is also a github project with all the configuration.