mardi 25 février 2020

How can I use "R.id object = new R.id();" with Build-Tools 3.6.0?

I have to use different layouts for different build flavors where in one flavor there are ui elements that don't exist in the other flavor.

Earlier I just set elements invisible/gone but now I have a NestedScrollView with a child fragment that contains a RecyclerView. The child needs a button in one flavor that the other flavor must not have. If I just set it invisible or gone the layout doesn't work properly (doesn't scroll and height is much too low).

I copied the layout xml to the path of the flavor and removed the button and used reflection in fragment code to know if the button exists and implement it's code.

    Class<R.id> c = R.id.class;
    R.id object = new R.id();
    Field[] fields = c.getDeclaredFields();

    for (Field field : fields)
    {
        field.setAccessible(true);
        try
        {
            if (field.getName().equals("TheButton"))
            {
                Button tBtn = tView.findViewById((Integer) field.get(object));
                if (tBtn != null)
                    tBtn.setOnClickListener(...);
            }
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }

After updating to Gradle build tools from 3.5.3 to 3.6.0 I get

error: id() has private access in id R.id object = new R.id();

for the line

R.id object = new R.id();

when trying to build. At the moment I just keep uinge the old build tools but that's no permanent solution, I think.

Can I do something else? I would like to get rid of reflection but if it's really the only way to go it should work with latest build tools...





Aucun commentaire:

Enregistrer un commentaire