Complexity using java library in Android Studio, works fine in IDEA

new android programmer. I've been working on an Android app for the past couple of days and I ran into a little road block.

I am using a library called Kuromoji ( http://www.atilika.org/ ) to parse Japanese text blocks into single words. But it doesn't seem to work correctly when I use it in Android Studio.

It builds and works fine in Intellij IDEA with pure Java as well as a Java project with Gradle, but it starts working when I switch to Android Studio.

I just run the empty projects I just created for both IDEA and Android Studio, I can't seem to figure out the conflict factor.

Thanks for reading, any help would be greatly appreciated.

import org.atilika.kuromoji.Tokenizer;
import org.atilika.kuromoji.Token;

public class kuromoji_test {

public static void main(String[] args){
    Tokenizer tokenizer = Tokenizer.builder().build();
    for (Token token : tokenizer.tokenize("寿司が食べたい。")) {
        System.out.println(token.getSurfaceForm() + "\t" + token.getAllFeatures());
    }
}
}

      

Running in IDEA with or without Gradle, this code generates the correct parsing words along with part of speech, etc.

寿司 名詞, 一般 ,,, 寿司, ス シ, ス シ '

が 助詞, 格 助詞, 一般 ,, *, が, ガ, ガ

食 べ 動詞, 自立 ,, 一段, 連用 形, 食 べ る, タ ベ, タ ベ

た い 助動詞 ,, *, 特殊 タ イ, 基本 形, た い, タ イ, タ イ

... 記号, 句 点 ,,,.,.,.

package com.example.towson.kuromoji_testing_with_android_studio;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import org.atilika.kuromoji.Token;
import org.atilika.kuromoji.Tokenizer;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    Tokenizer tokenizer = Tokenizer.builder().build();
    for (Token token : tokenizer.tokenize("寿司が食べたい。")) {
        System.out.println(token.getSurfaceForm() + "\t" + token.getAllFeatures());
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

      

Everything builds fine, but now Kuromoji just returns my input with a character. Another function that returns information about dictionaries doesn't seem to work at all.

Kuromoji is in my libs folder and I added the jar as a library.

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: 寿

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: 司

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: が

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: 食

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: べ

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: た

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out: い

05-10 01: 06: 52.813 5758-5758 / com.example.towson.kuromoji_testing_with_android_studio I / System.out :.

my build.Gradle from Android Studio:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.example.towson.kuromoji_testing_with_android_studio"
    minSdkVersion 19
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),    'proguard-rules.pro'
    }
}
}

repositories {
mavenCentral()
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}

      

kuromoji is compiled in libs folder

my build.Gradle from Intellij

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}

      

EDIT: The library continues to fail when using IDEA for an Android app. The problem seems to be related to Android, not IDE.

Checking in debug mode, class objects from my library file are building correctly. Most string methods simply return "". Not sure what's going on here.

Edit 2: kuromoji running correctly

Here is a photo of my token and tokenizer objects. This is what they look like when they work right.

kuromoji running incorrectly

Here the photo of my token objects and tokenizer object is not working correctly, you can see that the "surface_form" tag contains only one character instead of two.

Does anyone know how I can debug this?

+3


source to share


1 answer


Okay, just got it. I am using a package that makes calls to java functions not included in the Android version of Java.

This is why I had problems when my project was set up as an Android app.

Copied this from the lint report:

* Used a different set of libraries since I posted this question. Kuromoji is a Japanese morphology analyzer and has since donated its code to the Apache Software Foundation.

Use of the org.apache.lucene libraries in this report.

InvalidPackage: a package not included in Android

../../libs/lucene-analyers-common-5.1.0.jar: invalid package reference in library; not included in Android: java.nio.file. Link to org.apache.lucene.analysis.hunspell.Dictionary website.



../../libs/lucene-core-5.1.0.jar: invalid package reference in library; not included in Android: java.lang.invoke. Link from org.apache.lucene.util.AttributeFactory.

Priority: 6/10

Category: Correctness

Severity: error

Explanation: The package is not included in Android. This check checks libraries looking for API calls that are not included in Android.

When you create Android projects, the classpath is set up so that you can only access the classes in the API packages that are included with Android. However, if you add other projects to your libs / folder, there is no guarantee that these .jar files were generated with a specific Android class, and in particular, they may access unsupported APIs such as java.applet.

This check checks library jars and looks for references to API packages that are not included in Android and tags them. It is only an error if your code calls one of the library classes that ends with a reference to an unsupported package.

0


source







All Articles