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

问题描述 投票:4回答:2

我的应用程序中有两种产品风味flavorOne(src / flavorOne / java)flavorTwo(src / flavorTwo / java)Main(src / main / java)是目录形式,两种风格都使用这些类。我想从src / main / java / ActivityA中存在的Activity开始活动src / flavorTwo / java / ActivityB。java。在运行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));

                }
            });
        }
    }
}

flavorOne]下的活动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);
    }
}

flavorTwo]下的活动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));

                }
            });
        }
    }
}

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

我的应用程序中有两种产品口味,即flavorOne(src / flavorOne / java)和flavorTwo(src / flavorTwo / java)。 Main(src / main / java)是目录形式,两种风格都使用这些类。我...

android build android-gradle-plugin build.gradle android-productflavors
2个回答
6
投票

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

想象一下,我们有一个要根据产品口味替换的类,我们称它为ReplacableActivity.java


0
投票

不是为每种口味创建不同的代码,而是可以使用继承概念维护相同的代码,并且gradle

© www.soinside.com 2019 - 2024. All rights reserved.