使用Traditional make文件调试AndroidStudio NDK

时间:2015-10-06 00:33:15

标签: android debugging android-studio android-ndk android-gradle

根据链接http://ph0b.com/new-android-studio-ndk-support/查看子标题"使用Android.mk/Application.mk" ;,文章声称可以使用Android Studio进行编辑和调试。我已按照该文章的所有说明进行操作,但调试器似乎从未遇到过断点。这对任何人都有效吗?

更多信息

我的设置

  1. Android Studio 1.4
  2. Gradle 2.6
  3. Android Gradle插件0.3.0-alpha5
  4. 我的build.gradle看起来像这样

    申请插件:' com.android.model.application'

    模型{     android {         compileSdkVersion = 21         buildToolsVersion =" 23.0.0 rc2"

        defaultConfig.with {
            applicationId = "blah.blah1.com.blah3"
            minSdkVersion.apiLevel = 21
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }
    }
    
    android.buildTypes {
        release {
            minifyEnabled = false
        }
    }
    
    android.ndk {
        moduleName = "blahPlayer"
        ldLibs = ["log"]
        cppFlags += "-std=c++11"
        cppFlags += "-I${blahRoot}".toString()
        stl = "gnustl_shared"
    }
    
    android.sources {
        main.jni {
            source {
                srcDirs = ['src/main/none']
            }
        }
        main.jniLibs {
            source {
                srcDirs = ['src/main/libs']
            }
        }
    }
    
    android.productFlavors {
        create("arm") {
            ndk.with {
                abiFilters += "armeabi-v7a"
            }
        }
    }
    

    }

  5. 我的原生资源在app / src / jni中的资源和FS的不同位置之间分配(因为某些来源是第三方)

  6. 代码完成正常(我可以在第三方源中包含和使用源代码)
  7. 我使用Android.mk和Application.mk以及外部的ndk-build构建所有本机源。 SO文件在jniLibs下生成,应用程序运行正常
  8. 当我在app / src / jni或第三方源位置的本机源中设置断点时,调试器永远不会命中它们。

1 个答案:

答案 0 :(得分:0)

使用传统的makefile编译时,生成如下结构:

-obj 
  -local
    -armeabi-v7a
        -obj-debub
         xxx.o
         xxx.d
         zzz.o
         zzz.d 
      hello-jni.so 

使用Android Studio调试的技巧是jniLibs块的配置。必须是这样的:

main.jniLibs {
            source {
//                srcDirs = ['src/main/libs']
                srcDirs = ['src/main/obj/local']
            }
        }

为了不再怀疑,我的gradle看起来像这样。

    import org.apache.tools.ant.taskdefs.condition.Os
    plugin: 'com.android.model.application'
    model {
       android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.0"
        defaultConfig.with {
            applicationId = "com.example.hellojni"
            minSdkVersion.apiLevel = 4
            targetSdkVersion.apiLevel = 23
        }
        tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn buildNative
        }
      }

    compileOptions.with {
        sourceCompatibility=JavaVersion.VERSION_1_7
        targetCompatibility=JavaVersion.VERSION_1_7
     }
    android.ndk {
        moduleName = "hello-jni"
        stl = 'gnustl_shared'
     }
    android.sources {
        main.jni {
            source {// srcDirs = ['src/main/jni/']
                       srcDirs = ['src/main/jni/none']
            }
        }
        main.jniLibs {
            source {// srcDirs = ['src/main/libs']
                       srcDirs = ['src/main/obj/local']
            }
        }
     }
    android.buildTypes {
      release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
   } 
   String 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').newDataI
 putStream())
    String ndkdir = properties.getProperty('ndk.dir', null)
    if (ndkdir == null)
        throw new GradleException("NDK location not found. Define 
ocation with ndk.dir in the local.properties file or with an 
NDROID_NDK_ROOT environment variable.")
    return ndkdir
}
String getNdkBuildCmd() {
    String ndkbuild = getNdkDir() + "/ndk-build"
    if (Os.isFamily(Os.FAMILY_WINDOWS))
      ndkbuild += ".cmd"
    return ndkbuild
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine getNdkBuildCmd(),
            '-C', file('src/main/').absolutePath, // Change src/main/jni the relative path to your jni source
            '-j', Runtime.runtime.availableProcessors()
//            'NDK_DEBUG=1'
}

我的Android.mk文件:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/Application.mk
LOCAL_MODULE := hello-jni
LOCAL_PATH := $(call my-dir)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/
LOCAL_LDLIBS := -llog \
                 -landroid \
LOCAL_SRC_FILES := bmain.c \
                   hello-jni.c
LOCAL_CPPFLAGS += -std=c++11
include $(BUILD_SHARED_LIBRARY)

我的Application.mk文件:

APP_OPTIM := debug
APP_ABI   := armeabi-v7a
APP_PLATFORM := android-10
APP_STL := gnustl_static