2023-01-20 241
我的本地库包含我想在编译时删除的日志.
通过在LOCAL_CFLAGS中定义前处理器宏ENABLE_DEBUG来显示日志:
include $(CLEAR_VARS)
LOCAL_MODULE := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)
我正在通过Android Studio与Gradle一起构建该应用程序,我想在没有LOCAL_CFLAGS := -DENABLE_DEBUG的情况下拥有另一个android.mk文件以进行发布构建,有效地禁用了日志记录.
我试图通过在src下创建文件夹release/jni来做到这一点,并在没有CFLAG的情况下放置Android.mk的副本.它可以成功构建和部署,但我仍然看到日志.这是我的build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
sourceSets {
main {
//Tell Gradle where to put the compiled shared library
jniLibs.srcDir 'src/main/libs'
//disable automatic ndk-build call
jni.srcDirs = [];
}
release {
jni.srcDirs = [];
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Tell Gradle the run the ndkBuild task when compiling
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
// This task utilizes the Android.mk file defined in src/main/jni so that you
// have more control over the build parameters (like library inclusion)
// The system must define property 'androidNdkHome' in ~/.gradle/gradle.properties file
// to point to NDK path
task ndkBuild(type: Exec) {
commandLine "$androidNdkHome/ndk-build", '-C', file('src/main/jni').absolutePath
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.2'
}
我的项目结构看起来像这样:
src/
main/
jni/
Android.mk
release/
jni/
Android.mk
有可能做我想做的事吗?
我认为我在这里有一个解决方案.它并不像我想要的那样漂亮(尤其是涉及创建临时文件),但是我已经对其进行了测试,并且正在按预期工作.
我已经写了这篇文章以使用另一个 application.mk 进行调试构建,称为 application-debug.mk .您可以将APP_CFLAGS := -DENABLE_DEBUG放在里面做您想做的事情.
关于其工作原理的评论是在代码中.如果是用于应用程序构建文件,请使用applicationVariants.all而不是libraryVariants.all:
android.libraryVariants.all { variant -> // executes this block for each variant, current and futures
def task = tasks.create(name: "ndk${variant.buildType.name.capitalize()}Build") { //create ndkReleaseBuild and ndkDebugBuild tasks
doLast {
exec { //execute all this block when the task is executed. (A Build task gets only the commandLine to be executed, so here we're using doLast.exec instead)
def extraParameter = ""
def rebuildFlag = ""
if (variant.buildType.name == "debug")
extraParameter = "NDK_APPLICATION_MK=" + file('src/main/jni/Application-debug.mk').absolutePath //reference to another Application.mk to use when doing a debug build.
def lastBuildFile = file('src/main/jni/.lastbuild.tmp') //save the last build type to a temp file, to avoid triggering rebuilds all the time.
if (!lastBuildFile.exists())
lastBuildFile.createNewFile()
def lastBuildType = lastBuildFile.text
if (lastBuildType != variant.buildType.name) {
println "== ndk build variant has changed, triggering full rebuild. =="
rebuildFlag = "-B"
}
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
} else {
commandLine 'ndk-build', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
}
lastBuildFile.write(variant.buildType.name)
}
}
}
variant.javaCompile.dependsOn task
}
我也将此代码推送为GIST:/92F1F664C453869636F8
以上所述是小编给大家介绍的Android NDK和Gradle。每个构建类型有不同的Android.mk,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/26043.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态