Skip to content

Unable to use import androidx.appcompat.widget.Toolbar into a java file when using AndroidJavaSource #8191

@opcodewriter

Description

@opcodewriter

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS 2022 Version 17.6.5

Description

In my Android .NET 7 app, I have a java file where I'm trying to import Toolbar from androidx.appcompat.widget:

package androidapp5;

import androidx.appcompat.widget.Toolbar;

But it gives the following compilation error:

Severity	Code	Description	Project	File	Line	Suppression State
Error	JAVAC0000	 error: package androidx.appcompat.widget does not exist
import androidx.appcompat.widget.Toolbar;

This is the csproj:

  <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-android</TargetFramework>
    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
    <OutputType>Exe</OutputType>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationId>com.companyname.AndroidApp5</ApplicationId>
    <ApplicationVersion>1</ApplicationVersion>
    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.3" />
  </ItemGroup>
</Project>

I also tried to manually add the following in the csproj but it didn't make any difference:

  <ItemGroup>
    <AndroidJavaSource Include="Interop.java" />
  </ItemGroup>

Note that when using the Toolbar class in C# code, it compiles OK:

using Toolbar = AndroidX.AppCompat.Widget.Toolbar;

namespace AndroidApp5
{
    [Activity(Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            Toolbar toolbar;
        }
    }
}

Steps to Reproduce

Just create a Android .NET 7 app, add a file called Interop.java like above.

Here's the sample:

AndroidApp5.zip

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions