简体   繁体   English

将Android应用程序转换为android库会给出错误:需要常量表达式

[英]Converting Android application to android library gives Error: constant expression required

I have an Android application which I wish to convert to a library to use in another application. 我有一个Android应用程序,希望将其转换为库以在另一个应用程序中使用。 I have changed com.android.application to com.android.library in the apply plugin section in gradle file. 我已经改变com.android.applicationcom.android.library在gradle这个文件中的应用插件部分。 Removed application id. 删除了应用程序ID。 Removed the Launcher intent in the Launcher application of library. 在库的Launcher应用程序中删除了Launcher意图。 But when I build this project I get several errors which require constant expression like in R.id.imageButton . 但是,当我构建这个项目时,我遇到了一些错误,需要像R.id.imageButton那样的常量表达式。

Some other posts indicate that ids are used inside switch cases which require 'constants'. 其他一些帖子指出,在需要“常量”的开关盒中使用了ID。 So a workaround may be replacing switch cases with if-else constructs. 因此,一种解决方法可能是用if-else构造替换开关案例。 But is the only way? 但是唯一的方法吗? Also, if yes, why are ids not treated as constants now that I am switching my project to a library instead of application? 另外,如果是的话,既然我将项目切换到库而不是应用程序,为什么不将id视为常量?

you need covert switch into if/else code block。 您需要秘密switchif/else代码块。

reference: Non-constant Fields in Case Labels 参考: 案例标签中的非常数字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM