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

DeleteBinObj bug with AndroidResource #2193

Closed
jonathanpeppers opened this issue Sep 18, 2018 · 4 comments · Fixed by #2199
Closed

DeleteBinObj bug with AndroidResource #2193

jonathanpeppers opened this issue Sep 18, 2018 · 4 comments · Fixed by #2199
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects. bug Component does not function as intended. vs-sync For internal use only; creates a VSTS "mirror" issue.
Milestone

Comments

@jonathanpeppers
Copy link
Member

jonathanpeppers commented Sep 18, 2018

Steps to Reproduce

  1. File | New Project | Android App (Xamarin) | Tabbed App, set Minimum Android Version to Android 5.0 (Lollipop)
  2. Build
  3. Open Resource\layout\activity_main.axml (I used the ancient VS XML editor instead of the designer, to remove that from the equation)
  4. Add a new TextView
  5. Run via F5 or the Play button

A crash at runtime is encountered:

Unhandled Exception:

Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView occurred

On this line:

SetContentView (Resource.Layout.activity_main);

My TextView source is basically the default if dragged from the toolbox in the designer:

  <TextView
      android:text="Text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minWidth="25px"
      android:minHeight="25px"
      android:id="@+id/textView1" />

Once in this state, a Rebuild fixes the problem. You can deploy again to see the issue resolved.

Expected Behavior

Modifying a layout should not cause a runtime exception!

Actual Behavior

Modifying a layout causes runtime exception! Rebuild resolves the issue...

Version Information

I have been able to repro this on VS 2017 15.8.4, and the build of Xamarin.Android we will be shipping with 15.9 P3.

Log File

Relevant logs: DeleteBinObj.zip

VS bug #686581

@jonathanpeppers jonathanpeppers added bug Component does not function as intended. Area: App+Library Build Issues when building Library projects or Application projects. labels Sep 18, 2018
@jonathanpeppers jonathanpeppers self-assigned this Sep 18, 2018
@jonathanpeppers jonathanpeppers added this to the d16-0 milestone Sep 18, 2018
@jonathanpeppers jonathanpeppers added the vs-sync For internal use only; creates a VSTS "mirror" issue. label Sep 18, 2018
@jonathanpeppers
Copy link
Member Author

Hmm, weird classes.dex has the type from the error message!

obj\Debug\MonoAndroid81\android\bin> dexdump.exe .\classes.dex | Select-String 'Landroid/support/design/widget/BottomNavigationView;'

type          : 'Landroid/support/design/widget/BottomNavigationView;'

So perhaps obj\Debug\MonoAndroid81\android\bin\packaged_resources is the file that's messed up?

I'll keep digging...

@jonathanpeppers
Copy link
Member Author

@dellis1972 repro project here: https://github.com/jonathanpeppers/DeleteBinObj

My commands were:

git clean -dxf
msbuild .\DeleteBinObj\DeleteBinObj.csproj /bl:build.binlog /restore
git stash pop
msbuild .\DeleteBinObj\DeleteBinObj.csproj /bl:install.binlog /t:Install

Instead of git stash pop you can just add this to Resource\layout\activity_main.axml:

  <TextView
      android:text="Text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minWidth="25px"
      android:minHeight="25px"
      android:id="@+id/textView1" />

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Sep 19, 2018
Fixes: xamarin#2193

I have been able to reproduce a #deletebinobj bug with the following
steps:
1. `File | New Project | Android App (Xamarin) | Tabbed App`, set
   `Minimum Android Version` to `Android 5.0 (Lollipop)`
2. Build
3. Add a new `TextView` to `Resources\layout\activity_main.axml`, with
   an id of `textView1`
4. Build and Deploy

Get a crash at runtime:

    Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView occurred

A `Rebuild` fixes the problem, and the app starts correctly again.

After comparing `obj` directories from before and after, I noticed
`obj\Debug\android\src\android\support\compat\R.java` was missing the
field for `R.id.textView1`!

    public static int textView1=0x7f080091;

This doesn't match the error message we are getting here at all... But
this file is updated by the `_GenerateJavaDesignerForComponent`
MSBuild target. Further research showed that this target was getting
skipped at step no. 4 above, because it was found to be up to date
according to its inputs and outputs.

To verify this was the case, I could delete
`obj\Debug\Component.R.cs.flag` which would also resolve the runtime
exception (instead of a full `Rebuild`).

First, I created a new test:
- `CustomViewAddResourceId` builds a project with
  `<android.support.design.widget.BottomNavigationView />`
- Adds a `textView1` layout, builds again
- Verifies that `obj\Debug\android\src\android\support\compat\R.java`
  contains `textView1`

To fix the problem, I did the following:
- Added `$(_AndroidResgenFlagFile)` as an input to
  `_GenerateJavaDesignerForComponent`, so it will run again when
  `_UpdateAndroidResgen` runs
- Removed
  `AndroidComponentResgenFlagFile="$(_AndroidComponentResgenFlagFile)"`
  from the call to the `<Aapt />` MSBuild task, so it will re-run
  `aapt` and generate `R.java`.

However, things are still breaking when `$(AndroidUseAapt2)` is
enabled. Removing `AndroidComponentResgenFlagFile` from the
`<Aapt2Link />` MSBuild task did not solve the problem here...
I'm not seeing exactly what generates `R.java` when
`$(AndroidUseAapt2)` is enabled? Perhaps another target that calls
`<Aapt2Compile />`?

This PR isn't quite complete until we also get this new test passing
under aapt2.
@Silic0nS0ldier
Copy link

If anyone hits this a work around is to add

<Target Name="BeforeBuild" Condition="Exists('$(ProjectDir)obj\Debug\MonoAndroid81\android\src\android\support\compat\R.java')">
  <Delete Files="$(ProjectDir)obj\Debug\MonoAndroid81\android\src\android\support\compat\R.java" />
</Target>

Or something similar (MonoAndroid81 might be different) to the project file.

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Sep 20, 2018
Fixes: xamarin#2193

I have been able to reproduce a #deletebinobj bug with the following
steps:
1. `File | New Project | Android App (Xamarin) | Tabbed App`, set
   `Minimum Android Version` to `Android 5.0 (Lollipop)`
2. Build
3. Add a new `TextView` to `Resources\layout\activity_main.axml`, with
   an id of `textView1`
4. Build and Deploy

Get a crash at runtime:

    Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView occurred

A `Rebuild` fixes the problem, and the app starts correctly again.

After comparing `obj` directories from before and after, I noticed
`obj\Debug\android\src\android\support\compat\R.java` was missing the
field for `R.id.textView1`!

    public static int textView1=0x7f080091;

This doesn't match the error message we are getting here at all... But
this file is updated by the `_GenerateJavaDesignerForComponent`
MSBuild target. Further research showed that this target was getting
skipped at step no. 4 above, because it was found to be up to date
according to its inputs and outputs.

To verify this was the case, I could delete
`obj\Debug\Component.R.cs.flag` which would also resolve the runtime
exception (instead of a full `Rebuild`).

First, I created a new test:
- `CustomViewAddResourceId` builds a project with
  `<android.support.design.widget.BottomNavigationView />`
- Adds a `textView1` layout, builds again
- Verifies that `obj\Debug\android\src\android\support\compat\R.java`
  contains `textView1`

To fix the problem, I did the following:
- Added `$(_AndroidResgenFlagFile)` as an input to
  `_GenerateJavaDesignerForComponent`, so it will run again when
  `_UpdateAndroidResgen` runs
- Removed
  `AndroidComponentResgenFlagFile="$(_AndroidComponentResgenFlagFile)"`
  from the call to the `<Aapt />` MSBuild task, so it will re-run
  `aapt` and generate `R.java`.

However, things were breaking when `$(AndroidUseAapt2)` was
enabled.

For `aapt2` support I had to:
- Remove `AndroidComponentResgenFlagFile` from the `<Aapt2Link />`
  call
- Set an extra property that the other `<Aapt2Link />` calls are
  already doing:
  `CompiledResourceFlatArchive="$(IntermediateOutputPath)\compiled.flata"`
dellis1972 pushed a commit that referenced this issue Sep 20, 2018
Fixes: #2193

I have been able to reproduce a #deletebinobj bug with the following
steps:
1. `File | New Project | Android App (Xamarin) | Tabbed App`, set
   `Minimum Android Version` to `Android 5.0 (Lollipop)`
2. Build
3. Add a new `TextView` to `Resources\layout\activity_main.axml`, with
   an id of `textView1`
4. Build and Deploy

Get a crash at runtime:

    Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView occurred

A `Rebuild` fixes the problem, and the app starts correctly again.

After comparing `obj` directories from before and after, I noticed
`obj\Debug\android\src\android\support\compat\R.java` was missing the
field for `R.id.textView1`!

    public static int textView1=0x7f080091;

This doesn't match the error message we are getting here at all... But
this file is updated by the `_GenerateJavaDesignerForComponent`
MSBuild target. Further research showed that this target was getting
skipped at step no. 4 above, because it was found to be up to date
according to its inputs and outputs.

To verify this was the case, I could delete
`obj\Debug\Component.R.cs.flag` which would also resolve the runtime
exception (instead of a full `Rebuild`).

First, I created a new test:
- `CustomViewAddResourceId` builds a project with
  `<android.support.design.widget.BottomNavigationView />`
- Adds a `textView1` layout, builds again
- Verifies that `obj\Debug\android\src\android\support\compat\R.java`
  contains `textView1`

To fix the problem, I did the following:
- Added `$(_AndroidResgenFlagFile)` as an input to
  `_GenerateJavaDesignerForComponent`, so it will run again when
  `_UpdateAndroidResgen` runs
- Removed
  `AndroidComponentResgenFlagFile="$(_AndroidComponentResgenFlagFile)"`
  from the call to the `<Aapt />` MSBuild task, so it will re-run
  `aapt` and generate `R.java`.

However, things were breaking when `$(AndroidUseAapt2)` was
enabled.

For `aapt2` support I had to:
- Remove `AndroidComponentResgenFlagFile` from the `<Aapt2Link />`
  call
- Set an extra property that the other `<Aapt2Link />` calls are
  already doing:
  `CompiledResourceFlatArchive="$(IntermediateOutputPath)\compiled.flata"`
dellis1972 pushed a commit that referenced this issue Oct 3, 2018
Fixes: #2193

I have been able to reproduce a #deletebinobj bug with the following
steps:
1. `File | New Project | Android App (Xamarin) | Tabbed App`, set
   `Minimum Android Version` to `Android 5.0 (Lollipop)`
2. Build
3. Add a new `TextView` to `Resources\layout\activity_main.axml`, with
   an id of `textView1`
4. Build and Deploy

Get a crash at runtime:

    Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView occurred

A `Rebuild` fixes the problem, and the app starts correctly again.

After comparing `obj` directories from before and after, I noticed
`obj\Debug\android\src\android\support\compat\R.java` was missing the
field for `R.id.textView1`!

    public static int textView1=0x7f080091;

This doesn't match the error message we are getting here at all... But
this file is updated by the `_GenerateJavaDesignerForComponent`
MSBuild target. Further research showed that this target was getting
skipped at step no. 4 above, because it was found to be up to date
according to its inputs and outputs.

To verify this was the case, I could delete
`obj\Debug\Component.R.cs.flag` which would also resolve the runtime
exception (instead of a full `Rebuild`).

First, I created a new test:
- `CustomViewAddResourceId` builds a project with
  `<android.support.design.widget.BottomNavigationView />`
- Adds a `textView1` layout, builds again
- Verifies that `obj\Debug\android\src\android\support\compat\R.java`
  contains `textView1`

To fix the problem, I did the following:
- Added `$(_AndroidResgenFlagFile)` as an input to
  `_GenerateJavaDesignerForComponent`, so it will run again when
  `_UpdateAndroidResgen` runs
- Removed
  `AndroidComponentResgenFlagFile="$(_AndroidComponentResgenFlagFile)"`
  from the call to the `<Aapt />` MSBuild task, so it will re-run
  `aapt` and generate `R.java`.

However, things were breaking when `$(AndroidUseAapt2)` was
enabled.

For `aapt2` support I had to:
- Remove `AndroidComponentResgenFlagFile` from the `<Aapt2Link />`
  call
- Set an extra property that the other `<Aapt2Link />` calls are
  already doing:
  `CompiledResourceFlatArchive="$(IntermediateOutputPath)\compiled.flata"`
@Silic0nS0ldier
Copy link

Fix is out in Visual Studio 15.9 Preview 4, and I can confirm it works.

@xamarin xamarin locked as resolved and limited conversation to collaborators Jun 7, 2022
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. bug Component does not function as intended. vs-sync For internal use only; creates a VSTS "mirror" issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants