I'm trying to use data-binding with Android.
I can not anymore build my project. I got this error :
"Error:(13, 46) error: package ch.company.project.databinding does not
exist"
Here my gradle :
http://pastebin.com/dkXd1Mxr
and
http://pastebin.com/n9hkFWGQ
And here the gradle output :
https://pastebin.com/w93Rausg
Thanks to Yigit!
The issue was not directly link to Android Databinding.
There were a bug in the project (some variables not correctly setted)
I would recommend to use gradle with "--debug and --stacktrace" for more informations, it's helping a lot.
earlier my package name was "com.xyz.abc.Models"
changing the package name to all small letters "Models" -> "models"
solved the issue.
The bug is not the DataBinding Package, it's a syntactic or logical error. For example, you have the attribute "lastName" in your POJO, but in the layout it's android:text="#{user.lastname}".
Check your "layout" and do Rebuild Project.
I am not satisfied with accepted answer, that tell you to stack trace without hints.
Here are some possible causes that lead to this problem. Check if you are not doing any of the following.
Basically Android DataBinding is not that mature still. It will fail without appropriate errors many times.
So if you have an issue like package ch.company.project.databinding does not exist".
Possible causes of fail:
First of all check your recently edited layouts xml one by one for errors (for wrong imports & variables). I don't get proper error in this case usually.
Check your data binding syntax in binding block ({...}) in layout element for errors. Always Rebuild (not Build) project after working in one layout.
Check your #BindingAdapter method having correct parameters. For example imageUrl binding adapter would accept ImageView or View as first parameter.
You should always Rebuild project after doing work in one layout.
If you are not able to find errors by above steps, then try --debug and --stacktrace in compile option of
File> Settings> Build, Execution, Deployment> Compiler> Command-line Options
Make sure your package name start with lowercase letter.
in my case issue solved after two hours of struggle
Package name should start with small letter.
for example Activities is wrong it'll give an error instead refactor->rename to activities
I got the error:
Error:(9, 46) error: package com.company.www.bar.databinding does not
exist.
i just remove "=" sign . it worked for me
From this :
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="#={()->activity.onButtonClick()}"/>
to :
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="#{()->activity.onButtonClick()}"/>
I had similar problems with my project
You could try:
check xml files for errors that cause a build failure
clean project
File -- invalidate caches / restart
On my particular case, I was using Dagger 2. This package error appears in a lot of classes without any relation with the original error: a dependency injection error.
Happy reminder: Scroll more on your stacktrace to know what is the real problem.
I was stuck with same error for hours. After trying several solution from stackoverflow, I updated my project with stable gradle dependencies.
Still it was not solved, however with the same gradle dependency DataBinding was working fine in another project of mine.
So, I went project folder using explorer and Deleted 2 things.
build folder
all files from .idea/libraries
After that i synced the project and it continued to work just fine.
Package names have to START with Small Letters. Otherwise, Binding library cannot understand that is it class or package. Moreover, you do NOT need to do all of it with small letters.
Example, wrong usage:
package com.thecompany.activity.ContactInfo; //Problem is ContactInfo, 'C'.
Example, TRUE usage:
package com.thecompany.activity.contactInfo; //Solution is contactInfo, 'c'.
Make sure your model's fields you reference in layout have public access modifiers
Change
{ databinding = true}
to
buildFeatures{
dataBinding = true
}
If you're coming to this question because you switched to JDK11 in Android Studio Artic Fox and your view binding broke in the UI but not during execution then be aware that this is a known issue and should be resolved in Bumble Bee:
https://issuetracker.google.com/issues/180946610
The current fix is to switch back to JDK8 (or install the Bumble Bee canary release).
To get rid of this error just enclose your complete layout design inside a plain layout tag in the activity_main.xml file.
After wasting many hours in finding solution this worked for me. Give it a try.
if you tried this steps
invalidate/restart`
keeping this properties in gradel.properties
android.databinding.enableV2=false
android.enableExperimentalFeatureDatabinding=true
and checking all xml files looks good.
then you should go with this solution, add below code in project level build.gradle
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "1000"
}
}
}
this will give you exact error where you have actual error
explanation: above code will increase the size of the compile error
in my case, i follow the android documentation :
buildFeatures {
viewBinding true
}
use "=" instead of space
buildFeatures {
viewBinding = true
}
Try following Refactor -> migrate to androidx
and in the build.grade(:app)
implementation 'androidx.appcompat:appcompat:1.0.0'
or use new version is released
implementation 'androidx.databinding:databinding-runtime:4.1.0'
Make sure that if your layout filename is named in the following format: <name>_activity.xml that your binding class name complies the following format as well: <name>ActivityBinding
For me, changing my layout filename from activity_login.xml to login_activity.xml resolved this issue because my binding class name was LoginActivityBinding.
Here's an except from the Android Layouts and binding expressions page mentioning this:
A binding class is generated for each layout file. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main.xml so the corresponding generated class is ActivityMainBinding
if you use a model in your layout, make sure you dont have the model and the package named same and also the paackage name should start with small letter.
i changed mine from Model>Model.class to modelPac>Model.class
In my case the problem appeared when I was creating productFlavors and set sourceSets.
Changing
sourceSets {
develop {
res.srcDirs = ['myApp/src/develop/res']
}
to
sourceSets {
develop {
res.srcDirs = ['src/develop/res']
}
}
solved my issue.
Related
I've just tried to generate signed apk for one of my projects (I already did this before), but (maybe since updating Android Studio) I'm getting
Error:Error: Expected resource of type string [ResourceType]
This is because I'm using Butterknife's #BindString, that is generated into something like that
target.settings = res.getString(2131230792);
How can I make studio not detect this as error? I've tried searching in settings, but without success.
Answer to this is: disable lint rule in your build.gradle
android {
lintOptions {
disable "ResourceType"
}
}
Edit:
This may happen particularly when migrating from Eclipse to Android Studio.
This is reported on the GitHub project.
It will be fixed in the next version of ButterKnife.
The workaround is indicated there, and is to add a lint.xml file on the app module with the following content to ignore that errors on *$$ViewBinder classes (the ones that ButterKnife generates):
<issue id="ResourceType">
<!-- Remove this when this is fixed: https://github.com/JakeWharton/butterknife/issues/338 -->
<ignore path="**/*$$ViewBinder.java" />
</issue>
Maybe a better solution is to temporary disable error/warning by using #SuppressLint("ResourceType") just before the method definition.
I has a similar problem. with a getString method. Turned out i was trying to get the string value not from strings.xml but from ids.xml, because i was getting it with getString(R.id.MYSTRING), when it should be R.string.MYSTRING
this is for some Views by same id and u try to change some property for one of them.
When try to generate apk android find some confilict about resours id
To solve that
better way find the code and try solve that from another way to your purpose
GoodLuck
I have some code snippets below when I use android databinding framework with ViewStub.
ItemPostBinding binding=ItemPostBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
final Post post = mDataset.get(position);
binding.stub.getViewStub().setLayoutResource(App.getPostExtensionManager().getLayout(post.getExtension()));
I kown binding.stub will be replaced with ViewStubProxy when codes compiled. But how could I ask Android Studio to ignore the error here before compiling?
(The error is Android Studio cannot resolve getViewStub() method from class ViewStub)
UPDATE 1
I don't know if it was due to my old approach of using android data-binding framework.
classpath 'com.android.databinding:dataBinder:1.0-rc4' // project build.gradle
apply plugin: 'com.android.databinding' // module build.gradle
But with the new method mentioned in the official data-binding guide, all you need to do is adding the settings below in your module's build.gradle file.
android {
....
dataBinding {
enabled = true
}
}
And the error inspections mentioned above is gone, you can even directly use binding.stub without any problem.
binding.stub.setLayoutResource(...)
binding.stub.inflate()
...
UPDATE 2
You can use binding.stub.someViewStubMethod() directly but it will fail while compiling. You still need to use binding.stub.getViewStub().someMethod(). However, just recently the error inspections is gone somehow no matter which method you add data-binding into your project by.
You are not suppose to call getViewStub method this way. binding.stub is already the ViewStub which you put in your layout item_post. You should call binding.stub.setLayoutResource()
Android Studio can't recognize ViewStubProxy correctly. Just cast it before manipulate.
import android.databinding.ViewStubProxy;
...
ViewStubProxy viewStubProxy = (ViewStubProxy)(Object)mBinding.viewStub;
if (!viewStubProxy.isInflated()) {
viewStubProxy.getViewStub().inflate();
}
Error is gone :)
I am trying to create a Android Application which uses 3 spinners. I keep getting this error and I can't figure out how to fix it.
This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView)
On the "v7-appcompat" library:
preferences -> Android Lint Preferences
Search for "Instantiatable" and set to Warning.
If you are using Eclipse:
Project > Clean > OK
Or Try:
Preferences -> Android Lint Preferences
Search for Instantiatable and set as Warning.
http://developer.android.com/tools/debugging/improving-w-lint.html
This worked for me.
Go to appcompat_v7 library -> Properties -> Android Lint Preferences -> Instantiatable -> Select "Warning" severity.
Android Lint Checks
Instantiatable
Summary: Ensures that classes registered in the manifest file are instantiatable
Priority: 6 / 10 Severity: Warning Category: Correctness
Activities, services, broadcast receivers etc. registered in the
manifest file must be "instiantable" by the system, which means that
the class must be public, it must have an empty public constructor,
and if it's an inner class, it must be a static inner class.
I found a solution, in the project.properties file, just replace the line saying
android.library.reference.1=../appcompat_v7_3
with
android.library.reference.1=../appcompat_v7
That solved the problem to me at least.
You can also do this manually in the build.gradle file:
lintOptions {
abortOnError false
disable "Instantiatable"
}
This is not a true solution, but maybe help.
Create project with higher API level then 8.
Whenever I select minimum Required SDK to API level 9 instead 8 this error does not happen.
Observation: My last answer was deleted, maybe the moderator think it was barely more than a link to an external site, for this reason I don't posted the link again.
Eclipse inside the setup issues. Windows -> Preferences -> Android ->
Lint Error Checking
-> Enter "Instantiatable" Inside the Issues -> select the Search Content -> lower right corner Severity election warning -> OK!
I know this question is pretty old. However, would like to add in the updated answer to resolve this issue.
android {
.
.
.
lintOptions {
warning("Instantiatable")
}
}
Add the above lines to your gradle file.
Basically what this does is, it tells the linter to treat Instantiatable rule as a warning and not error.
When I add a packageNameSuffix to my build.gradle debug build type (see https://plus.google.com/108967384991768947849/posts/XGXH3arvbrK), all of my Robolectric tests are failing due to the following:
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f050000
at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:354)
at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:387)
at android.content.res.Resources.openRawResource(Resources.java)
at com.myapp.utilities.CSVUtility.parseRawResourceCSV(CSVUtility.java:38)
The problem seems to be that the raw folder src/main/res/main no longer being found. This folder contains a csv which is parsed at application start... which is where the tests go boom.
Architecture/data restructuring suggestions aside (I know CSV may not be the best way to get this data loaded at app start), does anyone know how I might remedy this problem?
Edit: I tried moving the file to the assets folder instead, and then my tests failed on a Context.getString() call. Resources look to be getting completely hosed when adding packageNameSuffixes.
Edit: tmurakami posted on the github issue - https://github.com/robolectric/robolectric/issues/1001#issuecomment-42740897
I've copied the full response:
Try using this gradle snippet.
This works fine in my environment.
def hasLibraryVariants = android.hasProperty('libraryVariants')
def variants = hasLibraryVariants ? android.libraryVariants : android.applicationVariants
tasks.withType(Test).whenTaskAdded {
it.systemProperty 'android.package', variants.iterator().next().processResources.packageForR
}
The original package name has been stored in the following fields of any variant:
variantData.variantConfiguration.originalPackageName
processResources.packageForR
generateBuildConfig.buildConfigPackageName
However these are internal only, so might become inaccessible in the future.
If you don't want to use these fields, try the following snippet:
tasks.withType(Test).whenTaskAdded {
it.systemProperty 'android.package', android.defaultConfig.packageName
}
To use this, you need to add the main package name in the 'android.defaultConfig' section.
android {
defaultConfig {
packageName 'main.package.name'
}
}
Looks like I need to add an android.package system property for the package name. See this issue conversation on Github - https://github.com/robolectric/robolectric/issues/1001
I'm trying to use Android annotations framework because it seems quite powerful. I'm quite stuck to configuring my first project based on it.
I followed every step of the wiki but it doesn't generate any file after a build.
So when I ask for a generated class from the manifest:
<activity android:name=".MyActivity_"
android:label="#string/app_name">
I get an exception:
java.lang.ClassNotFoundException
My activity is exactly the same one as in the wiki:
#EActivity(R.layout.main)
public class MyActivity extends Activity {
#ViewById
EditText myInput;
#ViewById(R.id.myTextView)
TextView textView;
#Click
void myButton() {
String name = myInput.getText().toString();
textView.setText("Hello "+name);
}
}
Any ideas?
EDIT: Just found out a directory ".apt_generated" is made but it's empty after the build.
This seems to be an AndroidAnnotations bug, and should be reported on the dedicated bug tracker, here : http://code.google.com/p/androidannotations/issues/entry . You could also use the AndroidAnnotations mailing list, http://groups.google.com/group/androidannotations
First, I have a few questions :
Which IDE do you use : Eclipse, Netbeans, IntelliJ ? Which version ?
Do you use Maven, Ant, or only your IDE to build the project ?
Your problem may be due to a few things : annotation processing not triggered, a bug in AA, or the files generated in a folder not part of the classpath.
In Eclipse, you may get more information from the "Window > Show View > Error Log" view. If annotation processing is triggered, you should see some messages about AndroidAnnotations.
For other people who are running into this and the leading answer doesn't work, run a build and then search for the file androidannotations.log somewhere in the project. This log file is generated and may hint at what is wrong.
For me, it had a warning message that it could not locate AndroidManifest.xml. Though this seemed like just a warning, it was actually the cause of the error... Not finding my AndroidManifest.xml file resulted in it not generating some of the classes it should have.
Check if you have the xml file. If not, the solution is obvious. If you do have it, the typical reason AA cannot find the file is because it is in a non-standard location -- AA recursively checks the parent directories above where it generates files for this xml file and will fail if it's not there. In my case, my AndroidManifest.xml was located in [project root]/app/src/main which is not a direct ancestor folder so that was the problem.
You can specify where your xml file is in your project build.gradle:
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["androidManifestFile": "specify_location_of_AndroidManifest.xml_here"]
}
}
}
}