JNI简单入门

做Android开发,最終还是免不了要用NDK的,我这才真的需要学习下JNI。简单的说,JNI就是让Java代码与native代码(比如C和C++)交互的一种机制。

参照The JNI Programmer's Guide and Specification,很成功的运行了一个小DEMO

首先编辑一个Java文件Prompt.java

public class Prompt {
  private native String getLine(String prompt);

  public static void main(String[] args) {
    Prompt p = new Prompt();
    String input = p.getLine("Type a line: ");
    System.out.println("User typed: " + input);
  }

  static {
    System.loadLibrary("Prompt");
  }
}

其中native用来声明一个方法,而方法的实现则交给C代码。static代码块用来加载接下来即将生成的libPrompt.so

READ MORE>>

Android中aidl如何import文件

如果你做Android开发,那就应该会知道aidl工具的,但是会使用aidl命令行工具的人有吗有吗有吗,如果有的话你们为神马不出来写写怎么用!!!!

用aidl命令行的如果不出意外肯定会遇见类似这样的错误

$ aidl IRemoteServiceCallback.aidl 
IRemoteServiceCallback.aidl:19: couldn't find import for class android.location.Location

遇到错误,自然要先看下aidl的帮助是怎么写的

$ aidl
usage: aidl OPTIONS INPUT [OUTPUT]
       aidl --preprocess OUTPUT INPUT...

OPTIONS:
   -I<DIR>    search path for import statements.
   -d<FILE>   generate dependency file.
   -p<FILE>   file created by --preprocess to import.
   -o<FOLDER> base output folder for generated files.
   -b         fail when trying to compile a parcelable.

看到如此简洁不明了的介绍你有何感想?

READ MORE>>

lucene 3的中文分词mmseg4j

我的日啊,昨天刚完成了lucene 2.9到3.0的迁移,今天就有lucene 3.0.1了!!!

很久没写点有代码的东西了,实在是没时间。最近,很意外的,我竟然又写起了Java。

我的主要任务是把网站(不是我这个blog)的全文检索由lucene 2.9升级到3.x版本,同时更新下mmseg4j。

lucene 3.x版本采用了全新的API,作为过渡的2.9中那些deprecated方法在3.0中已经彻底废弃了。不过我也没有太多东西要改,主要是修正了TokenStreams的相关代码,似乎TokenStream也是3.0中最大的革新。

A new TokenStream API has been introduced with Lucene 2.9. This API has moved from being Token-based to Attribute-based. While Token still exists in 2.9 as a convenience class, the preferred way to store the information of a Token is to use AttributeImpls.

lucene的中文分词是使用的mmseg4j 1.8.2,这个版本也是针对lucene 2.x的,因此首先对mmseg4j下手。

与lucene相关的代码全部位于com.chenlb.mmseg4j.analysis;中,可以看到要做修正的地方并不多,主要还是把MMSegTokenizer中的next()换作boolean incrementToken()

READ MORE>>