vinjn / articles

Everything is possible
8 stars 0 forks source link

How to build pure C++ Android application #16

Open vinjn opened 11 years ago

vinjn commented 11 years ago

The following steps take /ndk/samples/native-activity as the example

install adt

( android-dev-tool, which contains sdk && eclipse) important sdk tools:

important ndk tools:

set JAVA_HOME = c:\Program Files\Java\jdk1.7.0_25\

generate buid.xml

$ android update project -p "/project/full/path"

you can simply enter to the path of your project using cd then you use $ android update project -p .

Sucess

Updated and renamed default.properties to project.properties Updated local.properties No project name specified, using Activity name 'NativeActivity'. If you wish to change it, edit the first line of build.xml. Added file C:\android-ndk-r8e\samples\native-activity\build.xml Added file C:\android-ndk-r8e\samples\native-activity\proguard-project.txt

Error

Error: The project either has no target set or the target is invalid. Please provide a -t or --target to the android.bat update command.

$ android list targets to find supported targets $ android update project -p . -t android-10 to explicitly specify the target id

A proper output would be:

Available Android targets:
----------
id: 1 or "android-17"
     Name: Android 4.2.2
     Type: Platform
     API level: 17
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
     ABIs : armeabi-v7a

We can change the content of default.properties file.

build jni so

$ ndk-build

build apk

$ ant clean debug # clean to make sure .so file is replaced $ adb install -r bin/NativeActivity-debug.apk # -r means forcefully install

run apk from adb

adb shell commands is am start -n your.package.name/your.activity.name $ adb shell am start -n com.example.native_activity/android.app.NativeActivity

error

E/AndroidRuntime( 9570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.native_activity/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to find native library: native-activity
E/AndroidRuntime( 9570): Caused by: java.lang.IllegalArgumentException: Unable to find native library: native-activity
<application android:label="@string/app_name" android:hasCode="false">

changed to

<application android:label="@string/app_name" android:hasCode="true">

create an empty src/ folder and try ant debug again

vinjn commented 11 years ago

compile-instal.bat

call ndk-build
call ant debug
call adb install -r bin\NativeActivity-debug.apk
vinjn commented 11 years ago

android-ndk-r7b\samples\native-activity-diy\jni\main.c patch

#if 0
    context = eglCreateContext(display, config, NULL, NULL);
#else
    EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
    context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
#endif
vinjn commented 6 years ago

Check https://github.com/vinjn/android-survival-kit.cpp