Prerequisiti x windows = aver installato CygWin (
www.cygwin.com)
Percorsi:
<provaNDK> = path del progetto Android che andiamo a creare.
<android_ndk_path> = path dell'Android NDK (che trovate qui
Android NDK | Android Developers)
<java_jdk> = path del java software development kit (scaricare e installare il JDK Java da qui
Java SE Downloads - Sun Developer Network (SDN))
Bene, partiamo!
1) Creare un nuovo progetto Android chiamato provaNDK
Project name: provaNDK
Application name: provaNDK
Package name: com.doom.provandk
Nome activity: frmMain
Min SDK Version: 4
2) Nel frmMain mettere questo codice:
package com.doom.provandk;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class frmMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("provaNDK",new NativeLib().getHelloWorld());
}
}
class NativeLib
{
public native String getHelloWorld();
static
{
System.loadLibrary("helloworld");
}
}
3) Andare con la linea di comando del dos in <provaNDK>/bin
4) lanciare "<java_jdk>\javah -jni com.doom.provandk.NativeLib" (attenzione al path di javah, potrebbe essere diverso!)
Adesso dovrebbe creato il file com_doom_provandk_NativeLib.h in <provaNDK>/bin
5) Creare cartella 'provaNDK' in <android_ndk_path>/sources
6) Metterci com_doom_provandk_NativeLib.h appena creato
7) Creare sempre lì il file doom.c col seguente codice:
#include "com_doom_provandk_NativeLib.h"
JNIEXPORT jstring JNICALL Java_com_doom_provandk_NativeLib_getHelloWorld
(JNIEnv * env, jobject obj)
{
return (*env)->NewStringUTF(env, "Ciao, neh?!");
}
8) Nella stessa dir creare un file chiamato Android.mk e scrivergli:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld
LOCAL_SRC_FILES := doom.c
include $(BUILD_SHARED_LIBRARY)
9) Adesso andare nella cartella 'apps' in <android_ndk_path> (e se non c'è, la creo la 'apps'!) e creo una cartella provandk' in cui creo un file chiamato Application.mk, che contiene:
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := helloworld
10) Poi creo, sempre in apps le seguenti cartelle 'project/jni' e ci metto, in jni, i files doom.c, Android.mk e
com_doom_testnkd2_NativeLib.h
11) Ora, da linea di comando, torno sul livello di <android ndk path> e lancio:
make APP=testndk2
Dovrebbe scrivere qualcosa tipo:
Android NDK: Building for application 'provandk'
Compile thumb : helloworld <= apps/provandk/project/jni/doom.c
SharedLibrary : libhelloworld.so
Install : libhelloworld.so => apps/provandk/project/libs/armeabi
12) E' stato creato in <android_ndk_path>/apps/project/libs/armeabi il file libhelloworld.so che è da copiare nel progetto Eclipse (copiare quindi la cartella libs nel progeto eclipse).
13) Fare un refresh al progetto e dovrebbe apparire la cartella.
14) Ora lanciare il progetto e voilà! Nel logCat apparirà la fatidica 'Ciao, neh?!'
