当前位置:  开发笔记 > Android > 正文

如何从主目录访问产品风味类?

如何解决《如何从主目录访问产品风味类?》经验,为你挑选了1个好方法。

我的应用程序中有两种产品口味,即
flavorOne(src / flavorOne / java)flavorTwo(src / flavorTwo / java)Main(src / main / java)是目录形式,两种风格都使用这些类。我想开始活动的src / flavorTwo / JAVA / ActivityB的.java从活动中存在的src / main / JAVA / ActivityA。在运行flavourTwo时有效,但当我切换flavourOne时,它显示导入com.packagename.ActivityB错误。

+ App // module
    |- src
       |- main// shared srcDir
          |- java
           |- SharedActivity
       + flavorOne
          |- java
           |- FlavorOneActivity
       + flavorTwo
          |- java
           |- FlavorTwoActivity

这是dir src / main / java / SharedActivity.java中的SharedActivity.java

package com.example.buildvariants;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
/**********************  works if it is in flavorOne otherwise it shows error on this package import ***********/
import com.example.buildvariants.flavorOne.LoginActivity;

public class SharedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        //if flavor is flavorTwo hide the fab
        //else flavor is flavorOne show fab and launch activity under flavorOne/java/LoginActivity.java
        if (BuildConfig.FLAVOR.equalsIgnoreCase("flavorTwo")) {
            fab.setVisibility(View.GONE);
        } else {
            fab.setVisibility(View.VISIBLE);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                    startActivity(new Intent(SharedActivity.this, LoginActivity.class));

                }
            });
        }
    }
}

flavourOne src / flavorOne / FlavorOneMainActivity.java 下的活动

package com.example.buildvariants.flavorOne;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.buildvariants.R;
import com.example.buildvariants.SharedActivity;

public class FlavorOneMainActivity extends AppCompatActivity {

    private static final String TAG =FlavorOneMainActivity.class.getSimpleName() ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flavor_one_activity_main);
        Intent intent = new Intent(FlavorOneMainActivity.this, SharedActivity.class);
        startActivity(intent);
    }
}

flavourTwo src / flavorOne / FlavorTwoMainActivity.java 下的活动

package com.example.buildvariants.flavorTwo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.buildvariants.R;
import com.example.buildvariants.SharedActivity;

public class FlavorTwoMainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flavor_two_activity_main);
        startActivity(new Intent(FlavorTwoMainActivity.this, SharedActivity.class));
    }
}

当我更改构建变体flavorTwo时,如下所示在SharedActivity(src / main / java /)包导入时显示错误

package com.example.buildvariants;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
/********error  on package import***********/
import com.example.buildvariants.flavorOne.LoginActivity;

public class SharedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        //if flavor is flavorTwo hide the fab
        //else flavor is flavorOne show fab and launch activity under flavorOne/java/LoginActivity.java
        if (BuildConfig.FLAVOR.equalsIgnoreCase("flavorTwo")) {
            fab.setVisibility(View.GONE);
        } else {
            fab.setVisibility(View.VISIBLE);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                    startActivity(new Intent(SharedActivity.this, LoginActivity.class));

                }
            });
        }
    }
}

解决此问题的最佳解决方案是什么?



1> JBirdVegas..:

之所以失败,是因为在构建您的应用程序时,一次仅存在一种产品类型的代码。因此,您真正想要做的是在两个产品类型中使用一个类名称。

假设我们有一个要根据产品口味替换的类,可以称之为 ReplacableActivity.java

为了使替换正常工作,两种产品口味都需要具有该类,而main的源集将没有该类

例:

src/main/com/blah/myApp/ReplacableActivity #<- should not exist
# exists and is the implementation of ReplacableActivity for `flavorOne`
src/flavorOne/com/blah/myApp/ReplacableActivity.java
# exists and is the implementation of ReplaceableActivity.java for `flavorTwo`
src/flavorTwo/com/blah/myApp/ReplacableActivity.java

现在,对于所有产品口味,您的建筑物都已ReplacableActivity存在,并且可以从main源集中进行引用。在构建时,只有ReplaceableActivity特定口味的会与应用程序打包在一起。现在,您的导入将按预期 import com.blah.myApp.ReplaceableActivity;main源集中进行。

编辑:

如果您唯一关心的是隐藏或显示单个元素,那么以上内容就太过分了。从中获取它会容易得多。BuildConfigField

android {
    productFlavors {
        flavorOne {
            buildConfigField "boolean", "flavorShowsFab", 'false'
        }
        flavorTwo {
            buildConfigField 'boolean', 'flavorShowsFab', 'true'
        }
    }

然后在您的Java代码中执行

findViewById(R.id.myHidableFab).setVisibility(BuildConfig.flavorShowsFab ? View.VISIBLE : View.GONE));

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有