Final Edit:
This was kind of a long question with several edits, and I wasn't necessarily doing the right thing at each step of the way. For anyone who stumbles on this question and is having trouble converting a project to gradle in Android Studio, I'd suggest checking out this repository mentioned in question # 16718026 and trying to import that and compile and run it. Once you have something working it's easier to see what might be wrong with your own project.
To summarize, here's what I did to convert a non-Gradle Android Studio project into a working Gradle one, using Android Studio .2.5, which auto installed Gradle 1.6 and the Gradle Plugin .5 for me.
(1) Created a new blank Android Studio project with an icon and an activity. NOTE: I didn't realize this at first because I was learning Android and Gradle at the same time, but Android Studio (in this version anyway) actually sets you up with a multi-project setup, which was confusing for me. Multi-Project means there's another folder above your project in the hierarchy, giving a location to store settings for all projects. AS did not create a build.gradle file in the subproject folder (in my case, FrontlineSMS), despite a video guide indicating it would. So I had to create that subproject build.gradle file and also create a very simple settings.gradle (see Update 2 below.)
(2) Copied src/, res/, .git/, lib/, .gitignore, and AndroidManifest.xml to the root of the new project.
(3) Used git mv
to move these items to the appropriate places according to conventions in the Gradle Plugin User Guide, replacing or removing some existing files or folders in the blank project where necessary.
(4) Created a build.gradle
in my subproject folder and gave it some basic settings (see below or in the question linked above)
(5) Imported the project as described in Update 4
Original question:
I'm a novice Android developer trying to convert an Android Studio project that does not currently use Gradle (and compiles and runs just fine) to one that uses Gradle on OS X. I'm stuck on a Resource error, which I think may have to do with either my project structure, settings in the IDE, settings in the build.gradle files, or some combination of those.
The way I went about doing the conversion was to first create a new project in Android Studio, copy over files to appropriate locations, then add some settings in the IDE and build.gradle files. My resulting project structure is,
├── frontlinesms-for-android
├── build.gradle
├── gradle
├── gradlew
├── gradle.bat
├── settings.gradle (added in update 2)
├── local.properties
├── lib
└── lib1.jar
└── lib2.jar
└── ...
├── frontlinesms-for-android.iml
├── FrontlineSMS
└── build.gradle
└── src
├── main
│ └── res
│ └── AndroidManifest.xml
│ └── java
│ └── net
│ └── frontlinesms
│ └── android
│ ├── FrontlineSMS.java
│ ├── ...
Most notably, maybe, is I put the res/ folder at the same level as java/ and AndroidManifest.xml. I'm trying to follow the conventions mentioned in the Gradle Plugin User Guide so I don't have to customize too much in the build.gradle files.
I also marked the folder FrontlineSMS/src/main/java/net as source because if I don't then nothing attempts to compile. I created a new build.gradle file in FrontlineSMS/build.gradle because it did not exist when I made the new project, even though an instructional video on the Gradle website said it would.
In the IDE I also set the dependencies for the external libraries I have in /lib.
When I try to compile via the IDE I get many Package r does not exist
errors, indicating the auto generated R class is not getting generated or recognized. This is defined in the project as,
import net.frontlinesms.android.R;
in several files. What can I do to make Android Studio / Gradle recognize the resources I have defined under src/main/res?
Reading up on others' questions, I notice that some solutions to this error involved marking a "gen/" folder as a source root or "cleaning" the project. I tried creating a gen/ folder in the root directory and marking it as source but that didn't work. And, rebuilding the project just gives me the same errors.
My root build.gradle file has the following,
dependencies {
compile project(':FrontlineSMS')
}
And the one under FrontlineSMS/build.gradle has,
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile file('../lib/acra-3.1.1.jar')
compile file('../lib/activation.jar')
compile file('../lib/additionnal.jar')
compile file('../lib/annotations.jar')
compile file('../lib/libGoogleAnalytics.jar')
compile file('../lib/mail.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
When I try to build from the command line using ./gradlew clean
I get this error,
(removed error)
In addition to my previous question, I'd appreciate any tips you can lend as to how this project should be structured. This is not meant to be a multi-project setup. Thanks for your help.
Update 1
I was able to generate an APK from the command line. My main problem there is I was calling gradle clean
from the wrong directory. Calling it from FrontlineSMS worked. I'm guessing Android Studio set me up with a multi-project setup when I just had one project, and I hadn't entered all the settings for that, ie, I hadn't created a settings.gradle file.
Update 2
Created the following settings.gradle file, which allows me to run gradle clean && gradle build
from the root directory (where settings.gradle resides):
include ':FrontlineSMS'
Note: the version of gradle I'm using here is the one installed by homebrew, 1.6. If I use the gradlew wrapper that was installed it says ,
Gradle version 1.6 is required. Current version is 1.7
Searching the web for this error seems to indicate that Android doesn't support 1.7 at this point, only 1.6 (and maybe it's always a version or two behind), yet this is the version that was installed by Android Studio when I created a new project!
Update 3 (edit: this is basically all wrong, don't follow this)
Trying to build the project from Android Studio yields the same error as above regarding 1.6 being required. I tried reimporting the project, though it said it could not find any source files. I guess this means either (1) I have the wrong version of gradle and need to fix it and maybe (2) I'll need to define source files in the build.gradle file after all, even though I thought I was following the conventions from the guide.
I tried copying gradle-wrapper-1.6.jar
to replace gradle/wrapper/gradle-wrapper.jar
but that just gave a class or method not found error when I ran it via ./gradlew clean
.
Attempted solution: In gradle/wrapper/gradle-wrapper.properties
there is a line distributionUrl=
and I changed this to point to the 1.6 version,
distributionUrl=http\://services.gradle.org/distributions/gradle-1.6-bin.zip
Then the next time I ran ./gradlew clean
it downloaded a new version of of gradle into my home directory ~/.gradle
(which up until now I didn't realize existed) and now when I type ./gradlew --version
it says it is on 1.6, and everything builds via the command line using ./gradlew clean && ./gradlew build
, resulting in apks. So it seems to work from the command line, but I also want this to work from the IDE
In the IDE it now says
Project is using an old version of the Android Gradle plug-in. The minimum supported version is 0.5.0
So I update the build.gradle
file to say
classpath 'com.android.tools.build:gradle:0.5'
And reopen the IDE, which tells me,
Could not find com.android.tools.build:gradle:0.5.
Are Android Studio gradle plugin and gradle versions tied together? If so what do I want, gradle plugin .4 + gradle 1.6 or gradle plugin .5 and gradle 1.7? And how do I properly install these?
Update 4
I finally got this to compile via both the command line and the IDE. The trick was to import the project using Gradle after having created the basic Gradle files. Thanks Ethan! During the Import Project process there's a step where you choose "Create project from existing sources" or "Import project from external model". I'd been choosing the former when I should've chosen the latter and picked Gradle. After that I selected "use gradle-wrapper (recommended)" and checked "use auto-import" and clicked Finish. It warned me about a couple things I had to change, such as using the latest gradle plugin, for which I made this change in my build.gradle
file,
classpath 'com.android.tools.build:gradle:0.5.+'
which is suggested in the known issues for the android gradle plugin.
Every Android app has a unique application ID that looks like a Java or Kotlin package name, such as com. example. myapp. This ID uniquely identifies your app on the device and in the Google Play Store. Important: Once you publish your app, you should never change the application ID.
The package name of an Android app uniquely identifies your app on the device, in Google Play Store, and in supported third-party Android stores.
It looks like you are missing your settings.gradle file from the project root directory.
You are going to need the settings.gradle file to access the subproject that is located inside your source repo.
When you import the project be sure to select Gradle as the source!
Actually a couple of points:
Go to file -> settings -> gradle and browse to:
select Use local gradle distribution and browse to:
c:\users\.gradle/wrapper/dists/gradle-1.6-bin/72srdo3a5eb3bic159kar72vok/gradle-1.6
This works, but you have to clear all the previous stuff out, by going to:
File -> Invalidate caches / restart
Then you should come back in, it will re-import (might prompt you to again configure gradle if the above wasn't exactly correct i think)
Now it should work...you don't have to manually build the build.gradle files.
the settings.gradle under the top level project actually.
Finally if project structure isn't showing you much (mine only showed me platform settings and didn't have all the other selections in most guides) you can right-click on your top-level project eg HelloWorldProject and then select Module Settings and this might get you where you need.
Some screenshots for the above may be helpful - looks like they are changing layouts etc and hiding stuff so the official guides aren't always correct:
Lame can't post you images...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With