17

Why import org.apache.commons.lang.StringUtils cannot be imported in android by default.

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;
        }
}
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124

5 Answers5

34

You don't say whether you are using Eclipse or Android Studio. In Android Studio, you would add,

import org.apache.commons.lang3.StringUtils;

to your source code file. In build.gradle, you need to change your dependency from something like,

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.

VectorVortec
  • 667
  • 7
  • 10
  • Don't use this lib. By using this lib, While uploading apk to Google play store, It will show 0 supported devices. Instead use compile 'commons-lang:commons-lang:2.6'. it works. – vikoo Aug 07 '17 at 21:32
13

Android offers a subset of that functionality in android.text.TextUtils.

Depending on what you need from StringUtils, that might be an option. E.g., it has join, split.

Alex
  • 5,909
  • 2
  • 35
  • 25
6

Apache Commons lang is a separate library. You can find it here.

wds
  • 31,873
  • 11
  • 59
  • 84
  • I have it downloaded already. The downloaded folder does not have any jar file that i can include in my android project. What to do now? – Muhammad Maqsoodur Rehman May 11 '10 at 07:51
  • The jar files are inside this zip file: http://apache.mogo.be/commons/lang/binaries/commons-lang-2.5-bin.zip – wds May 11 '10 at 08:00
  • Ok! Downloaded and ran the code but i'm getting null pointer exception when i call the encode method in my main activity? – Muhammad Maqsoodur Rehman May 11 '10 at 09:36
  • @Maxood I think that would be an unrelated problem to this question. Without looking at your main method I can't say what might be the issue. Are you sure you fill in the `id` value? Perhaps ask another question about this specific problem if you can't figure it out. – wds May 11 '10 at 13:20
  • Here is my main activity: import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); long var = 70; Long multiplier=Long.parseLong("1zzzz",36); EncodeIdUtil edUtil = new EncodeIdUtil(); //Null pointer exception edUtil.encode(var); } } – Muhammad Maqsoodur Rehman May 12 '10 at 09:27
  • You are possibly getting the null pointer exception because you don't package it with the application, to do this go configure build path then order and export, then click the checkbox next to the jar file you want – FabianCook May 18 '12 at 09:14
4

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.

  1. File -> Project Structure -> Modules -> app -> Dependencies Tab
  2. Click '+' in the bottom left corner and select "Library dependency"
  3. In the search field type: "org.apache.commons:commons-lang3" and click Search
  4. Select "org.apache.commons:commons-lang3:3.7"

enter image description here

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
3

just add following dependency in module level gradle file(build.gradle)

implementation  'org.apache.commons:commons-lang3:3.7'
Prateek Gupta
  • 148
  • 11