我的应用程序中有两种产品味道,即flavorOne(src/flavorOne/java)和flavorTwo(src/flavorTwo/java)。 Main(src/main/java)是一个目录,其中两种风味都使用类。我想从src/main/java/ActivityA中的活动开始src/flavorTwo/java/ActivityB.java的活动。在运行flavorTwo时它可以工作,但是当我切换到flavorOne时,它显示import com.packagename.ActivityB错误。
什么是这个问题的最佳解决方案?
+ App // module
|- src
|- main// shared srcDir
|- java
|- SharedActivity
+ flavorOne
|- java
|- FlavorOneActivity
+ flavorTwo
|- java
|- FlavorTwoActivity
这里是SharedActivity.java,位于src/main/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下的Activity,路径为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下的Activity,位于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));
}
});
}
}
}
什么是这个问题的最佳解决方案?
import flavorOne.java;
。 - JBALI