为什么在 Android 中无法识别 StringUtils Apache 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2808733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why StringUtils Apache class is not recognized in Android?
提问by Muhammad Maqsoodur Rehman
Why import org.apache.commons.lang.StringUtilscannot be imported in android by default.
为什么import org.apache.commons.lang.StringUtils不能在android中默认导入。
Do i have to include an external library? Then where can i find that library on the web?
我必须包含一个外部库吗?那么我在哪里可以在网上找到那个图书馆?
package com.myapps.urlencoding;
import android.app.Activity;
import org.apache.commons.lang.StringUtils;
public class EncodeIdUtil extends Activity {
/** Called when the activity is first created. */
private static Long multiplier=Long.parseLong("1zzzz",36);
/**
* Encodes the id.
* @param id the id to encode
* @return encoded string
*/
public static String encode(Long id) {
return StringUtils.reverse(Long.toString((id*multiplier), 35));
}
/**
* Decodes the encoded id.
* @param encodedId the encodedId to decode
* @return the Id
* @throws IllegalArgumentException if encodedId is not a validly encoded id.
*/
public static Long decode(String encodedId)
throws IllegalArgumentException {
long product;
try {
product = Long.parseLong(StringUtils.reverse(encodedId), 35);
} catch (Exception e) {
throw new IllegalArgumentException();
}
if ( 0 != product % multiplier || product < 0) {
throw new IllegalArgumentException();
}
return product/multiplier;
}
}
采纳答案by wds
回答by VectorVortec
You don't say whether you are using Eclipse or Android Studio. In Android Studio, you would add,
您没有说明您使用的是 Eclipse 还是 Android Studio。在 Android Studio 中,您将添加,
import org.apache.commons.lang3.StringUtils;
to your source code file. In build.gradle, you need to change your dependency from something like,
到您的源代码文件。在 build.gradle 中,您需要更改依赖项,例如,
dependencies {
compile 'com.android.support:support-v4:+'
}
to
到
dependencies {
compile 'com.android.support:support-v4:+'
compile 'org.apache.commons:commons-lang3:3.0'
}
In other words, you would add to the dependency.
换句话说,您将添加到依赖项中。
回答by Alex
Android offers a subset of that functionality in android.text.TextUtils.
Android 在android.text.TextUtils 中提供了该功能的一个子集。
Depending on what you need from StringUtils, that might be an option. E.g., it has join, split.
根据您对 StringUtils 的需求,这可能是一个选项。例如,它具有连接、拆分。
回答by Aniket Thakur
If you are using Android Studio try to use workflow provided to add dependencies rather than manually adding the downloaded library or manually changing gradle files.
如果您使用的是 Android Studio,请尝试使用提供的工作流来添加依赖项,而不是手动添加下载的库或手动更改 gradle 文件。
- File -> Project Structure -> Modules -> app -> Dependencies Tab
- Click '+' in the bottom left corner and select "Library dependency"
- In the search field type: "org.apache.commons:commons-lang3" and click Search
- Select "org.apache.commons:commons-lang3:3.7"
- 文件 -> 项目结构 -> 模块 -> 应用程序 -> 依赖项选项卡
- 单击左下角的“+”并选择“库依赖项”
- 在搜索字段中输入:“org.apache.commons:commons-lang3”并点击搜索
- 选择“org.apache.commons:commons-lang3:3.7”
回答by Prateek Gupta
just add following dependency in module level gradle file(build.gradle)
只需在模块级gradle文件(build.gradle)中添加以下依赖项
implementation 'org.apache.commons:commons-lang3:3.7'