Running a script during build of android app - android

I wanted to now if it's possible to run a .jar program or another script when building an android application or if somebody could guide me to an example.
I would want to sort my strings.xml file when I build a release.
probably what I forgot to say that I'm using gradle.

You can define your own task of type Exec and make the assembleRelease task depends on it:
task executeScript(type:Exec) {
println 'Executing script...'
commandLine './script.sh'
}
gradle.projectsEvaluated {
assembleRelease.dependsOn executeScript
}

You can use ANT tools to build your application and use shell scripts to sort your strings.xml.

Related

Gradle Exec task running from command line build but not from Android build

I am attempting to execute a python script from a Gradle task of type Exec. My main build task has a dependency on this task. Let's for this example call my build task assembleProjectDebug. Let's simplify the example also to this:
tasks.create(name: newFooTask, type: Exec) {
commandLine "python", "doSomething.py"
}
tasks[assembleProjectDebug].dependsOn(newFooTask)
If I run ./gradlew assembleProjectDebug from a terminal, my python script executes and all is well in the world. However if I attempt to build from Android Studio with Build -> Make Project, my python script will not execute.
I can confirm as well that my currently selected build variant is set to projectDebug, and can also confirm that when building from Android Studio, :app:assembleProjectDebug appears in the log leading me to believe that assembleProjectDebug task is or will be executed (I could be wrong there).
For the sake of brevity I have also left out where I assign an environment variable in my task which points my $PATH variable to the location of my python exec.
Is there any reason why, outside of me messing up the assignment of a proper $PATH variable for the subprocess, that a command line build will execute this task, yet a build from within Android Studio will not execute this task?
It's likely you'll need to set the workingDir on the Exec task
Eg:
tasks.create(name: newFooTask, type: Exec) {
commandLine "python", "doSomething.py"
workingDir = projectDir
}
I found a solution to my problem. Building from within Android Studio, my task dependency was not being executed as it was when building from the command line. This was due to having Configure on Demand selected in Android Preferences -> Build, Execution, Deployment -> Compiler. Android Studio applies this setting by default. You can read more about Configure on Demand here:
Configure on Demand in Gradle

android studio use sync project with gradle files action

I want to use sync project with gradle file in command line but I dont know how to do
like this :
It besides do ./gradlew build also do other things
For example, in a file is written to a local library name android-test
than in settings.gradle read this file and call include ":${android-test}" .if I use AS sync project with gradle files button,AS can load
module to project , but when I run ./gradlew build, it doesn't work.
But I have developed a plugins, and I want to use this action, so how to call this function ?
Building and syncing from the command line is really simple. In the root of your project you can invoke the gradle wrapper's build command with
./gradlew build
To get a list of all available build tasks for your project:
./gradlew tasks
"./gradlew build" and "./gradlew tasks" in the path of your project.
more commands and steps here:https://developer.android.com/studio/build/building-cmdline.html
The action id is Android.SyncProject, so you can call it in your plugin:
ActionManager.getInstance().getAction("Android.SyncProject").actionPerformed(e);
You can find it in android-plugin.xml, maybe line 161.
Also in SyncProjectAction.java, line 36 you can find its text.

How to execute an Ant target task in build.gradle?

How can I get the task associated with an Ant target to execute from within the build.gradle file?
The part in build.gradle which imports the build.xml file:
def targetSdk = android.defaultConfig.targetSdkVersion.getApiLevel()
ant.properties['android.version'] = "android-$targetSdk"
ant.properties['sdk-location'] = SDK_DIR
ant.importBuild(rootDir.absolutePath + "/bcproj/build.xml")
The ant target I want to execute is named compile-android. It successfully runs at the command line with:
./gradlew compile-android
But I'd like to include this in build.gradle so I can specify which ant target needs compiling for each buildType. In all my googling, all I could find references for is getting it to compile at the command line.
Update 1
I've tried adding the compile-android task as a dependency to another task that gets executed along with the build process. Though conceptually the two are really independent of each other. This still failed in getting compile-android to execute.
task copyShopMetadata(type: Exec, dependsOn: 'compile-android') {
workingDir rootDir
commandLine './copy_shop_metadata.sh'
}
copyShopMetadata.execute()
I've tried reversing the dependency, still doesn't work.
Task t = tasks.findByName('compile-android')
t.dependsOn(copyShopMetadata)
copyShopMetadata.execute()
Update 2
I should note that adding the dependency examples above does work when executing from the command line. However my end goal here is to be able to hit "sync project with gradle files" in Android Studio and have the the compile-android ant target compile. Perhaps this just isn't possible?
Assuming your task is not intended to be independent of all other processing, you're going to want your task to be depended on by some other task, which means that when you try to run that other task, your task will be executed first. That task might be "build" for instance, so you might do something like this:
project.build.dependsOn("compile-android")
(I'm not certain of the exact syntax for this.)
Ok, looks like the problem was a misunderstanding on my part for what the "Sync Project with Gradle" button was doing. It is not the equivalent of executing a build from the command line. It's not until I actually hit the run button in Android Studio does it perform the compile-android task. As hitting the run button is what's equivalent to the command line.

Cannot execute a shell script from gradle file

I am using TeamCity as my CI server. I have a build.gradle file for an android project which contains
android{project.afterEvaluate{
task packDex(type:Exec) {
....
commandLine './script.sh'
.....
}
}
On building my project I am getting a org.gradle.process.internal.ExecException error. How can I solve this. It is building in Android Studio but not in TeamCity. I am using gradle wrapper to build my project.Please help me out.
EDIT:
My simple doubt is whether the command commandLine './script.sh' is correct to start a shell script from a build.gradle file.I am using Mac as my server and using gradle 1.0.0
I managed to work this one out myself.Just use the following piece of code.
android{project.afterEvaluate{
task packDex(type:Exec) {
....
commandLine 'sh', './myScript.sh'
.....
}
}

Android build variants on travis.ci

I currently have an Android project using gradle and integrated with travis.ci which has different productFlavors and buildTypes. When the "connectedCheck" task is then executed on travis.ci, it tries to package all build variants (all combinations of flavors and types). Some of them fail as the release builds need password input which I can't automate at the moment. Is there a way to tell travis.ci to build and test only a certain build variant of an Android project?
Say you only want to run the product flavor Trial and the build type Debug.
Instead of running ./gradlew assemble connectedCheck, which is similar to what you're doing, run this instead:
./gradlew assembleTrialDebug connectedCheckTrialDebug
So here's how I made it work:
Run a connectedAndroidTest<productFlavor><buildType> task instead of connectedCheck.
Also set the assemble task in the install section of the .travis.yml:
install: - TERM=dumb ./gradlew -s assemble<productFlavor><buildType>

Categories

Resources