0

Is there any way to include assembly code into a native library module in android studio.

Since this post says that you can only include *.C, *.CPP, *.c++, *.cc, *.cp, *.cpp, *.cxx files with srcDir i was wondering if there is maybe another solution or workaround to at least compile it with the native library.

I guess i could pre-compile the assembly code and then link it to my native module but with this i would have the problem that i have to rebuild it at every change and then rebuild the project itself which is not really a nice workflow so if you have any nicer ways let me know.

Community
  • 1
  • 1
Someone
  • 560
  • 9
  • 21
  • So wait, it doesn't run the make to build it? Oh well... (not looking forward to migrate my NDK projects under gradle ... as I'm using the Makefiles+`ndk-build` heavily to define everything needed during build and steps of build). Isn't there some easy way to code custom build step running make in the gradle? (I mean, it's groovy, so it should be programmable? But I never really understood it, actually the whole gradle annoys a hell out of me, both by its performance and cryptic way of build process definition. I'm all the time on the verge of dropping it completely and write GNU make rules.) – Ped7g Aug 01 '16 at 16:04
  • Because you mention it, I think i read somewhere that you can use a custom makefile to build it inside of android studio, i will definitely take a look at it. – Someone Aug 01 '16 at 16:55
  • The `ndk-build` itself does use make, so I don't use any custom setup to run make, I just put the custom rules into those common Makefiles from NDK examples (and use the ndk-build script from NDK to build it). There's probably way to run the ndk-build from gradle (as you can probably code anything into gradle), but I didn't do that yet. – Ped7g Aug 01 '16 at 19:15

1 Answers1

1

First create include folder inside jni folder and libs folder inside the same folder. You also can create inside libs folder different folders for different chipsets (armeabi, mips,x86).

Copy your .h files inside include folder and your compile files (.a) in your libs folders

Then you have to change your build.gradle, adding a new repository inside the model

repositories {
    libs(PrebuiltLibraries) {
        libYourCLibrary{
            headers.srcDir "src/main/jni/include"
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = file("src/main/jni/libs/${targetPlatform.getName()}/YOUR_C_LIBRARY.a")
            }
        }
    }
}

add dependencies of this repository inside the model

android.sources {
    main {
        jni {
            dependencies {
                library "libYourCLibrary" linkage "static"
            }
        }
    }
}

And finally Sync project with gradles files.

Miguel Benitez
  • 2,322
  • 10
  • 22
  • I know how to include a prebuilt library but i don't really want to prebuild my assembly code because i would like to just tell android studio to build it and wait until its done instead of manually compiling it with every change. – Someone Aug 01 '16 at 14:51
  • As far as I know It's not possible yet to do it in this way. The only way that I can imagine is using ant with gradle to built your c lib, copy in libs as static and then run gradle, maybe this to links could be useful http://ant-contrib.sourceforge.net/cpptasks/ and https://docs.gradle.org/current/userguide/ant.html – Miguel Benitez Aug 01 '16 at 15:09
  • This would be better than building it manuay but it is not really an ideal solution but thank you for the links. – Someone Aug 01 '16 at 16:56