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.
Related
I have a project that I already run in Android Studio.
After I've opened the project in Android Studio I got the message:
Migrate Project to Gradle?
This project does not use the Gradle build system. We recommend that you migrate to using the Gradle build system.
More Information about migrating to Gradle
Don't show this message again.
Following the link of More Information about migrating to Gradle although I'm already on Android Studio I've followed the option Migrating from IntelliJ Projects .
Since I've already have a build.gradle file at the root of my project I've modified to include some dependencies.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile "com.android.support:support-v4:18.0.+"
compile "com.android.support:appcompat-v7:18.0.+"
}
android {
compileSdkVersion 18
buildToolsVersion "18.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')
}
}
As instructed, i've ran gradle assembleDebug on the Android Studio terminal windows but was getting some errors. To solve this errors I had to create a local.properties with sdk.dir=C:\\Program Files (x86)\\Android\\android-sdk and remove the android-support-v4.jar file from the /libs folder and from the Libraries->libs from the Project Structure.
I now ran again gradle assembleDebug and this time it finishes without any errors.
I've restarted Android Studio but after restarting I've saw that on the External Libraries of the project there were no support-v4 and appcompat-v7 and in one of my classes I have the import android.support.v4.widget.CursorAdapter; marked as error since Cannot resolve symbol Cursor Adapter.
If I press the button Sync project with gradle filesi got the message: The project MyProject is not a Gradle-based project
Can someone help me figuring out how to solve this?
Here is my project Structure
The project thinks it's still a non-Gradle based project; it's not the presence of the build.gradle file that makes it Gradle-based, but it's how the project was set up in the first place. You'll need to re-import your project to finish the conversion to Gradle.
First, though, it looks like you don't have a settings.gradle file; it looks like you need one. Since you've set up your project as a single-module project, then you can put the file in your project's root directory, next to build.gradle. It should contain this:
import ':'
In the future if you add more modules to your project you may want to convert it to a multi-module directory structure, but you don't need to worry about that now. In any event, now you need to do the re-import in Android Studio:
Close your project
Back up your project
Delete the .idea folder in the root directory of the project
Delete all the .iml files in your project
Import your project in Android Studio, and in the dialog that prompts you for a file, choose the build.gradle file.
After this you should be good to go.
In my case, when I tried to open an existing gradle project and AS didn't recognize it like a gradle project, the problem was inside settings.gradle file. settings.gradle was corrupt because, for some reason, the semicolon was missing. The right structure is include ':app'; where app is the name of the project's module.
Also, if you need, you can put more than one module, like this: include ':module1', ':module2', ':module3';
I hope it could help someone.
In my case: Step 1: Project Structure > Settings.gradle (Open it and add-- include ':app'; .
Step 2: Goto Gradle Tab (It normally present in Left or Right of Android Studio) > Then click to Refresh All Gradle Project
Step 3: Bang....... I hope its work.(Its works for me)
All I did was to try importing the project with the "import Gradle, Eclipse ETC" option at the start window of Android Studio, selected my gradle-incompatible project to import, and then it did import by asking me automatically if I wanted to make a gradle build project and voila it did import correctly this time with no errors.
The process is pretty straightforward, nothing to fiddle around with nasty settings, just follow the IDE auto-fixes and it will work.
At last that worked for me!
Just check if you are importing the right folder which contains .gradle and .idea folder
click on File
Click on Import Projects
Click on Users then AndroidStudioProjects
Choose your Project then Click OK
NOTE: If you are moving from eclipse to Android Studio then you have do exactly same thing but choose the eclipse projects.
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.
I just spend some time and failed trying to migrate an existing android studio project from gradle 1.8 to gradle 1.9 final ( which was released yesterday 19th Nov ).
I read most of the other gradle related posts here but none worked for me.
here a list of what I've tried so far:
./gradle/wrapper/gradle-wrapper.properties changed distributionUrl to gradle-1.9-all.zip
Rebuild Project
in android-studio: Tools -> Android -> Synch Project with Gradle Files
running command gradle wrapper in project dir to update the wrapper
-
build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.0'
}
all ended with the "need gradle 1.8, change your gradle-wrapper.properties to gradle-1.8-all.zip" error message.
Gradle 1.9 isn't supported with the Android plugin 0.6.3; it requires 1.8. The plugin uses internal Gradle APIs and is tied to specific Gradle versions to ensure it works. This limitation will be lifted in the future, but will require some new features in Gradle.
The next version of the plugin will support Gradle 1.9.
EDIT:
Android Studio 0.4.0 and Android Gradle plugin 0.7.0 have been released; these support Gradle 1.9. At the time of writing, Gradle 1.10 is current, but is not supported yet in v0.7.0 of the plugin.
You could try this:
Close your Project and try to open your project, but instead of choosing the root folder of your project choose your settings.gradle file.
AS will ask you if you want to open this project -> click "Yes". Than a window appears "Import Project from Gradle". Make sure that "Use default gradle wrapper" is selected.
This helped me after upgrading AS to 0.3.1, since this version you need gradle 1.8.
The android tools team made a huge step when introducing android studio 0.3.0 with the new user interface for modifying the build.grade file using the project structure.
But how can I import an android library project into my general project? When I press the '+' button in the Project structore -> Modules section I can only create NEW module.
The way I just did it was by copy pasting the library project in the root of your project (not using android studio, this gives a 'cannot create class-file error', but just using the file manager of your operating system. When using android studio for this it didn't copy some of the files).
Then mark the java folder of the library project as 'source root' (right mouse on the folder).
Then to settings.gradle add your lib project like this:
include ':youApp', ':yourLibrary'
then to build.gradle of yourApp add dependancy:
dependencies {
compile project(':yourLibrary')
}
Now rebuild your project.
Now add a function of the library project (showing red) when you click on it and press Alt-Enter it should say "add dependancy on module yourAppProject"
In android studio 0.3.1 they fixed it.
Project structure -> Modules -> "+" -> Import module.
It's still broken 0.3.6...
The manual way can be a good solution. For example, to import a library like Android-Validator (https://github.com/throrin19/Android-Validator) in your project "Test":
The structure will be:
Test
build.gradle
Android-Validator (the library sources)
build.gradle
src
res
settings.gradle
And now, let's gonna edit the files...
In Test/build.gradle add:
dependencies {
compile project(':Android-Validator')
}
Android-Validator/build.gradle (remember to change xx and yy):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion XX
buildToolsVersion "XX.0.0"
defaultConfig {
minSdkVersion YY
targetSdkVersion XX
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
settings.gradle:
include ':Test', ':Android-Validator'
I had a very similar issues with gradle builds / adding .jar library. I got it working by a combination of :
Moving the libs folder up to the root of the project (same directory as 'src'), and adding the library to this folder in finder (using Mac OS X)
In Android Studio, Right-clicking on the folder to add as library
Editing the dependencies in the build.gradle file, adding: compile fileTree(dir: 'libs', include: '*.jar')}
But annoyingly, HOURS after I get it working, Android Studio have just released 0.3.7, which claims to have solved a lot of gradle issues such as adding .jar libraries
http://tools.android.com/recent
Hope this helps!
I've been having a lot of trouble in Android Studio trying to create an app with GoogleMap.
I have followed the following guide before with (almost) no issues using Eclipse as my IDE:
https://developers.google.com/maps/documentation/android/start
I have never used Android Studio before and I'm having difficulty with the whole project/module paradigm.
I haven't been able to successfully configure the Google Play Services SDK
http://developer.android.com/google/play-services/setup.html
Here is one of the weird errors I'm getting:
Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MyMapApp:compileDebug'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Finally I managed to run GoogleMapsAPIv2 project using Android Studio.
EDIT: As mentioned by Xavier, this method is going to work for non-gradle based projects only. And UI part which was used in this tutorial will be excluded from AndroidStudio. So if you have your own project which uses Gradle build system, you need to manually modify build.gradle configuration file since Android Studio UI doesn't affect it.
EDIT2: With AndroidStudio v0.1.1 release, UI part responsible for modules dependencies has been eliminated, so for now we need to update dependencies manually through build.gradle file. UI for changing gradle dependencies is going to be released in next releases
EDIT3: For those who still tries to use this approach - please note that it is obsolete and doesn't work anymore
Here is what I did:
1) I took maps project from the Google Play Services samples and copied that to the separate directory. That is going to be our MapsApiV2 project we will be trying to run. On my Mac it was located at <sdk_location>/extras/google/google_play_services/samples
I placed it to the ~/Work/stack/
2) Copied google-play-services_lib project directory to the same place (~/Work/stack), so my working directory looks like this. Btw, lib project is located at <sdk_location>/extras/google/google_play_services/libproject:
3) Now let's open Android Studio. On welcome screen press Import Project and import our maps project from ~/Work/stack/maps. Now we see a lot of complaints about unknown reference to GMS library:
4) Now we need to add Google Play Service as a reference library. Going to View -> Open Module Settings
5) On the Modules tab, click + button and select Import Module and import your GooglePlayServices lib. I didn't change anything in the wizards, so clicked Next all the way to the end:
6) Now you need to reference this imported library. Open this screen again (go to View -> Module Settings). Make sure you have your maps project and Dependency tab selected. Click + to add a dependency and select Library. Choose your imported library there:
7) Now we can see that it is not complaining about GMS library, but still complaining about support library:
8) Let's fix it. I have my support library located at <sdk location>/extras/android/support/v13/android-support-v13.jar. So let's try to add it to our workspace. Go to View -> Open Module Settings and select Libraries tab. Select + -> Java and select support library:
9) Now it is going to ask you which project to add this lib to, so make sure you have selected your maps project:
10) At this point code should compile w/o problems. Just make sure you are targeting the right SDK version in Manifest.
Have fun
I was following the same instructions except I was creating a new project. Under the project structure I removed the Android-Gradle facet and was able to build successfully. Optionally one can update the gradle build files and add the Android-Gradle facet to the play services library.
NOTE: I changed the name of Google Play Services directory.
build.gradle for Google Play Services library.
apply plugin: 'android-library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile files('google-play-services.jar')
}
android {
compileSdkVersion 17
buildToolsVersion '17.0.0'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
build.gradle for test app.
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile project(':lib-google-play-services')
compile files('../lib-google-play-services/libs/google-play-services.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 11
targetSdkVersion 16
}
}
I have tried and failed many a tutorial on this, but finally find a simple solution that seem to work with Android Studio and gradle
I just installed Android Studio 0.2.3 on my mac, and these are the steps that made me view a maps fragment on a fresh hello world project template:
1) Click the SDK manager button in the toolbar in Android Studio.
2) Under 'Extras' locate 'Google play services' and download it.
3) in your build.gradle file in your src directory, add this line to dependencies:
compile 'com.google.android.gms:play-services:3.1.36'
4) order and install your API-key following this tutorial: https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key
5) add the fragment to your layout xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
6) you should now be able to run your project on your device.
Here is a configuration for a project I made that uses Google Maps API V2, in Android Studio 0.2, with gradle 0.5.+. Also other modules like ActiobarSherlock, and a custom NumberPicker are used, and i just leave them in case someone needs them.
Project structure:
1) TOP Directory settings.gradle:
include ':SuperModule', ':libraries:actionbarsherlock', ':libraries:numberPicker'
2) TOP Directory build.gradle
task assemble {}
(some of you may faces the task assemble not found. thats why you put this line!)
3) SuperModule build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android.gms:play-services:3.1.36'
compile 'com.android.support:gridlayout-v7:13.0.0'
compile project(':libraries:actionbarsherlock')
compile project(':libraries:numberPicker')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
}
}
Notice the dependencies here. Its actiobarSherlock(ABS), and NumberPicker, used as android Libraries. I also use Play Services(thats why the min sdk must be >=8), and the layout lib (for the space element)
Support library is NOT included here, since its included in ABS library!
Actionbar Sherlock build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
NumberPicker build.gradle isnt shown because it has the same logic with the others..
I wrote an article and stepped through creating a map application using both the Location interface and google map api. This will go through many of the idiocyncracies I ran into while trying to get a map application up and running including, which SDK's to include, what libraries to import, what to set in the android manifest and how to change the gradle file.
Hope this helps
http://www.todroid.com/how-to-create-a-google-map-application-using-android-studio/
I had a similar problem, but it was just that it couldn't find the com.google.android.gms.maps package (it kept on saying it did not exist). This was after I imported everything (seemingly) properly by following the migration tutorial on Google's own docs: http://developer.android.com/sdk/installing/migrate.html
Anyway, after importing everything, I still had the issue and wasn't sure how to set the build path (or the non-eclipse equivalent).
I just had to follow half of your tutorial below, remove the google-play-services library which was already listed in the module settings window, then add it again.
I hope this is useful to someone else.
I had similar problem too,i solved importing google_play_services.jar like Library instead google play services project Like a Module.