Can't migrate project to Gradle in Android Studio - android

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.

Related

Migrating Library and Apps from Eclipse to Android Studio

In my team, we developed a library in Eclipse with ADT plugin.
We have a "big" demo application and several "mini" applications using the library to test different aspects.
Since the official IDE for Android is now Android Studio and ADT plugin seems to be abandoned (last update on October '14), we are studying the migration to Android Studio.
On Eclipse, we can load projects from different locations in the disk within the same workspace. We like this because this allows us to have our library project loaded aside with our demos and we can also load temporal projects in the same workspace referencing the project library.
I see there are two ways to migrate, but I find cons in both:
Importing the Eclipse project directly into Studio
This option works but:
This changes the folders structure of all the projects. It copies the eclipse projects into the Android Studio project folder, and this would mean making changes to our internal repository.
If we want to add a temporal project to test it with our library, we'd need to import it so it will also be copied next to the "main" library and demos.
Exporting the Eclipse project from Eclipse as a Gradle project.
This option keeps the folder structure of our Eclipse projects (#1 above) but #2 above is still valid here and I also get this error when I try to Import a project from a different location.
Error:FAILURE: Build failed with an exception. * What went wrong: Task
'compileDebugSources' not found in project ':TestMemCons'. * Try: Run
gradle tasks to get a list of available tasks. Run with --stacktrace
option to get the stack trace. Run with --info or --debug option to
get more log output.
So, can we migrate to Android Studio keeping the folder structure and allowing us to load other Eclipse/Android Studio projects in the same instance so they can reference our library project?
Answering my own question...
So, can we migrate to Android Studio keeping the folder structure and
allowing us to load other Eclipse/Android Studio projects in the same
instance so they can reference our library project?
I found you can add a temporal project as follows:
Copy the temporal test project where you want it, it only has to be inside your main project tree.
Add the test project relative path to the settings.gradle file of the main project. Ie, if you moved the test project to \Demos\Tests\Test1:
include ':Demos:Tests:Test1'
So you can have something like this:
include ':Sources:Android' //library
include ':Demos:Android' //Features Demo
include ':Demos:Tests:Test1' //Test1
Create a build.gradle file for the test project. Ie, \Demos\Tests\Test1\build.gradle:
apply plugin: 'android'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':Sources:Android')
}
android {
compileSdkVersion 7
buildToolsVersion "21.1.2"
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')
}
}
In this case, I'm adding a dependence to my library, that is a library module in the main project at \Sources\Android.
So, if you want to keep your folder structure, you can go for the option 2 "Exporting the Eclipse project from Eclipse as a Gradle project" and still load test/temporal projects as described above.
The only problems I see here are:
This is less automatic than it was with Eclipse.
In Eclipse you could remove the test project when done and the project was still in the disk but the workspace was clean again. In Android Studio, with the technique above, you should remove the test entries from the settings.gradle in your main project and remove the test project folder to get the main project clean as before the test.

Migrate Eclipse project to Gradle

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

How to Get a PhoneGap Project to Run in Android Studio with Gradle Build System

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.

Unable to add AndEngine to Android Studio

I am trying to almost 2 days to add AndEngine to Android Studio but unable to do so. I tried the following two methods, neither worked.
1st Try
I download the AndEngine code from GitHub Link -- this is NOT a Gradle Project
and tried to add it to my Android Studio build.gradle and settings.gradle, but i get this error, my screenshot: https://postimg.cc/image/5mcvpvsar/
(I think I am getting this error because AndEngine is not a gradle project - HOW TO MAKE IT A GRADLE PROJECT??)
2nd Try
I have also tried adding the andengine.jar (file I just googled for) in /libs folder and do right-click --> "Add as Library" but still I cannot do "import org.andengine...." in my project files.
All tutorials available online are in Eclipse, I am using Android Studio.
I am not even able to start.
UPDATE: Yes, I gave up using Android Studio for AndEngine! Took me 15mins to do this in Eclipse, compared to the unsuccessful weekend (which i will never get back!) I spent on Android Studio
I use this techique: I set this in my settings.gradle
include 'andengine'
project(':andengine').projectDir = new File(settingsDir, '../relative/path/to/andengine')
that is in the root directory of the project (I think gradle has already created it for your main project).
In AndEngine use a build.gradle like the following for the AndEngine project
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
and add in the dependencies of your project
compile project(':andengine')
Maybe you have to close and reopen Android Studio, but normally for me this works.
BTW after have write the answer I see that someone has opened a pull request for a gradle build file.
I created a tutorial for this - How to add Andengine, Andengine Tile Map, Andengine PhysicsBox2D to Android Studio 0.8.9.
Here is the link, I hope everything works - https://docs.google.com/document/d/1zk2QjNiPvkj52G4qSVivEPrLfkCUVqmnCVH8TfsnER8/edit
ANDENGINE WITH ANDROID STUDIO 0.8.9
Note: I am using the AnchorCenter brach and TortoiseGit to get all the files.
Download Andengine from github using TortoiseGit: https://github.com/nicolasgramlich/AndEngine
After the dowload use TortoiseGit to switch to branch GLES2-AnchorCenter
Create new project in Android Studio
Create new module:
Select File -> New Module -> Android Library
Set Application name to AndEngine
Set Module Name to AndEngine
Set Package Name to org.andengine
Set Minimum SDK 14
Target SDK 19
Compile with 19
Theme None
Keep clicking next until module is created (no difference what you pick)
Enter the folder where you have downloaded Andengine, enter src/org/andengine and copy all the files inside.
Paste the copied files into your new module in your project src/java/org.andengine. After pasting everything remove tha MainActivity that was created on default
Enter the folder where you have downloaded Andengine, copy AndroidManifest and paste it into your new module (src/main)
Add the module to the project:
Select File-> Project Structure -> app-> Dependencies
Click the “+” button and pick “Module Dependency”
Select from the list your AndEngine Module
Check your project gradle in app folder (build.gradle) and make sure you have a line like this under dependencies - compile project(':AndEngine')
You should now be able to use AndEngine in your project
ANDENGINE TMX TILED MAP EXTENSION WITH ANDROID STUDIO 0.8.9
Note: We do this the same way like with Andengine but we change a few things:
Download AndengineTMX from github using TortoiseGit: https://github.com/nicolasgramlich/AndEngineTMXTiledMapExtension
After the dowload use TortoiseGit to switch to branch GLES2-AnchorCenter
Create new project in Android Studio
Create new module:
Select File -> New Module -> Android Library
Set Application name to AndEngineTMXTiledMapExtension
Set Module Name to AndEngineTMXTiledMapExtension
Set Package Name to org.andengine.extension.tmx
Set Minimum SDK 14
Target SDK 19
Compile with 19
Theme None
Keep clicking next until module is created (no difference what you pick)
Enter the folder where you have downloaded AndengineTMX , enter src/org/andengine/extension/tmx and copy all the files inside.
Paste the copied files into your new module in your project src/java/org.andengine.extension.tmx. After pasting everything remove tha MainActivity that was created on default
Enter the folder where you have downloaded AndengineTMX, copy AndroidManifest and paste it into your new module (src/main)
Add the module to the project:
Select File-> Project Structure -> app-> Dependencies
Click the “+” button and pick “Module Dependency”
Select from the list your AndengineTMX Module
Check your project gradle in app folder (build.gradle) and make sure you have a line like this under dependencies - compile project(':AndEngineTMXTiledMapExtension')
You should now be able to use AndEngineTMXTiledMapExtension in your project.
ANDENGINE PHYSICSBOX2D WITH ANDROID STUDIO 0.8.9
Download this file - http://d-h.st/FyC
Unzip the file
You should have 2 jar files, copy them to your project app/libs
Right click on andenginephysicsbox2dextension.jar and select “Add as library” (or something like this)
Open your build.gradle in your app folder
Under dependencies add compile files('lib/physicsbox2d_so_files.jar')
You should now be able to use PhysicsBox2D in your project.
As the question's answers havn't been accepted yet, and I know people are still searching for solutions to this, I found this great website with very very clear and concise instructions to import AndEngine to your Android Studio projects. Here's the link:
http://geq-i.blogspot.com/2014/02/how-to-setup-andengine-in-android-studio.html
All credits go to the user who created this page. I can attest to the fact that this is working perfectly. I JUST used this website after trying 10s of different ways.
Only thing to note on the link is the last part:
$ cd <project folder>/AndEngine/src/main
$ rm -r java/org
$ mv org java
This part is copying the org folder from . to ./main/java. Best way to do this is to just drag and drop the org folder into main/java when the project has finished building once.
Hope this helps!
Well i had the same problem - This helped me to solve it.
http://www.makethegame.net/android-andengine/how-to-setup-andengine-with-android-studio/
Check out this tutorial on how to use andengine in android studio
http://javaprogrammernotes.blogspot.in/2014/05/settings-up-andengine-in-android-studio.html
Brief Summary of the Tutorial(Check out the full tutorial if you face any issue):
Let's assume that you already have created a project and it has default structure. First create folder named third_party in the root directory of the project. Then in third_party directory create subdirectories called andengine and andenginebox2d. I assume that you already downloaded or cloned AndEngine and Box2d extension for it. Put AndEngine and AndEngineBox2d in andengine and andenginebox2d directories respectively. Create file named build.gradle in andengine directory and andenginebox2d directory. Build.gradle files is a file that tells gradle how to build your project.
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-project.txt')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Open settings.gradle that is located in your project's root directory and add two lines to it:
include ':third_party:andengine'
include ':third_party:andenginebox2d'
Next open build.gradle which locates in app directory and add
compile project(':third_party:andengine')
The final step is to open AndroidManifest.xml in andegine and andenginebox2d directories and make them look like this:
<!--?xml version="1.0" encoding="utf-8"?-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.andengine">
<application>
</application>
</manifest>
application block is needed because of bug in manifest merging tool.
That's it! Now clean your project and press run. Everything should work just fine.

How can I import a new module (android library project) using Android studio 0.3.0

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!

Categories

Resources