I am having a hard time moving one of my projects to Gradle. The reason I want to do this is to be able to use build flavours.
I imported my existing IDEA project in Android studio and created the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android.gms:play-services:3.2.+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
However, I get the following errors two errors:
Unsupported Modules Detected: Compilation is not supported for following modules: idoms-android, tests. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project.
and also
No resource found that matches the given name (at 'value' with value '#integer/google_play_services_version').
I don't know what module it is about, and don't know how to include play_services properly in Android Studio other than including it in the build. There seems to be nothing under my project structure:
Fixing the project structure seems to work (i.e. remove the two references) but next time they are back.
edit:
This is the second error:
Execution failed for task ':processDebugResources'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Applications/Android Studio.app/sdk/build-tools/android-4.4.2/aapt package -f --no-crunch -I /Applications/Android Studio.app/sdk/platforms/android-19/android.jar -M /Users/.../build/manifests/debug/AndroidManifest.xml -S /Users/.../build/res/all/debug -A /Users/.../build/assets/debug -m -J /Users/.../build/source/r/debug -F /Users/.../build/libs/idoms-android-debug.ap_ --debug-mode --custom-package org.idoms.iDomsAndroid --output-text-symbols /Users/.../build/symbols/debug
Error Code:
1
Output:
/Users/.../build/manifests/debug/AndroidManifest.xml:40: error: Error: No resource found that matches the given name (at 'value' with value '#integer/google_play_services_version').
Related
I have problem with Android Studio in build.gradle after I converted Eclipse project to Android Studio project.
I spent more than 2 hours finding a solution, with no results!!
build.gradle code:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
It gives: Error:(1, 0) Plugin with id 'com.android.application' not found.
Screen Shot
Any help will be appreciated ...
This error message means that it can't find the plugin at all. In Gradle, you need to tell it where to go to look for plugins, which is different from telling it where to go to resolve module dependencies. Add this block to the beginning of your build file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In projects that Android Studio creates, the New Project wizard will put this in the top-level build file in your project, but based on the screenshot it looks like your project only has a single build file, so there's only one place to put it.
I have a simple HelloWorld Android project (built in Eclipse IDE), I am able to do "gradle build" successfully in cmd prompt for this project.
Also I have written a simple JUnit Android Test Project for it, and it runs fine in Eclipse.
Now I want to run this Test Project or Unit Test Cases (if my understanding is wrong!) using Gradle script.
How do I do that?
Following is the build.gradle file I am using. I want to know how to write script code to automate the test case execution.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
test {
java.srcDirs = ['src/test/java']
resources.srcDirs = ['src/test/resources']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
androidTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Here is what I use:
android {
// ...
// your stuff
// ...
// So Android Studio can nicely edit the test files
sourceSets {
androidTest {
java.srcDir file('src/test/java')
}
}
}
// <Task to run tests>
sourceSets {
unitTest {
java.srcDir file('src/test/java')
}
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type: Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest
// </Task to run tests>
dependencies {
// ...
// compile stuff
// ....
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.11'
}
Run your test:
gradlew unitTest
This solution is not yet implemented and tested. Work in progress, comments are welcomed.
After reading all user guide carefully, it is clear that there is no direct support for Eclipse Android Test projects. The reason is that new build system adopts Gradle(and Maven) style to have Unit test inside module/project.
But because Eclipse Android Test projects is still Android project, there is way to try
Add build.gradle see below to Eclipse Android Test project.
It should have compile project(':app') in dependencies
/* Android
* http://www.nodeclipse.org/projects/gradle
* Nodeclipse/Enide build.gradle template for classic Android project
* https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/android/build.gradle
* Gradle Plugin User Guide:
* http://tools.android.com/tech-docs/new-build-system/user-guide
* Usage
* 1. put in project root
* 2. check version numbers
* 3. use from command line or with Gradle for Android http://marketplace.eclipse.org/content/gradle
* Support for this template
* https://github.com/nodeclipse/nodeclipse-1/issues/
* History
* 2014-03-13 android plugin updated to 0.9, see http://tools.android.com/tech-docs/new-build-system/migrating_to_09
* 2014-04-01 check for gradle version
* 2014-04-10 wrapper and runAndroidApplication tasks
* 2014-04-25 rename to run, add <<
* 2014-05-23 defaut plugin version 0.10.x
* 2014-06-06 show "boilerplate part"
* #author Paul Verest
*/
println GradleVersion.current().prettyPrint()
assert gradle.gradleVersion >= "1.10" // android plugin 0.10 requires Gradle 1.10, 1.11, 1.12
// after `gradle wrapper` it is possible to use './gradlew build' with Gradle version specified
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
//{ "boilerplate part"
buildscript {
repositories {
mavenCentral()
//jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
//androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
// for multi-module project build (needs `settings.gradle`):
// reference needed modules or App under test (for Eclipse Android Test project)
compile project(':app')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
//} "boilerplate part"
Because app and its test now make multi-module project add settings.gradle. The easiest way to put it 1 level down (folder where both project are)
include ':app'
include ':apptest'
Other way is to put in sibling folder, e.g. called parent
includeFlat 'app'
includeFlat 'apptest'
In later case you would need to run build from that folder (or specify each time settings.gradle location with -c CLI option
UPDATE Doing research about pure JUnit tests with gradle (a bit offtopic for this question), it seems like Android Tooling team overlooks this question, breaking what others developers are trying to do with newer android gradle plugin. Even in Android Studio it is not so even How can I create tests in Android Studio? . The only orgnization that cares is Robolectric contributors that have its framework ready for pure JUnit testing https://github.com/robolectric/gradle-android-test-plugin
I have exported my Android project in Eclipse as gradle build files, and imported in Android Studio.
I have updated my Android Studio today to v0.6.0 which built on June 05, 2014.
As a remote dependency I have added AppCompat to the dependencies which works fine as expected.
compile 'com.android.support:appcompat-v7:19.+'
But when I try to add other libraries such as SmoothProgressBar, actionbarsherlock, nineoldandroids and etc, it fails. When I run the app it shows following in Gradle Build tab:
Error:A problem occurred configuring root project 'MyApp'.
> Could not resolve all dependencies for configuration ':_debugCompile'.
> Could not find com.github.castorflex.smoothprogressbar:library:0.5.1.
Required by:
:MyApp:unspecified
And when I sync project with Gradle file, it shows following in Gradle Sync tab:
Error:com.github.castorflex.smoothprogressbar:library:0.5.1 (double-click here to find usages.)
I tried an empty new project and add remote dependency for mentioned libraries(Sherlock and etc) worked as expected. So I guess I missed something in build.gradle or any other solution?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.castorflex.smoothprogressbar:library:0.5.1'
compile 'com.android.support:appcompat-v7:19.+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Add this code to your build.gradle
repositories {
mavenCentral()
}
Update to explain the answer:
To resolve your dependencies, you have to indicate in the gradle script which are the repositories to be used. You can use more repositories...
With the support libs (support and support-compact) it is not necessary, because the android plugin searches in your androidRepository and googleRepository downloaded with your SDK Manager.
I just recently converted my project to Gradle and it works fine. I'm trying to add some tests to it now. I followed the Android Gradle user guide for setting up my build.gradle with tests. Here is my build.gradle:
buildscript {
repositories {
maven {
url 'http://repo1.maven.org/maven2'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
testPackageName "com.instrumentTest.java"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('src/instrumentTest')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
I created a package under src/ called com.instrumentTest.java that I put a sample ServiceTest.java class in to test my Service. I run the test using gradle aDT and get this:
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':<MyProject>:compileDebugAidl'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
Caused by: com.android.ide.common.internal.LoggedErrorException: Failed to run command:
<some big command>
Error Code:
1
Output:
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "public"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "public"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "class"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "MyServiceTest"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "extends"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "ServiceTestCase<MyService>"
It outputs the syntax error don't know what to do with for each line of code in my sample test class. Not really sure what's going on. The documentation for setting all of this up is pretty fuzzy.
I'm also not really sure the difference between assembleDebugTest, connectedInstrumentTest, and installDebugTest based on their descriptions from running gradle tasks. Is there something I have to do to my manifest in order to have the tests visible to Gradle? Am I supposed to create my tests in an entirely different project? I read that previously people would create separate test projects, but since I set the root for instrumentTest in the build.gradle file I assume we can do it all from one project now?
In build.gradle, you declared your instrumentTest folder to be in src/instrumentTest, which means if you have a test inside that's in the com.instrumentTest package, it should be located at src/instrumentTest/com/instrumentTest/MyServiceTest. Instead you've put the file at src/com/instrumentTest/java/MyServiceTest.
For reasons I don't quite understand it's trying to compile it with the AIDL compiler, and your java file isn't valid AIDL. If you move it to the directory it's expecting for instrumentTest, it should compile okay.
There's an explanation of the Gradle tasks at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Android-tasks.
assembleDebugTest - The task to assemble the output(s) of the project
connectedInstrumentTest - Runs checks that requires a connected device or emulator. they will run on all connected devices in parallel.
installDebugTest - installs the test APK on the connected device or emulator
You can try this instead if you don't want to mess around with the gradle file:
http://rexstjohn.com/unit-testing-with-android-studio/
Basically, you set up and run an Android Test configuration which can run all tests in the module, in a specific package, class or method. So you can set up your test classes wherever you want. It will pick up the test classes without having to modify the build.gradle file and running commands. That also answers your last question, it can be all from one project.
I just started using Android Studio and Gradle and couldn't get the tests to run properly at first until I tried the above. I found it odd having to modify the build.gradle file just to set up a simple test.
Gradle noobie, a little confused. I'm trying to tell gradle to use a different build script, from the command-line, but whenever I do so, it tells me it can't find my dependencies.
Whenever I call:
gradle -b build.gradle
It works fined.
But when I call:
gradle -b other.gradle
It pukes trying to find my dependencies. e.g.
* What went wrong:
A problem occurred evaluating root project '<main_app>'.
> Project with path ':facebook-android-sdk-3.0.1:facebook' could not be found in root project '<main_app>'.
I'm using the exact same build script in the exact same directory, just named differently. Here it is:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile project(':facebook-android-sdk-3.0.1:facebook')
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
My settings.gradle file:
include '<app_module>'
include '<:facebook-android-sdk-3.0.1:facebook>'
-b only works for single-project builds. In multi-project builds, settings.gradle determines which build scripts constitute the build, which project they belong to, and where they are located.