我的应用中有两种产品风味i-e
flavorOne(src / flavorOne / java)和 flavorTwo(src / flavorTwo / java)。 Main(src / main / java)是目录表单,其中两个flavor都在使用这些类。我想从 src / main / java / ActivityA 中的Activity开始活动 src / flavorTwo / java / ActivityB .java。在运行flavorTwo时它可以工作但是当我切换flavorOne时它会显示导入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));
}
});
}
}
}
这个问题的最佳解决方案是什么?
答案 0 :(得分:5)
它失败了,因为当您构建应用时,一次只存在一种产品风味的代码。所以你真正想做的是在两种产品口味中使用一个类名。
想象一下,我们想要根据产品风格替换一个类,让我们称之为ReplacableActivity.java
为了替换工作,两个产品口味都需要有这个类,主要的源集不会有类
示例:
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));
答案 1 :(得分:1)
您可以使用继承概念并在 gradle 和清单文件中进行很少的更改,而不是为每种口味创建不同的代码,我们可以通过三个步骤来实现这一目标
1)合并应用程序级别build.gradle
文件中每种口味的来源集
sourceSets {
flavorOne {
java.srcDirs = ['src/main/java','src/flavorOne/java']
}
flavorTwo {
java.srcDirs = ['src/main/java','src/flavorTwo/java']
}
}
2)主要技巧发生在这里,为每种口味创建清单,并为其定义启动器活动,但不在主要口味中
在主清单文件中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
//no launcher defined here
</application>
</manifest>
有一个主要文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<application>
<activity
android:name="{Your launcher activity}">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
有两个清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<application>
<activity
android:name="{Your launcher activity}">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这样在构建gradle时合并清单文件时不会出错
3)因此,在 main java src中,创建BaseSharedActivity.java
并编写您的常规实现,在每种形式中 create SharedActivity.java
并从定义的启动器活动中调用它们每种口味。
在每种口味的SharedActivity.java
中:
public class SharedActivity extends BaseSharedActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//You can override whatever method you want from BaseSharedActivity here
}
这样,您就不必为 SharedActivity 添加相同的代码,并且可以为不同风格的同一屏幕提供最大的自定义,您也可以对启动器活动使用相同的逻辑