android studio 1.0 开发 ndk 调用 c++ so库
一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟我一样的人,欢迎随便转载:
1.下载安装android sdk和ndk,ndk r10(目前最新)是单独可以编译c++的,无需cygwin。
apply plugin: ‘com.android.application‘ android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.sample.hello" minSdkVersion 15 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ } } sourceSets { main { jniLibs.srcDirs = [‘libs‘] } } } dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) compile ‘com.android.support:appcompat-v7:21.0.3‘ }
/* * 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. */ package com.example.hellojni; public class HelloJni { /* A native method that is implemented by the * ‘hello-jni‘ native library, which is packaged * with this application. */ public native String stringFromJNI(String msg); /* This is another native method declaration that is *not* * implemented by ‘hello-jni‘. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the ‘hello-jni‘ library on application * startup. The library has already been unpacked into * /data/data/com.example.hellojni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni"); } }
import com.example.hellojni.HelloJni;
finalEditText editText =(EditText) findViewById(R.id.editText); HelloJni hj =newHelloJni(); editText.setText(hj.stringFromJNI("this is java!"));
LOCAL_CFLAGS := -std=c99
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。