使用Android Studo开发NDK之Gradle的配置(能debug C代码)
配置:
用的版本号是AS1.5(也能够尝试更高版本号)。 Gradle地址是distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
NDK用的是CrystaX NDK
有參考:https://github.com/TsinStudio/AndroidDev/blob/master/Android%20NDK%20C%2B%2B%20开发利器:Android%20Studio.md
常见问题:
1.我在mk的NDK配置和gradle的NDK配置间切换时,突然不能debug进C++代码了。然后通过
清空main/jni/android.mk文件,
点击build->clean,
在debug界面右键tab标题(如 ctbManager Native)->close tab(关闭全部debug的tab)后又能debug进去了,
不知道是哪一步fix了问题。假设遇到编译成功但不能debug C++文件,尝试这几个步骤吧。同一时候不建议mk和gradle配置之间切换。
2.假设C文件报错。考虑能否改动C文件,比解决报错要简单
gradle代码:
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.internal.os.OperatingSystem; apply plugin: 'com.android.model.application' final APP_ABIS = ["armeabi", "armeabi-v7a","arm64-v8a"]
final BOOST_SHARED_LIBS = ["boost_serialization","boost_system","boost_filesystem","boost_thread"] model { android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
applicationId = "com.duotin.ctblib"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = compileSdkVersion.asType(Integer)
versionCode = 1
versionName = "1.0"
}
} android.ndk {
toolchain = "gcc"
toolchainVersion = "4.9"
moduleName = "ctblib"
cppFlags.add("-std=c++11")
cppFlags.add("-fexceptions")
cppFlags.add("-frtti")
cppFlags.add("-I" + getBoostIncDir())
cppFlags.addAll(["-fvisibility=hidden", "-Wno-error=format-security",
"-I${file("src/main/jni/jnisrc")}".toString(),
"-I${file("src/main/jni/src/Jelly")}".toString(),
"-I${file("src/main/jni/src/Papaya")}".toString(),
"-I${file("src/main/jni/src/Helper")}".toString(),
"-I${file("src/main/jni/src/Helper/utf8")}".toString(),
"-I${file("src/main/jni/src/CTBModel")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBAlbum")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBAlbumParser")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBBaseModel")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBTrack")}".toString(),
"-I${file("src/main/jni/src/Xml")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlAttrNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlBaseNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlElementNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlTextNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlUtil")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlUtil/BoostXmlUtil")}".toString(),
"-I${file("src/main/jni/src/Module")}".toString(),
"-I${file("src/main/jni/src/CTBDataManager")}".toString(),
"-I${file("src/main/jni/src/Sync/SyncProcess")}".toString(),
"-I${file("src/main/jni/src/Sync/Task")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/Utils")}".toString()]
)
cppFlags.add("-I${file("src/main/jni/neon/include")}".toString())
CFlags.addAll(["-I${file("src/main/jni/jnisrc")}".toString(),
"-I${file("src/main/jni/src/Jelly")}".toString(),
"-I${file("src/main/jni/src/Papaya")}".toString(),
"-I${file("src/main/jni/src/Helper")}".toString(),
"-I${file("src/main/jni/src/Helper/utf8")}".toString(),
"-I${file("src/main/jni/src/CTBModel")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBAlbum")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBAlbumParser")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBBaseModel")}".toString(),
"-I${file("src/main/jni/src/CTBModel/CTBTrack")}".toString(),
"-I${file("src/main/jni/src/Xml")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlAttrNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlBaseNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlElementNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlModel/XmlTextNode")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlUtil")}".toString(),
"-I${file("src/main/jni/src/Xml/XmlUtil/BoostXmlUtil")}".toString(),
"-I${file("src/main/jni/src/Module")}".toString(),
"-I${file("src/main/jni/src/CTBDataManager")}".toString(),
"-I${file("src/main/jni/src/Sync/SyncProcess")}".toString(),
"-I${file("src/main/jni/src/Sync/Task")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/Utils")}".toString(),
"-I${file("src/main/jni/neon//external/android")}".toString(),
"-I${file("src/main/jni/neon/external/openssl/include/openssl")}".toString(),
"-I${file("src/main/jni/neon/external/openssl/include")}".toString(),
"-I${file("src/main/jni/neon/external/expat/include")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString(),
"-I${file("src/main/jni/src/SyncLog")}".toString()])
CFlags.addAll(["-DDEBUG"])
ldLibs.addAll BOOST_SHARED_LIBS
ldLibs.addAll(["log", "m", "z"])
ldFlags.addAll(["-llog","-fvisibility=hidden","-L/usr/lib ","-lm"])
stl = "gnustl_shared"
} android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.pro'))
}
} android.sources {
main {
jni {
source {
srcDir "src/main/jni"
dependencies {
APP_ABIS.each { abi ->
library file("src/main/jni/neonlib/${abi}/libneon.a") abi "${abi}"
library file("src/main/jni/neon/external/expat/${abi}/libexpat.a") abi "${abi}"
library file("src/main/jni/openssllib/${abi}/libcrypto.so") abi "${abi}"
library file("src/main/jni/openssllib/${abi}/libssl.so") abi "${abi}"
}
}
}
}
jniLibs {
source {
srcDir "src/main/libs"
}
}
}
} android.productFlavors {
APP_ABIS.each { abi ->
create(getFlavorName(abi)) {
ndk.with {
abiFilters.add(abi)
getPrebuiltLibPaths(abi).each { path ->
ldFlags.add("-L" + path)
}
}
}
}
}
} tasks.all { task ->
println "printTask#${task}"
if (task.name.startsWith('link')) {
println "${task.name}.startsWith('link')"
task.dependsOn copyNativeLibs
}
} task copyNativeLibs {
["debug", "release"].each { buildType ->
APP_ABIS.each { abi ->
def libs = [:]
BOOST_SHARED_LIBS.each { name ->
libs[name] = "${getBoostLibDir(abi)}/lib${name}.so"
}
libs.crystax = getLibCrystax(abi) libs.each { name, file ->
dependsOn tasks.create(name: "copy-native-library-${name}-${abi}-${buildType}", type: Copy) {
from file
into getTargetLibDir(abi, buildType)
}
}
}
}
} task stripNativeLibs(dependsOn: copyNativeLibs) {
["debug", "release"].each { buildType ->
APP_ABIS.each { abi ->
def libs = []
libs.addAll(BOOST_SHARED_LIBS)
libs += "crystax" libs.each { name ->
dependsOn tasks.create(name: "strip-native-library-${name}-${abi}-${buildType}", type: Exec) {
commandLine getStripExecutable(abi), "--strip-unneeded", "${getTargetLibDir(abi, buildType)}/lib${name}.so"
}
} }
}
} dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':ApiClient')
compile 'de.greenrobot:eventbus:2.4.0'
} def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkdir = properties.getProperty('ndk.dir', null)
if (ndkdir == null)
throw new GradleException("""\
NDK location not found.
Define location with ndk.dir in the local.properties file
or with an ANDROID_NDK_ROOT environment variable.""") return ndkdir
} def getCrystaxNdkDir() {
def ndkDir = getNdkDir()
if (!(new File(ndkDir, "sources/crystax").exists()))
throw new GradleException("""\
'${ndkDir}' is not a CrystaX NDK.
Edit ndk.dir in local.properties or set ANDROID_NDK_ROOT
environment variable pointing to CrystaX NDK""") return ndkDir
} def getFlavorName(abi) {
switch (abi) {
case "armeabi":
return "arm";
case "armeabi-v7a":
return "arm7"
case "arm64-v8a":
return "arm64"
default:
return abi.replaceAll('-', '_')
}
} def getToolchainName(abi) {
switch (abi) {
case ~/^armeabi.*/:
return "arm-linux-androideabi"
case ~/^arm64.*/:
return "aarch64-linux-android"
case "mips":
return "mipsel-linux-android"
case "mips64":
return "mips64el-linux-android"
case ["x86", "x86_64"]:
return abi
default:
throw new GradleException("Unsupported ABI: '${abi}'")
}
} def getToolchainPrefix(abi) {
switch (abi) {
case ~/^armeabi.*/:
return "arm-linux-androideabi"
case ~/^arm64.*/:
return "aarch64-linux-android"
case "mips":
return "mipsel-linux-android"
case "mips64":
return "mips64el-linux-android"
case "x86":
return "i686-linux-android"
case "x86_64":
return "x86_64-linux-android"
default:
throw new GradleException("Unsupported ABI: '${abi}'")
}
} def getHostOS() {
if (OperatingSystem.current().isLinux())
return "linux"
if (OperatingSystem.current().isMacOsX())
return "darwin"
if (OperatingSystem.current().isWindows())
return "windows"
throw new GradleException("Unsupported host OS")
} def getHostArch() {
def arch = System.getProperty("os.arch")
switch (arch) {
case ["x86_64", "amd64"]:
return "x86_64"
case ~/^i[3456]86/:
case "x86":
return "x86"
default:
throw new GradleException("Can't detect host's CPU architecture: '${arch}'")
}
} def getHostTag() {
def tag = getHostOS()
def arch = getHostArch()
if (tag != "windows" || arch != "x86")
tag += "-${arch}"
return tag
} def getStripExecutable(abi) {
def ndk = getCrystaxNdkDir()
def toolchainName = getToolchainName(abi)
def toolchainPrefix = getToolchainPrefix(abi)
def hostTag = getHostTag()
def strip = "${ndk}/toolchains/${toolchainName}-4.9/prebuilt/${hostTag}/bin/${toolchainPrefix}-strip"
if (OperatingSystem.current().isWindows())
strip = strip.replaceAll('/', '\\\\') + '.exe'
return strip
} def getPrebuiltLibPaths(abi) {
def paths = []
paths += getBoostLibDir(abi)
paths += getLibCrystaxDir(abi)
return paths
} def getTargetLibDir(abi, buildType) {
return "${buildDir}/intermediates/binaries/${buildType}/${getFlavorName(abi)}/lib/${abi}"
} def getLibCrystaxDir(abi) {
return "${getCrystaxNdkDir()}/sources/crystax/libs/${abi}"
} def getLibCrystax(abi) {
return "${getLibCrystaxDir(abi)}/libcrystax.so"
} def getBoostDir() {
return "${getCrystaxNdkDir()}/sources/boost/1.58.0"
} def getBoostIncDir() {
return "${getBoostDir()}/include"
} def getBoostLibDir(abi) {
return "${getBoostDir()}/libs/${abi}"
}
使用Android Studo开发NDK之Gradle的配置(能debug C代码)的更多相关文章
- Android高效开发环境(Genymotion,Gradle,Andriod Studio)
临近十一,项目接近上线,终于有些碎片时间可以查看一些博客. 这篇博客是Android开发大牛Cyril Mottier在去年写的博客,我把它翻译一下共享给国内志同道合的朋友,同时也是对自己一个很好的锻 ...
- [Android] 环境配置之Android Studio开发NDK
分类:Android环境搭建 (14351) (20) ========================================================作者:qiujuer博客:bl ...
- Mac Android开发环境变量的配置(java、sdk、ndk、gradle)
1.打开terminal 2.然后输入 vi .bash_profile 后按"e"进入编辑模式 3.输入想要配置的环境变量(Java.sdk.ndk.gradle): expor ...
- Android NDK开发 Android Studio使用新的Gradle构建工具配置NDK环境(一)
本文主要讲述了如何如何在Android Studio使用新的Gradle构建工具配置NDK环境,现在把相关的步骤整理出来分享给Android程序员兄弟们,希望给他们在配置NDK环境时带来帮助. 从An ...
- android 串口开发第一篇:搭建ndk开发环境以及第一个jni调用程序
一:ndk环境搭建 1:开发环境 我使用的是android studio 2.3.3版本,搭建ndk开发环境比较简单,打开File----Settings----Appearance&Beha ...
- Android开发之深入理解Android Studio构建文件build.gradle配置
摘要: 每周一次,深入学习Android教程,TeachCourse今天带来的一篇关于Android Studio构建文件build.gradle的相关配置,重点学习几个方面的内容:1.applica ...
- Android应用开发编译框架流程与IDE及Gradle概要
1 背景 建议阅读本文之前先阅读<Android Studio入门到精通>和<Groovy脚本基础全攻略>及<Gradle脚本基础全攻略>三篇博客作为背景知识,这样 ...
- Android游戏开发实践(1)之NDK与JNI开发03
Android游戏开发实践(1)之NDK与JNI开发03 前面已经分享了两篇有关Android平台NDK与JNI开发相关的内容.以下列举前面两篇的链接地址,感兴趣的可以再回顾下.那么,这篇继续这个小专 ...
- Android游戏开发实践(1)之NDK与JNI开发01
Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...
随机推荐
- TCP心跳包
所谓的心跳包就是客户端定时放送简单的信息给服务器端,告诉它我还在而已.代码就是每 隔几分钟发送一个固定信息给服务器端,服务器端回复一个固定信息.如果服务器端几分钟后没有收到客户端信息则视客户端断开.比 ...
- Wannafly挑战赛25 A 因子 数学
题面 题意:令 X = n!,给定一大于1的正整数p,求一个k使得 p ^k | X 并且 p ^(k + 1) 不是X的因子,n,,p(1e18>=n>=1e4>=p>=2) ...
- Mac osx下成功安装tensorflow
安装tensorflow有多种方式:Pip, Docker, Virtualenv, Anaconda 或 源码编译的方法安装. 我是在pip下成功安装tensorflow,以下只介绍在pip下安装( ...
- 简单介绍export default,module.exports与import,require的区别联系
他们都是成对使用的,不能乱用: module.exports 和 exports是属于CommonJS模块规范,对应---> require属于CommonJS模块规范 export 和 exp ...
- webpack打包大概流程
webpack 步骤 1. 新建一个webpack.config.prod.js 2. 压缩bundle.js和index.html //设置为生产环境 new webpack.DefinePlugi ...
- 利用JavaScript的%读分秒
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 【SQL】分析函数功能-排序
1:排名,不考虑并列问题 row_number() 2:排名,有并列,并列后的排名不连续 rank() 3:排名,有并列,并列后的排名连续 dense_rank() 测试: SQL> creat ...
- Python框架、库和软件资源大全(整理篇)
有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...
- php 的多进程
php的多进程处理依赖于pcntl扩展,通过pcntl_fork创建子进程来进行并行处理 例子1: <?php $pid = pcntl_fork(); if($pid == -1) { //错 ...
- 洛谷P3195 [HNOI2008]玩具装箱TOY 斜率优化
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 100000 ...