【问题标题】:Java:Android exclude packages with dependencies in final compiled fileJava:Android 在最终编译文件中排除具有依赖关系的包
【发布时间】: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


    【解决方案1】:

    你不应该按照你描述的方式去做。

    您应该创建额外的抽象级别来访问特定于客户的数据/逻辑。并将客户特定的逻辑/数据移动到单独的库/项目中。

    要处理分离的客户项目,您的抽象级别应该使用动态类加载。

    编辑。如果您无法重构您的项目,您可以为每个客户创建 Proguard 配置文件以删除无法访问的代码。

    【讨论】:

    • @titgar,提示您的方向是正确的,您无需更改源代码即可构建不同的包,而只需更改构建命令
    • pbespechnyi,我知道你认为我必须找到在我的情况下使用反射的方法?
    • @titgar,是的,动态加载类并创建您应该反思的实例。
    • @pbespechnyi,是的...我在其他项目中使用了反射,但在此,客户的方法与彼此无关。例如,在“任务”按钮中,一位客户读取文件,另一位进行数学计算......
    • @titgar,这就是为什么您需要额外的抽象级别。在core 项目中创建接口(以隐藏客户实现细节),并在另一个项目/lib 中为每个客户实现它们。然后,使用统一的接口来处理客户特定的逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多