Android JNI obtains native objects

Source: Internet
Author: User

Android JNI has many requirements for using native objects. Its implementation is very simple. The following uses paint as an example to record it.

Paint. Java (frameworks \ base \ graphics \ Java \ Android \ graphics)

public class Paint {    public int mNativePaint;    public Paint(int flags) {        mNativePaint = native_init();        ... ...    }    private static native int native_init();    ... ...};

Paint. cpp (frameworks \ base \ core \ JNI \ Android \ graphics)

class SkPaintGlue {    static SkPaint* init(JNIEnv* env, jobject clazz) {        SkPaint* obj = new SkPaint();        defaultSettingsForAndroid(obj);        return obj;    }    ... ...};static JNINativeMethod methods[] = {    {"native_init","()I", (void*) SkPaintGlue::init},    ... ...};

Mnativepaint in the Java layer is int, corresponding to skpaint in the native Layer *.

In this way, when you need to operate native layer objects on the Java layer, as long as the mnativepaint int is passed, native will know that it is the skpaint pointer.

It is very simple, but I think it is very powerful and clever.

Think about how to represent the C ++ object in the native layer on the Java layer, and think about pointers and integers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.