1

Per the link http://ph0b.com/new-android-studio-ndk-support/ see sub heading "Using Android.mk/Application.mk", the article claims that one can use Android Studio for editting AND debugging. I have followed all the instructions per that article, but the debugger never seems to hit the breakpoint. Has this worked for anyone?

Some more info

My setup

  1. Android Studio 1.4
  2. Gradle 2.6
  3. Android Gradle plugin 0.3.0-alpha5
  4. My build.gradle looks like this

    apply plugin: 'com.android.model.application'

    model { 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. My native source is split between sources in app/src/jni and also in a different location of the FS (as some source is third party)

  6. Code completion works fine ( I am able to include and use the source in third party source)
  7. I build all native sources using Android.mk and Application.mk and ndk-build externally. The SO files are generated under jniLibs and the app runs fine

When I set a breakpoint in the native source either under app/src/jni or the third party source location, debugger never hits them.

Harkish
  • 2,262
  • 3
  • 22
  • 31
  • Check *[define LOCAL_SRC_FILES in ndk{} DSL](http://stackoverflow.com/a/32640823/192373)*. TL;NR: build your code as **static** library with `ndk-build`, link into **shared** library with gradle plugin. – Alex Cohn Oct 06 '15 at 03:29

1 Answers1

0

When compiled with traditional makefile, is generated a structure like this:

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

The trick to debug with Android Studio is the configuration of jniLibs block. Must be like this:

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

To not remain doubts,my gradle looks like this.

    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'
}

My Android.mk file:

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)

My Application.mk file:

APP_OPTIM := debug
APP_ABI   := armeabi-v7a
APP_PLATFORM := android-10
APP_STL := gnustl_static
F. Eccard
  • 126
  • 1
  • 2