【发布时间】:2015-04-03 21:10:29
【问题描述】:
我在 Android/Eclipse 中遇到了这个问题。
我在 Android 项目中工作,这个项目必须为不同的客户以不同的方式进行配置,这种配置在他们之间并不相似(非常不同的方法)。
90% 的代码是共享的,但每个客户都有自己的要求。
每个客户都有一个包含不同和多个类的包。
我只想在最终编译的文件中包含所选客户的包。
但我的共享代码中有条件对象创建,我必须加入所有包进行编译。
或多或少是这样(示例)
//import every customer packages
import com.project.customer_1
import com.project.customer_2
import com.project.customer_3
…..
import com.project.customer_98
import com.project.customer_99
……..
final static int customer=2; //Define the customer for this compilation
………
if (customer==1) { //unreachable code
customerClass1 customer1=new customerClass1();
customer1.method1_1;
customer1.method1_2;
customer1.method1_3;
}
else if (customer==2) {
customerClass2 customer2=new customerClass2();
customer2.method2_1;
customer2.method2_2;
customer2.method2_3;
}
…….//unreachable code
else if (customer==98) {//unreachable code
customerClass98 customer98=new customerClass98();
customer98.method98_1;
customer98.method98_2;
customer98.method98_3;
}
else if (customer==99{//unreachable code
customerClass99 customer99=new customerClass99();
customer99.method99_1;
customer99.method99_2;
customer99.method99_3;
}
............
当customer=X时是明确的,其他选择是无法访问的代码。我想排除其他客户包/类,而不是在编译文件中选择客户。但代码中存在依赖
注意:对我来说不是有效的注释代码,也不是反射。
【问题讨论】:
标签: java android eclipse conditional-compilation