I'm adding unit tests to my existing Android Studio project and I'm a bit confused with the setup. Particularly, the androidTest vs instrumentTest flags within the gradle script. Can someone explain the different between these 2 sections and what exactly they target vs the other.
My project was migrated from an Eclipse project, so it does not have the default gradle structure. Here's what I've been playing around with:
androidTest {
setRoot('tests')
java.srcDirs = ['tests/src']
}
instrumentTest {
setRoot('tests')
java.srcDirs = ['tests/src']
manifest.srcFile file('tests/AndroidManifest.xml')
}
Is there any reason to have both?
Is there any reason to have both?
No, because they are the same thing, as instrumentTest was renamed androidTest in version 0.9.0 of the Gradle for Android plugin.
Related
In this thread both project structures were already compared. The question that is still open for me is if there is something that won't work or any disadvantages or staying with the old eclipse project structure after migrating to Android Studio? We have migrated a really big project to Android Studio staying with the old structure and all works fine, changing the structure of such a big project would be really complicated, are there any advantages with doing so?
You can stay with old Eclipse project structure. All you has to do is set up correct path to your code in build.gradle config file:
android {
...
sourceSets {
main {
manifes.srcFile 'path/to/AndroidManifest.xml'
java.srcDirs = ['path/to/src']
resources.srcDirs = ...
aidl.srcDirs = ...
res.srcDirs = ...
assets.srcDirs = ...
jni.srcDirs = ...
}
androidTest {
java.srcDirs = ['path/to/android/tests']
res.srcDirs = ...
...
}
test {
java.srcDirs = ['path/to/unit/tests']
}
}
}
Only thing that may be confusing that if you are using proxy and configure it in AndroidStudio, it will inject parameters as plain text in your root gradle.properties file. If you are using gradle.properties, i.e. you are inject variables from Jenkins, it can be frustrating, and also you can suddenly commit this file with all your credentials. To avoid it, you can use project structure with modules:
/root
-build.gradle
-gradle.properties
-module/
-module/build.gradle
-module/gradle.properties
In this case, you can hold your gradle variables in module/gradle.properties, and root gradle.properties will be used for AndroidStudio purposes.
I would like to use gradle for my Android/Eclipse project because I want to be able to install a development version of my app next to the released version (as seen here: What is a practical way to install stable and development app versions simultaneously?)
Using the command line for doing so is OK for me.
So I moved the src folder of my project to src/main/java and created a build.gradle file (as described here: http://www.nodeclipse.org/projects/gradle/android/Make-Android-Eclipse-project-ready-for-Android-Studio):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
androidTest.setRoot('tests')
}
}
But when I run gradle build, I get this error:
FAILURE: Build failed with an exception.
...
> Could not create plugin of type 'AppPlugin'.
I already found that error on stackoverflow. Some people say it's because of using the wrong version of gradle. (Gradle is issuing an error "Could not create plugin of type 'AppPlugin'")
My questions:
As far as I understand, this version number in build.gradle (0.12.+) is the version of the gradle plugin and not of gradle itself. How can I find out which gradle plugin version I'm using?
And where does this plugin come from? Is it part of the gradle installation? Or part part of Eclipse? Or of ADT?
I installed Gradle IDE as Eclipse plugin. Does it interfere with a regular gradle installation?
After some more research, finally I succeeded compiling my app with gradle.
Just in case somebody else has the same problem, here is what I did:
Right click your Android project in Eclipse -> Export -> Android -> Generate Gradle build files.
This creates build.gradle, gradlew, gradlew.bat and settings.gradle (in my case some levels above my project). Don't move these files into your project folder.
Then run ./gradlew build. This will download the gradle plugin automatically (afaik) and create three apk's in [PROJECT FOLDER]/build/outputs/apk.
My Android project contains some property files in the package structure. To read this property I use MyClass.getResourceAsStream("someProperties.xml"). MyClass has no access to a Context.
After migrating to Android Studio the someProperties.xml is not moved into the resulting package structure. Therefore my code can't find the file.
What can I do to read my file again? How can I modify the gradle build to have the file copied to my package structure again or is the a possibility to read resources without a context and put the file into assets?
I guess you have not specified within gradle where those resources are located.
Defaults are different in Eclipse ADT and Android Studio.
Here are defaults for Eclipse (when java sources already in src/main/java)
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
see MAKE ANDROID ECLIPSE PROJECT READY FOR ANDROID STUDIO, YET KEEPING IT ACCESSIBLE FOR ECLIPSE USE TOO
I'm trying to move my current android project to a Gradle project, so here what I've done so far:
add gradle to eclipse
generation of build.gradle file
create ANDROID_HOME environment variable in Windows 7
at the root of my project in command prompt: gradlew build
So now, I've got a new folders structure in my project:
It's my first time with gradle, I guess I missed something and I would like to know if to finish this migration I have to delete the src and res directories (in blue) ? For me I just have to get a new structure of folders after the generation of gradleview build..
And if what I've generated looks like to a gradle project ?
If your project structure does not fit to android studios structure you can add your folders to the source set in the gradle file of your app.
So your android studio will match the correct folders
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Project-Structure
sourceSets {
main {
// manifest.srcFile 'src/main/AndroidManifest.xml'
// java.srcDirs = ['src/main/java', 'build/generated/source/apt/${variant.dirName}']
// resources.srcDirs = ['src/main/resources']
// res.srcDirs = ['src/main/res']
// assets.srcDirs = ['src/main/assets']
}
}
as copied from here
There was typo: use gradlew build or gradle build
You can keep Eclipse folder structure and configure it in build.gradle
Yet I would recommend to move source into src/main/java.
Eclipse would be quite OK with that.
http://www.nodeclipse.org/projects/gradle/android/Make-Android-Eclipse-project-ready-for-Android-Studio
I'm trying to get a new PhoneGap application setup and running inside Android Studio with the Gradle build system.
At the moment I have successfully created the PhoneGap project and imported into Android Studio. It all appears to be working fine, but I cant work out how to move it to the Gradle build system, or even if its possible.
Can anybody help?
I managed to do this.
You need Android Studio and the Eclipse ADT version, as well as Cordova/PhoneGap all set up.
Import the Cordova project into Eclipse.
Go to File -> Export... -> Generate Gradle Build Files.
Click next to get past the "Import Instead?" screen.
Select both your Android project and the CordovaLib project to export and click Next.
Once this completes, open Android Studio.
Go to File -> Import Project...
Select the build.gradle file for the main Android project, which was generated by Eclipse, and click OK.
After the import, you may get some warnings about a newer gradle version in use, just check your settings and it seems to work itself out.
At this point, you should have a project structure that is your main project, but with CordovaLib as a module.
Now you can open the build.gradle file in the main project directory and change it to this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
}
}
apply plugin: 'android'
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']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':CordovaLib')
compile 'com.android.support:appcompat-v7:19.+'
}
You should now be able to convince Android Studio to compile.
An extra tip would be to create a script to run "cordova prepare" and add that to the module's run configuration as an external tool. Make sure to synchronise the whole project before deploying the APK to a device or emulator.
I am new to Android Studio and still getting used to the AS project structure and Gradle.
Using Cordova 4.1.2
Android Studio 1.0.1
1) I created the app using the Cordova CLI:
cordova create CordovaAndroidApp
cd CordovaAndroidApp
cordova platform add android
this version of Cordova created the build.gradle and settings.gradle files.
2) From Android Studio splash screen I selected "Start a new Android Studio project"
On the second screen I checked the Phone and Tablet box; on the third screen, I chose "Add No Activity"
3) In this new Android Studio application, from the Project view in the left panel with the top level of the project selected, I selected File -> Import Project. In the popup "select Eclipse or Gradle Project to Import", I chose to the Cordova project directory, clicked down to the platforms / android directory, and selected the build.gradle file, then OK.
I was able to build and run the basic Cordova project (just the splash screen) with no problem.