7

I notice that with Java8 there is no problem with viewBinding in AndroidStudio (Arctic Fox).

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
    viewBinding true
}

However, once I set compileOptions to JavaVersion.VERSION_11, AndroidStudio always falsely shows viewBiding(s) errors though it can compile and run with no problem.

I tested this with a fresh new project in AndroidStudio ArcticFox. It suggests to import the class. After imported, it says Package not found:

screenshot 1

screenshot 2

Is this AndroidStudio's bug, or are there any misconfiguration about Java compiler setting in my AndroidStudio?

All I did something particular was JavaVersion.VERSION_11.

hata
  • 11,633
  • 6
  • 46
  • 69
  • Maybe you need to update the Gradle plugin. Faced such kinds of issues due to Gradle plugin on lower AS – Sachin Jul 30 '21 at 06:23
  • @Sachin I think Gradle plugin is recent: `classpath "com.android.tools.build:gradle:7.0.0"` – hata Jul 30 '21 at 06:33
  • Still happening today -.- The project can be built and launched on a physical device but it show the annoying red message. – ieselisra Oct 10 '21 at 08:25
  • This bug is not fixed yet with Patch 4 (Gradle 7.0.4). – hata Dec 09 '21 at 17:28

3 Answers3

16

As google's support answered: This issue has been fixed and landed in BB canary 8. It is specific to Java 11.

Teralser
  • 176
  • 4
  • 3
    Thank you. FYI, I searched about Bumblebee canary then I found this issue tracker: [View Binding not working with Java 11](https://issuetracker.google.com/issues/180946610?pli=1). I'll wait next stable update. – hata Aug 11 '21 at 11:18
  • With stable Bumblebee the problem has actually been fixed! – hata Jan 27 '22 at 11:26
0

remove the import line from your project and import it again by pressing Alt+Enter then inform me

import com.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {   

ActivityMainBinding binding;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

}

}
0

I had this issue, and I did the following:

  • Run the gradle updater
    OR
  • open the terminal and run gradlew help --scan, it will generate a report that tells will what you should edit before migrate to Gradle 7.0
  • open your gradle.properties and distributionUrl to https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
  • Go to your Project build.gradle and set the classpath to com.android.tools.build:gradle:7.0.0.
  • Sync with Gradle FIles and rebuild your project.
Amjad Alwareh
  • 2,926
  • 22
  • 27
  • 1
    Thank you for your information. My test project is a newly created project and it uses Gradle 7.0 (gradle-wrapper.properties: gradle-7.0.2-bin.zip) from its creation. – hata Jul 31 '21 at 14:50