Skip to content

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

Closed

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

Activity

  1. added
    needs-triage
    on Jul 15, 2023
  2. moljac commented on Jul 16, 2023

    @moljac
    Contributor

    MacOSX

    net8.0

    dotnet build AndroidApp5/AndroidApp5.csproj /bl
    

    output:

    MSBuild version 17.7.0-preview-23251-02+59879b095 for .NET
      Determining projects to restore...
      Restored /Users/moljac/Downloads/12063900/AndroidApp5/AndroidApp5.csproj (in 686 ms).
    /usr/local/share/dotnet/sdk/8.0.100-preview.4.23260.5/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/Users/moljac/Downloads/12063900/AndroidApp5/AndroidApp5.csproj]
    /Users/moljac/Downloads/12063900/AndroidApp5/MainActivity.cs(15,21): warning CS0168: The variable 'toolbar' is declared but never used [/Users/moljac/Downloads/12063900/AndroidApp5/AndroidApp5.csproj]
      AndroidApp5 -> /Users/moljac/Downloads/12063900/AndroidApp5/bin/Debug/net7.0-android/AndroidApp5.dll
    
    Build succeeded.
    
    /Users/moljac/Downloads/12063900/AndroidApp5/MainActivity.cs(15,21): warning CS0168: The variable 'toolbar' is declared but never used [/Users/moljac/Downloads/12063900/AndroidApp5/AndroidApp5.csproj]
        1 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:53.47
    

    msbuild.binlog.zip

  3. added
    Area: App+Library Build
    and removed
    needs-triage
    on Jul 17, 2023
  4. dellis1972 commented on Jul 17, 2023

    @dellis1972
    Contributor

    Thanks for the repo app.

    I have replicated the issue. There is a bug in the AndroidJavaSource support. It will work with basic java types, however it does have a problem when you try to reference .jar files which are extracted from NuGet packages.

    In this case the androidx.appcompat has a classes.jar file, but it is inside an .aar file. This .aar file is extracted to the obj\Debug\net7.0-android\lp\ directory but these directories are NOT included in the list of file needed to compile the code in your .java file.

    We will need to update our build system to include these files.

  5. opcodewriter commented on Jul 17, 2023

    @opcodewriter
    Author

    @dellis1972 Thank you. Any timeline for the fix? Will it be fixed in .NET 7? Or is it already fixed in .NET 8?
    I need this to work in .NET 7...

    Any workaround at the moment?

  6. dellis1972 commented on Jul 17, 2023

    @dellis1972
    Contributor

    @opcodewriter Its not fixed anywhere at this time. I'll need to figure out what needs to get fixed first.
    It might make the final .net 8 release. I'm not sure if it will get ported back to .net 7.

    I'll try adn figure out a work around for you

  7. dellis1972 commented on Jul 17, 2023

    @dellis1972
    Contributor

    @opcodewriter

    ok try adding this to your .csproj just above the final </Project> element.

      <Target Name="_WorkaroundRemoveMeAtSomePoint" BeforeTargets="_DetermineBindingJavaLibrariesToCompile" DependsOnTargets="_GetLibraryImports">
      </Target>
    

    See if that fixes the issue.

  8. opcodewriter commented on Jul 17, 2023

    @opcodewriter
    Author

    In the actual sample app, there's no more compilation error, but when I try to use it in some actual code, I get this error and many warnings:

    Error	JAVAC0000	warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
      reason: class file for androidx.annotation.RestrictTo$Scope not found
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.TESTS
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.TESTS
    warning: unknown enum constant Scope.TESTS
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant AnnotationRetention.BINARY
      reason: class file for kotlin.annotation.AnnotationRetention not found
    warning: unknown enum constant AnnotationTarget.CLASS
      reason: class file for kotlin.annotation.AnnotationTarget not found
    warning: unknown enum constant AnnotationTarget.PROPERTY
    warning: unknown enum constant AnnotationTarget.LOCAL_VARIABLE
    warning: unknown enum constant AnnotationTarget.VALUE_PARAMETER
    warning: unknown enum constant AnnotationTarget.CONSTRUCTOR
    warning: unknown enum constant AnnotationTarget.FUNCTION
    warning: unknown enum constant AnnotationTarget.PROPERTY_GETTER
    warning: unknown enum constant AnnotationTarget.PROPERTY_SETTER
    warning: unknown enum constant AnnotationTarget.FILE
    warning: unknown enum constant AnnotationTarget.TYPEALIAS
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
     **error: cannot access LifecycleOwner
    		mainActivity.setContentView(activityLayoutId);**
      class file for androidx.lifecycle.LifecycleOwner not found
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    warning: unknown enum constant Scope.LIBRARY_GROUP_PREFIX
    
    public static void initMainActivity(AppCompatActivity mainActivity, int activityLayoutId, int toolbarId) {
    		mainActivity.setContentView(activityLayoutId);
    		
    		Toolbar toolbar = (Toolbar)mainActivity.findViewById(toolbarId);
    		 mainActivity.setSupportActionBar(toolbar);
    	}
    
  9. opcodewriter commented on Jul 17, 2023

    @opcodewriter
    Author

    same error and warnings. Note my actual code is in a Android .NET 7 library, not an app like in my sample, not sure if it matters.

  10. added 4 commits that reference this issue on Jul 18, 2023
    0595994
    5c6f6fa
    f195e91
    993c3f6
  11. added a commit that references this issue on Aug 22, 2023
    3269d4a
  12. opcodewriter commented on Oct 4, 2023

    @opcodewriter
    Author

    Is this going to be released in .NET 8?

    Thanks.

  13. locked and limited conversation to collaborators on Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: App+Library Build

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions