我的应用程序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下的Activity
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)); } }); } }}
什么是这个问题的最佳解决方案?
解决方法:
它失败了,因为当您构建应用程序时,一次只存在一个产品风格的代码.所以你真正想做的是在两种产品口味中使用一个类名.
想象一下,我们想要根据产品风格替换一个类,我们称之为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都存在,并且可以从主要源集引用.在构建时,只有该特定flavor的ReplaceableActivity与app一起打包.现在您的导入将按预期工作
import com.blah.myApp.ReplaceableActivity;来自主要的源集.
编辑:
如果您唯一关心的是隐藏或显示单个元素,那么上述内容就是矫枉过正.从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));
总结 以上是内存溢出为你收集整理的android – 如何从主目录访问产品类的类?全部内容,希望文章能够帮你解决android – 如何从主目录访问产品类的类?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)