I wanted to take a look into data binding for Android so it set up a small project. But i can't seem to get it working for Android Studio.
Basically the problem is it doesn't render my layouts in the Android Studio editor because it complains it cannot find the <Layout> and <data> classes. I could run the project though.
I followed the instructions mentioned in https://developer.android.com/tools/data-binding/guide.html. Although there are remarks about lacking Android Studio integration i couldn't find anything about not being able to view the Layout files, as it would be a major drawback.
I have setup
classpath 'com.android.databinding:dataBinder:1.0-rc1'
and
apply plugin: 'com.android.databinding'
compile 'com.android.support:appcompat-v7:21.0.3'
in the gradle files.
I also have the latest Android Studio installed and i already rebuild my project.
EDIT:
I found https://code.google.com/p/android/issues/detail?id=176274 discussion. They are mentioning this issue to be fixed in AI-141.2006197 and i am running AI-141.2017176 which should be later and therefore containing this feature, right?
EDIT 2:
Updated now and also tried with AI-141.2218876, still the same issue.
Make sure the <layout> tag is not capitalized in your XML file! I see that it is capitalized in your question. That won't work.
If this isn't the solution, please update our question to include the full activity and XML layout and stack trace of the build failure.
Related
I'm trying to include in my project GreenDAO ORM, useless to say that there is no good tutorial that makes it work from scratch and there is ridiculous documentation on the official site. I used this greendao tutorial and made all intermediary steps to generating classes with gradle task. But now I'm stuck, Android Studio gives me an error for every generated class like:
Cannot resolve symbol 'AbstractDaoMaster'. All imports are actually in place but there is no way to make it work.
Things I tried:
invalidating caches
updating AS to 1.2.1.1
I have iMac OS X Yosemite 10.10.2
I have no clue what to do next.
The documentation on the official site it's a bit old, for Eclipse projects. Using Android Studio I always follow this tutorial, it worked always like charm.
I am sorry I didn't find your question in time - the answer was already in the 2nd part of the tutorial you mentioned:
compile files('libs/greendao-1.3.7.jar')
For the future, I really recommend adding Gradle dependency for greenDAO, instead of a jar file. It's more flexible and easy this way. Just add compile 'de.greenrobot:greendao-generator:2.1.0' to build.gradle and sync it. You can always check the last version here.
See this blog post for a step by step tutorial with explanation.
I actually had to update build.gradle in the app module with:
compile files('libs/greendao.jar')
and manually sync gradle scripts with project files. After that, all dependencies are met.
Groovy's 2.4 release comes with official support for android app development http://docs.codehaus.org/display/GROOVY/Groovy+2.4+release+notes.
Having previously used eclipse for Android development, with no experience at all with Android Studio nor with Gradle, the existing instructions for setting up groovy android projects with Android Studio (e.g. http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-plugin, or http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html) are too vague, do not work for me, and the difference in version numbers used by them is confusing.
I'd like to see detailed step-by-step instructions that work for Android Studio 1.2.1.1 for Linux, and explain how to decide on the version numbers.
EDIT: In 2019, almost 4 years after the question and this answer, the method described below does not work anymore with the current Android Studio version (3.3.2). I could not find versions of the dependencies that still work, and Gradle complains about not being able to download some dependencies.
The fact that 4 years later there is still no reproducible setup guide by the Groovy maintainers is just one indicator that Groovy for Android has no future. We should have seen discussions on patterns regarding how Groovy's dynamic abilities simplify Android development long ago, but this has not happened. I'm going to try Kotlin next, which seems similar enough to Groovy on first glance, apart from the required type annotations, but most important: one does not have to fight so hard against Android Studio to use it.
The original answer follows, it was for a very specific Android Studio version, extended with guesses how to adapt versions of dependencies in future Android Studio versions. According to the comments, this has worked until Android Studio 3.0.1.
After learning by myself how to do it, this is the step-by-step description that I would have liked to read when I started. I have used information from a number of sources. None of these sources worked for me, and they differed in version numbers and where to insert things into build scripts. These instructions are the result of trying out various combinations and filling in some blanks.
From the welcome screen, select "Start a new Android Studio project".
Fill in the app name and a "company domain".
No changes necessary on this page.
This example uses the Blank Activity.
This example uses the name GroovyActivity for the main android screen. The new project now gets created. When android studio has finished creating the project, it shows two open files like this:
Ignore the "rendering problem" for now. It will be resolve itself later during compilation. Close the two open files by clicking on the crosses of their tabs.
Open this build.gradle (Module: app) file:
Insert the following code before the first line:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Let's pause for a while to reflect about the version numbers. The lines
classpath 'com.android.tools.build:gradle:1.1.0'
and
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
contain version numbers. How do you know which version number to insert here? At the time you read these instructions, there might have been new releases and unless you have specific version requirements, you will want to use the latest version of whatever it is that is used here in the gradle script.
How can you learn about the latest available versions? It depends:
Note that the editor has highlighted com.android.tools.build:gradle:1.1.0 with a yellow background color. When you hover the mouse pointer over this text, a tooltip appears:
So it seems that android studio knows about the latest version for this thing and advises you to use it. Heed the advice and update this version number to what your version of android studio indicates.
However, no such tooltip appears for this gradle-groovy-android-plugin thing.
What is happening here, anyway? The way I understand it, these names and version numbers identify names of binary components that are used for your app or for building your app, but neither are they part of android studio, nor are you expected to find and install or compile them yourself. Instead, this build script specifies a repository location where these binary components can hopefully be downloaded from. The function jcenter() seems to return the location of the repository. Currently, this repository is bintray.com.
To learn about the latest version of this gradle groovy android plugin, visit bintray.com with your webbrowser and use its search function. Search for groovy-android-plugin. The beginnig of the result looks like this at the time I write this:
The sheer number of listed results is a bit discouraging. I hope they have some relevance sorting in there. Searching through the first page, I only see 2 relevant matches, and the latest version number is 0.3.6.
Having learned about the latest version numbers (at the time of this writing), the correct start of the gradle script is this code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Back to modifying the gradle script, insert the following code after the "apply plugin: 'com.android.application'" line:
apply plugin: 'groovyx.grooid.groovy-android'
Finally, near the end of the gradle script, insert a line
compile 'org.codehaus.groovy:groovy:2.4.1:grooid'
so that it looks like this:
Again, there is a version number here, you can find the latest version on bintray by searching for *grooid.jar (yes, with the star as first character). The result list spans two pages, on the second page I find that 2.4.3 is the latest version:
After changing the gradle file, gradle needs to be "sync"ed. The gradle will sync if you exit android studio completely and restart it, so this is what I've done at this point. This picture shows the syncing after the restart:
Later I found that this icon in the toolbar
triggers the sync.
After the syncing finishes, change the left pane from the "Android" view to the "Project" view with the pull-down menu:
Now build and execute your project in the emulator at least once. I'm not sure what this changes, but if you do not build and execute your project now, while everything is still java sources only, then your groovy sources will not be found after we change to groovy. You can build and run your project using the run icon in the toolbar or with the keyboard shortcut shift-F10.
After executing your project in the emulator, turn back to the left pane, which still shows the "Project" view. In this view, navigate to the app/src/main directory, which currently contains the subdirectories java and res, and the AndroidManifest.xml file.
You will now create an additional "Java Folder" below main, with the help of the context menu:
Be sure to use the menu item New->Folder->Java Folder, and not New->Directory, which would cause problems later on.
In the next screen,
tick the check box "Change Folder Location" and make sure the name in the entry field is src/main/groovy.
If you still have that gradle script open, you will see that it has been adapted and contains the name of the new directory in a line starting with "sourceSets". For good measure, click on the gradle sync icon to make sure everything is properly synced.
Next you want to achieve that the new groovy folder contains the proper package path for the package name of your app. The only way that I have found to achieve this properly, is to create a new, temporary, java class with the context menu of the groovy folder,
and enter the name of this class fully qualified with the package name:
Make sure the spelling of the package name is correct!
The new package will appear in the groovy folder, and inside it, the temporary java class.
All groovy classes and also all java classes that make in some way use of groovy classes have to live below the src/main/groovy directory instead of the src/main/java directory. We can now drag the Activity class from the java directory tree to the same package below the groovy directory:
The temporary class can now be deleted (with the context menu), and the Activity class can be migrated from .java to .groovy by choosing Refactor->Rename File from the context menu.
The file extension .java is simply replaced with a .groovy extension. In the same way, you can later create new java classes and rename their file to .groovy when you actually want to create groovy classes.
You can now change some code in the activity to verify that you can actually have groovy code in an android app. Suggestion:
Give the "Hello World" text view an id in the res/layout/activity_groovy.xml file, like this:
Then, programmatically change the text shown by this view using groovy's string interpolation:
Build and execute in emulator:
I'm needing help again with my strange projects ;)
So I'm still building a library, and I need to use libA-1.01.jar and libA-2.1.jar
Of course I don't want them in the same flavor, so I used in my gradle file:
flavorACompile files('libs/libA-1.01.jar')
flavorBCompile files('libs/libA-2.1.jar')
This works fine when compiling with gradlew in command line. But the issue is that my classes don't see both libraries. So when I'm coding I can only import the first one (1.01). So I can't use my IDE to code properly I have to guess emacs style if everything is ok and compile to see...
How can I tell Android Studio that the 2 libraries are present? I tried added them both to compile without flavor (just to force android studio to use them) but it didn't work.
I'm sure there is a simple way to do this, but I can't seem to find it :(
I have followed the procedure as described here : Setup
I have clicked on the little 'Sync project with Gradle' button. Gradle and Android Studio seem to find everything but then I can't actually use the gms code. If I try to import, I will get autocomplete for com.google.android.gms but no further. I have updated all the packages with Android SDK Manager.
I'm running Android Studio 0.4.2.
My minSdk is set to 9
my build.gradle includes compile 'com.google.android.gms:play-services:4.0.30'
As far as the procedure is concerned I should be ready to code, but it just doesn't work. Any ideas?
[Edit, added info]
I can find the ComGoogleAndroiddGmsPlayServices3265.aar file in my exploded bundles directory. Inside of that file I also find the common directory and inside that I find the GooglePlayServicesUtil.class (which is what's not being found in my app)
I am lost.
[Edit 2]
The problem is not specific to Google Play Services OR Android Studio. I tried adding another library (HoloColorPicker) and had the same results. However, I was able to add the library's resources to my project! I was able to add them in my XML layouts and view them in my application. I was able to interact with them, they worked fine. The problem arose again when I tried to reference them in the code. Exactly like the case with gms, I had code completion when trying to import up to the point of the actual class, and I could not declare the class in the code.
I was able to use the library by cloning it and importing the project.
Also, this is not an Android Studio problem because the same thing happens on the command line with "./gradlew clean build"
This is a current bug in Android Studio: https://code.google.com/p/android/issues/detail?id=64508 to be fixed this week.
The workaround is to close the project, delete the .iml files and .idea project and re-import the project.
Keep Your compile 'com.google.android.gms:play-services:4.0.30' as very first line in build.gradle dependencies like
dependencies {
compile 'com.google.android.gms:play-services:4.0.30'
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:+'
}
`
Open File> Project Structure and do the following steps
Select your main module in which you want to add dependency and click on OK.
Now try to import.
I think the most important question is what you want to achieve. Not all code is under this package. Not even sure which one is.
As noted before, this is a bug with Android Studio. It just don't recognize the path for classes and shows you like if there is an error. If you try compiling you'll see that everything just work fine.
A googler recently said it'll be addressed in this week release, so, be patient and lets see whats coming.
The fact that AS is in Preview mode tell us this sort of things are going to happen :)
Android studio is crazy one, I think.
It's removed "Import module" function and you can do "New module" only.
If you are developed on Eclipse, you need export all your projects to Gradle before switch to Androids studio (WTF?)
I prefer "IntelliJ IDEA Community Edition", although It's similar Android studio but it's better than Android studio (at least until now). You just import your project as eclipse format, IntelliJ IDEA will detect dependences libraries automatic (May be you need import jar libraries by hand) and rebuild project. That Done.
In my app, I am using Pull TO Refresh list view library. I had designed layouts for all screens respectively. When I ran lint on my project, in one of my layout file for xlarge screen, it gave me fatal error:
Class referenced in the layout file,
com.handmark.pulltorefresh.library.PullToRefreshListView, was not
found in the project or the libraries
While in same layout file for other screens, it is giving no error at all. Also, I had used same library in other layouts of my file, where it does not give any error. It is giving same error for Google Maps api as well. My project targets Google Api level 16 and I had checked, all libraries are added to it. But,the error doesn't go away even on cleaning project.
This is a lint bug and you can safely ignore it. Lint is meant to help, not to hinder your development time. Change the lint Errors to warnings and you can compile and run the app just fine. I don't recommend turning it off because it helps to keep your code cleaner.
Make a project Project -> Clean... this resets the bug errors for me.
I was also getting a similar error, for me, it was resolved by adding android.enableJetifier=true to gradle.properties.
You can suppress the lint error by, passing the error type to the ignore attribute within that view, for example:
<com.handmark.pulltorefresh.library.PullToRefreshListView
...
tools:ignore="MissingClass"
...
/>
Just make sure you have tools imported:
xmlns:tools="http://schemas.android.com/tools"
References:
https://googlesamples.github.io/android-custom-lint-rules/checks/MissingClass.md.html
It happened to me when i changed thé android studio IDE, To solve it:
-reate a new project;
-replace the content of dependencies in build.gradle of your project with the content of dependencies in build.gradle of the new project;
-sync project with gradle.
And it will work.