Cannot execute a shell script from gradle file - android

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'
.....
}
}

Related

React Native Android build failing with 'compileDebugJavaWithJavac' on several packages

I've been working in the same React Native project for months and recently the Android build has started failing. To my knowledge I haven't changed anything about my development environment and I haven't updated or changed any packages or dependencies that the project uses.
Task :#react-native-community_async-storage:compileDebugJavaWithJavac FAILED
Task :#react-native-community_datetimepicker:compileDebugJavaWithJavac FAILED
Task :#react-native-community_toolbar-android:compileDebugJavaWithJavac FAILED
Task :#sentry_react-native:compileDebugJavaWithJavac FAILED
Task :#react-native-community_netinfo:compileDebugJavaWithJavac FAILED
I've done all of the following that I found from other questions with a similar problem
Synced the project with gradle files in android studio
Ran gradlew clean in the android directory which does succeed, but the build still fails
Checked that android.useAndroidX and android.enableJetifier in the gradle.properties file are both true
Checked that I have implementation "com.facebook.react:react-native:+" in my build.gradle file. I tried changing it from + to the actual version of react native that I'm using but it made no difference.
I'm not an expert mobile developer by any means so I'm hoping that I'm missing something obvious, but any help or suggestions would be greatly appreciated.
I had been struggling with the same issue for weeks. Android build just stopped working in November. Just found the solution here (phew!) https://github.com/facebook/react-native/issues/35210
On November 4th 2022 React Native version 0.71.0-rc0 was published but this event resulted in build failures for Android on several users as they ended up downloading the wrong React Native version (0.71.0-rc0 instead of the version they were using in their project, say 0.68.0).
See all the detail in the link but the solution for me was to add the configurations.all section below to the android/build.gradle file
allprojects {
repositories {...}
configurations.all {
resolutionStrategy {
// Remove this override in 0.65+, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:0.61.5"
}
}
}
For good measure I then also did the following
$ rm -rf node_modules
$ cd android
$ rm -rf .gradle
$ cd ..
$ npm install
$ react-native run-android
Hope this helps someone else as I was really struggling to fix this one.

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.

Building Android Studio project on Jenkins? android.compileSdkVersion is missing

I'm trying to set up my first Android project to build on Jenkins.
I'm running Jenkins 1.6.2 with version 1.24 of the Gradle plugin. Running on Windows 7 Professional SP1.
I've installed Android Studio and the Java7 JDK on my build machine, and a checked-out version of the software builds just fine, through Android Studio or when running gradlew.bat from the command line. But I can't get the Invoke Gradle script build task to work, within my Jenkins job.
There is a pair of radio buttons, on the configure project page:
Invoke Gradle
Use Gradle Wrapper
If I select "Invoke Gradle", I'm asked to select a "Gradle Version", which is something I setup in Configure System.
I created a gradle installation with GRADLE_HOME set to "D:\Program Files\Android Studio\gradle\gradle-2.2.1". That gives me a warning that "D:\Program Files\Android Studio\gradle\gradle-2.2.1 is not a directory on the Jenkins master...."
And when I run a build, I get an error "Can't retrieve the Gradle executable".
Which is probably related to the gradle plugin complaining about the directory. But the directory is correct. I've tried it with '/' instead of '\', and it made no difference.
Since that didn't work, I tried the alternative, "Use Gradle Wrapper". There's a checkbox: "From Root Build Script Dir". Whether I check it, or not, I get "java.lang.IllegalArgumentException: android.compileSdkVersion is missing!"
I've set both JAVA_HOME and ANDROID_HOME, so that's not the issue.
Any ideas?
As a followup, I delete the gradle task, and added an Execute Windows batch command task:
SET ANDROID_HOME=d:\Program Files\Android\sdk
SET JAVA_HOME=d:\Program Files\Java\jdk1.7.0_79
.\gradlew.bat clean
When I run that, I still get that error.
But when I run those commands from a command line on the build machine, they work just fine.
What could be different, when running gradlew.bat from Jenkins, than when running it from the command line? The Jenkins service is configured to use the same user as I'm logged in as, when I'm running from the command line. I've tried explicitly setting each and every environment variable I have set in the command line, in the Jenkins task, and I'm still seeing the error.
Any ideas?
Your local.properties file must contain:
sdk.dir="path to your android sdk"
I'm also using jenkins for building my apps and had a similar problem.
I fixed it by executing the gradle wrapper via jenkins without using the jenkins gradle plugin.
cd $WORKSPACE
./gradlew assembleRelease
And if you want lint add a second shell exec with
cd $WORKSPACE
./gradlew lint
Note that these commands are for a linux system but they will work just fine on windows (had a windows build server some time ago and did the same thing)
I met this problem too, but I don't know whether my solution suit for you.
My situation is below, in my project build.gradle file:
apply plugin: 'com.android.library'
apply from: 'maven_push.gradle'
dependencies {
......
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.0"
......
}
When i run: gradle clean build, error message show:
* Where:
Script 'C:\xxxxxx\Project\maven_push.gradle' line: 32
* What went wrong:
A problem occurred evaluating script.
> android.compileSdkVersion is missing!
My solution is change the position of this script apply from: 'maven_push.gradle' to the bottom in build.gradle file, and BUILD SUCCESSFUL!:
apply plugin: 'com.android.library'
dependencies {
......
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.0"
......
}
apply from: 'maven_push.gradle'
I think you should pay attention to the "Where" message in the error command line.
I can't be sure it's gonna solve your issue, but maybe you can consider using the sdk-manager-plugin which downloads and installs the android sdk directly from gradle.
If it works, it probably won't tell you why your current configuration is not working, but at least you won't be stuck anymore.

Running a script during build of android app

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.

Categories

Resources