framwork新加模組

來源:互聯網
上載者:User

標籤:framwork新加模組

    

主要是基於Android系統現在的Framework模組上再添加一個獨立的模組,目前只是很簡單的framework層調用native層(後期根據學習的進度,會將hal層補上,還有aidl, stub, 非同步)。基本思路為:

 

為應用添加framework介面,即SDK APIframework調用native代碼,即jni部分jni部分實現最終功能 最終功能其實就是很簡單的log列印,目的只要是重新溫習一下framework開發的流程。之前都是在Android現有的機制上添加API,這次嘗試添加一個獨立的模組,只是個人的一個想法,不清楚Android添加一個新的獨立模組的標準做法是怎樣的,就當作學習吧。
  1. 為應用添加framework介面,即SDK API

     

    在framework/base目錄建立pmps目錄,在pmps目錄建立\java\android\pmps\PmpsManager.java 檔案,輸入代碼:

     

    package android.pmps;

    import android.util.Log;

     

    public class PmpsManager

    {

    private static final String LOG_TAG = "PmpsManager";

     

    static {

    System.loadLibrary("pmps_jni");                

    }

     

    public PmpsManager() {

    Log.i(LOG_TAG, "PmpsManager constructor().");

    }


    public native static void sayHello();

    }

     

    PmpsManager 為應用程式層提供相關API,如 sayHello(),這裡是直接調用了JNI的native方法。值得注意的是PmpsManager載入了中的System.loadLibrary("pmps_jni")載入了文章中第3步將會產生的libpmps_jni.so檔案。

     

    還有一個需要注意的細節,我們可以先看一下framework/base目錄下的Android.mk檔案,其中有一段:

    # FRAMEWORKS_BASE_SUBDIRS comes from build/core/pathmap.mk

    LOCAL_SRC_FILES := $(call find-other-java-files,$(FRAMEWORKS_BASE_SUBDIRS))

    我們再看看build/core/pathmap.mk,其中有一段

     

    FRAMEWORKS_BASE_SUBDIRS := \

    $(addsuffix /java, \

        core \

        graphics \

        location \

        media \

        opengl \

        sax \

        telephony \

        wifi \

        vpn \

        keystore \

        voip \

     )

     

    也就是說我們要在build/core/pathmap.mk檔案中添加pmps目錄,添加後為:

    FRAMEWORKS_BASE_SUBDIRS := \

    $(addsuffix /java, \

        core \

        graphics \

        location \

        media \

        opengl \

        sax \

        telephony \

        wifi \

        vpn \

        keystore \

        voip \

        pmps \

     )

     

    然後我們就可以通過 mmm 命令編譯這個新添加的模組了。

    mmm framework/base 編譯,組建檔案為out/target/product/generic/system/framework/framework.jar

    代碼編譯通過後,就可以做接下來的事情了。


    2.framework調用native代碼,即jni部分

    上一步中的 publicnative static void sayHello() 就可以由應用程式層調用了,當然我們可以在framework裡面再封裝一下,以實現更加複雜的功能,例如:

     

    //api

    public int sayHelloFirst()

    {

    //省略的代碼...

    return sayHello();

    }

     

    //declaration

    public native static void sayHello();


     

    3.jni部分實現功能


    這一步通過編寫native代碼實現framework中的調用的功能,可以實現具體的功能或者調用hal層的相關驅動,完成硬體操作功能。

    當前只是簡單地列印一下資訊。

     

    值得注意的要實現JNI_OnLoad函數,如果有需要的話,也可以重寫JNI_OnUnLoad函數。

     

    /*//device/libs/android_runtime/android_media_MediaPlayer.cpp

    **

    ** Copyright 2007,The Android Open Source Project

    **

    ** Licensed under theApache License, Version 2.0 (the "License");

    ** you may not usethis file except in compliance with the License.

    ** You may obtain acopy of the License at

    **

    **     http://www.apache.org/licenses/LICENSE-2.0

    **

    ** Unless required byapplicable law or agreed to in writing, software

    ** distributed underthe License is distributed on an "AS IS" BASIS,

    ** WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.

    ** See the Licensefor the specific language governing permissions and

    ** limitations underthe License.

    */

     

    #define LOG_NDEBUG 0

    #define LOG_TAG"PmpsManager-JNI"

    #include"utils/Log.h"

     

    #include<stdio.h>

    #include<assert.h>

    #include<limits.h>

    #include<unistd.h>

    #include<fcntl.h>

    #include"jni.h"

    #include"JNIHelp.h"

    #include"android_runtime/AndroidRuntime.h"

    //----------------------------------------------------------------------------

     

    using namespaceandroid;

     

    static voidandroid_pmps_PmpsManager_sayHello(JNIEnv *env, jobject thiz)

    {

    LOGV("sayHello()");

    }

     

    static constJNINativeMethod method_table[] = { 

    {"sayHello","()V", (void*)android_pmps_PmpsManager_sayHello}, 

    };        

     

    // This function onlyregisters the native methods

    intregister_android_pmps_PmpsManager(JNIEnv *env) { 

    inti;

     

    LOGI("JNI_OnLoad:register_android_pmps_PmpsPlayer");

     

    returnAndroidRuntime::registerNativeMethods(env,

    "android/pmps/PmpsManager",method_table, NELEM(method_table)); 

    } 

     

     

    jintJNI_OnLoad(JavaVM* vm, void* reserved)

    {

        JNIEnv* env = NULL;

        jint result = -1;

     

        if (vm->GetEnv((void**) &env,JNI_VERSION_1_4) != JNI_OK) {

            LOGE("ERROR: GetEnvfailed\n");

            goto bail;

        }

        assert(env != NULL);

     

        if (register_android_pmps_PmpsManager(env)< 0) {

            LOGE("ERROR: PmpsManager nativeregistration failed\n");

            goto bail;

        }

     

        /* success -- return valid version number*/

        result = JNI_VERSION_1_4;

     

    bail:

        return result;

    }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.