Android风味 – 在R文件中找不到符号变量

我有两种口味 – paidapp和freeapp.
唯一的区别是,paidapp在MainActivity上还有一个按钮,比如说“付费按钮”.
paidapp有自己的布局,按钮 android:id =“@ id / paidbutton”,freeapp的布局没有这个按钮.

代码我用这个:

if (BuildConfig.FLAVOR.equals("paidapp")) {
            View paidbutton = findViewById(R.id.paidbutton);
            paidbutton.setonClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //...
                }
            });
}

我有一个错误找不到findViewById(R.id.paidbutton)中的符号变量;

如何在不克隆MainActivity.java的情况下解决这个问题?

EDIT1 – 添加更多代码示例:

摇篮:

productFlavors {
    paidapp {
    }
    freeapp {
    }
}

/app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/goNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go next" />

</LinearLayout>

/app/src/paidapp/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/goNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go next" />

    <Button
        android:id="@+id/paidbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="See more" />


</LinearLayout>

/app/src/main/java/company/com/myapplication/MainActivity.java

package company.com.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View goNext = findViewById(R.id.goNext);
        goNext.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"goNext click",Toast.LENGTH_SHORT).show();
            }
        });

        if (BuildConfig.FLAVOR.equals("paidapp")) {
            View paidbutton = findViewById(R.id.paidbutton);
            paidbutton.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,"paidapp click",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}

EDIT2 – 我找到了一些使用反射的解决方法,但仍在寻找正常的解决方案:

而不是View paidbutton = findViewById(R.id.paidbutton);我用过View paidbutton = findViewById(getIdFromrId(“paidbutton”));

其中getIdFromrId是:

private int getIdFromrId(String idName) {
    int id = 0;
    try {
        Class c = R.id.class;
        Field field = c.getField(idName);
        id = field.getInt(null);
    } catch (Exception e) {
        // do nothing
    }
    return id;
}

解决方法

如果你在freeapp产品风格中根本没有paybutton布局元素,你可以在freeapp product flavor文件夹中的一个资源xml文件中声明相应的id项.例如,创建包含以下内容文件src / freeapp / res / values / ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="paidbutton" type="id"/>
</resources>

在执行此操作之后,编译公共代码没有任何问题,因为将为两种产品风格定义符号R.id.paidbutton. findViewById()调用将返回paidapp构建中的真实布局元素,并将在freeapp构建中返回null.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...