Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
opcodewriter opened this issue Jul 15, 2023 · 9 comments · Fixed by #8194
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@opcodewriter
Copy link

opcodewriter commented Jul 15, 2023

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

@opcodewriter opcodewriter added the needs-triage Issues that need to be assigned. label Jul 15, 2023
@moljac
Copy link
Contributor

moljac commented Jul 16, 2023

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

@dellis1972 dellis1972 assigned dellis1972 and unassigned jpobst Jul 17, 2023
@dellis1972 dellis1972 added Area: App+Library Build Issues when building Library projects or Application projects. and removed needs-triage Issues that need to be assigned. labels Jul 17, 2023
@dellis1972
Copy link
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.

@dellis1972 dellis1972 added this to the Under Consideration milestone Jul 17, 2023
@opcodewriter
Copy link
Author

opcodewriter commented Jul 17, 2023

@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?

@dellis1972
Copy link
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

@dellis1972
Copy link
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.

@opcodewriter
Copy link
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);
	}

@moljac
Copy link
Contributor

moljac commented Jul 17, 2023

@opcodewriter

Try adding Xamarin.AndroidX.Annotation.Jvm package reference:

https://www.nuget.org/packages/Xamarin.AndroidX.Annotation.Jvm

dotnet/android-libraries#727

@opcodewriter
Copy link
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.

dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Jul 18, 2023
….widget.Toolbar into a java file when using AndroidJavaSource

Fixes dotnet#8191

TODO
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Jul 19, 2023
….widget.Toolbar into a java file when using AndroidJavaSource

Fixes dotnet#8191

TODO
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Jul 20, 2023
….widget.Toolbar into a java file when using AndroidJavaSource

Fixes dotnet#8191

TODO
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Jul 21, 2023
….widget.Toolbar into a java file when using AndroidJavaSource

Fixes dotnet#8191

TODO
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Aug 22, 2023
….widget.Toolbar into a java file when using AndroidJavaSource

Fixes dotnet#8191

TODO
jonpryor pushed a commit that referenced this issue Sep 21, 2023
…8194)

Fixes: #8191

The implementation of `@(AndroidJavaSource)` was missing one component:
if your project contains `@(PackageReference)`s to NuGet packages which
pull in `.jar` files, those `.jar` files would *not* be referenced when
building `@(AndroidJavaSource)`.

For example, if you reference the [Xamarin.AndroidX.AppCompat][0]
package, there is an expectation that `@(AndroidJavaSource)` code
should be able to use the type `androidx.appcompat.widget.Toolbar`.

Unfortunately, this would fail:

	Error	JAVAC0000	 error: package androidx.appcompat.widget does not exist

This would fail because the `javac` invocation was missing references
to the `classes.jar` files which are extracted into the
`$(IntemediateOutputPath)lp` directory.

Update the (internal) `$(_CompileBindingJavaDependsOnTargets)` property
so that the `_GetLibraryImports` target is a dependency.  This will
call the chain of targets which extracts the dependent `classes.jar`
files and populates the `@(Jars)` ItemGroup which is used in the
`_CompileBindingJava` target.  This should allow users to write simple
wrapper methods in Java that wrap more complex APIs.

[0]: https://www.nuget.org/packages/Xamarin.AndroidX.AppCompat/1.1.0.1
@opcodewriter
Copy link
Author

Is this going to be released in .NET 8?

Thanks.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
4 participants