Skip to content

Makes CLI build play nice along with Android Studio#224

Closed
saharshtibrewal wants to merge 1 commit intoapache:masterfrom
saharshtibrewal:patch-1
Closed

Makes CLI build play nice along with Android Studio#224
saharshtibrewal wants to merge 1 commit intoapache:masterfrom
saharshtibrewal:patch-1

Conversation

@saharshtibrewal
Copy link

Recently, I had had an issue after opening my cordova project with Android Studio whereby the cordova build android command would constantly give me this error message.

Running command: /Users/Developer/app/hooks/after_prepare/010_add_platform_class.js /Users/Developer/app
add to body class: platform-android
Running command: /Users/Developer/app/platforms/android/cordova/build
ANDROID_HOME=/usr/local/Cellar/android-sdk/24.3.4
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Running: /Users/Developer/app/platforms/android/gradlew cdvBuildDebug -b /Users/Developer/app/platforms/android/build.gradle -Dorg.gradle.daemon=true
:preBuild FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':preBuild'.

    failed to find Build Tools revision 23.0.1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.131 secs

/Users/Developer/app/platforms/android/cordova/node_modules/q/q.js:126
throw e;
^
Error code 1 for command: /Users/Developer/app/platforms/android/gradlew with args: cdvBuildDebug,-b,/Users/Developer/app/platforms/android/build.gradle,-Dorg.gradle.daemon=true
ERROR building one of the platforms: Error: /Users/Developer/app/platforms/android/cordova/build: Command failed with exit code 1
You may not have the required environment or OS to build this project
Error: /Users/Developer/app/platforms/android/cordova/build: Command failed with exit code 1
at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:139:23)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

Even though, I had all the build tools installed and everything, this issue started happening with the latest version of Android Studio. After a lot of debugging and almost giving up.

I found that the issue was happening due to local.properties file generated by Android Studio and the sdk.dir being specified in it. This change to the build script is not a permanent fix, there is something else wrong with the build process that stops it from finding the build tools.(FYI only cordova build android had this issue, a command-line based gradlew assemble task ran perfectly well).

But, this change does ensure that you can use terminal based cordova build android and you can still run and debug your app from the Android Studio as the file "local.properties" is restored afterwards.

There may be some more investigation needed to find out why the cordova build process does not work with local.properties or if this is an isolated issue that is happening on my dev environment only. This is just a temporary and quick fix to get your cordova build android working if you are also using Android Studio.

Recently, I had had an issue after opening my cordova project with Android Studio whereby the cordova build android command would constantly give me this error message.

Running command: /Users/Developer/app/hooks/after_prepare/010_add_platform_class.js /Users/Developer/app
add to body class: platform-android
Running command: /Users/Developer/app/platforms/android/cordova/build 
ANDROID_HOME=/usr/local/Cellar/android-sdk/24.3.4
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Running: /Users/Developer/app/platforms/android/gradlew cdvBuildDebug -b /Users/Developer/app/platforms/android/build.gradle -Dorg.gradle.daemon=true
:preBuild FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':preBuild'.
> failed to find Build Tools revision 23.0.1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.131 secs

/Users/Developer/app/platforms/android/cordova/node_modules/q/q.js:126
                    throw e;
                    ^
Error code 1 for command: /Users/Developer/app/platforms/android/gradlew with args: cdvBuildDebug,-b,/Users/Developer/app/platforms/android/build.gradle,-Dorg.gradle.daemon=true
ERROR building one of the platforms: Error: /Users/Developer/app/platforms/android/cordova/build: Command failed with exit code 1
You may not have the required environment or OS to build this project
Error: /Users/Developer/app/platforms/android/cordova/build: Command failed with exit code 1
    at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:139:23)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at maybeClose (internal/child_process.js:818:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)


Even though, I had all the build tools installed and everything, this issue started happening with the latest version of Android Studio. After a lot of debugging and almost giving up.

I found that the issue was happening due to local.properties file generated by Android Studio and the sdk.dir being specified in it. This change to the build script is not a permanent fix, there is something else wrong with the build process that stops it from finding the build tools.(FYI only cordova build android had this issue, a command-line based gradlew assemble task ran perfectly well).

But, this change does ensure that you can use terminal based cordova build android and you can still run and debug your app from the Android Studio as the file "local.properties" is restored afterwards.

There may be some more investigation needed to find out why the cordova build process does not work with local.properties or if this is an isolated issue that is happening on my dev environment only. This is just a temporary and quick fix to get your cordova build android working if you are also using Android Studio.
@saharshtibrewal
Copy link
Author

File Exists Check missing. Will add and resubmit.

@saharshtibrewal saharshtibrewal deleted the patch-1 branch October 8, 2015 03:03
@saharshtibrewal
Copy link
Author

Submitted a new one: #225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant