Migrate Eclipse project to Gradle - android

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

Related

Android Studio project with old eclipse project structure disadvanteages?

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.

Android Studio Gradle androidTest vs instrumentTest

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.

properties file not found after migrating Android project from eclipse to Android Studio

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

Can't migrate project to Gradle in Android Studio

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.

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.

Categories

Resources