Android Studio android.mk files written using Eclipse engineering

Source: Internet
Author: User

Now Android Studio is already 1.x version, its function is also more and more perfect, personal feeling as still good use, now is also more and more companies to use as, easy to use is a factor, Google officially does not support Eclipse is also a factor, regardless of it, anyway is to use as, exactly, before the time , my company is also turning to as. After a while, think about it or write down this article and share the issue of the NDK compilation when Eclipse turns as.

Gradle is a multi-platform compiler language, do not know why, on Android on the NDK support has been not very good, although now simple project can be compiled normally, but if the NDK part is more complex, then headache, at least, At the moment I find there is no way to compile a static library using Gradle build. may also be the official documentation I did not look too detailed, if anyone knows how to use Gradle to compile the NDK's static library, please inform, thank you.

Exactly, I developed the program dynamic library parts more complex, a number of requirements gradle is not enough, just at the beginning of the time there is no clue, looking for solutions, there are several ways to find a comparison, found that there is a method is basically seamless docking, there is no complicated operation, so use it, Now put the method to the people in need, avoid detours.

    1. Local.properties add Ndk.dir to set the NDK path
    2. The JNI directory in the Eclipse project is copied to the Src/main directory
    3. android{}, add Sourcesets. main.jni.srcDirs = [] and sourcesets. Main. jnilibs.srcdir ' src/main/libs '
    4. Add the following code to the Build.gradle
Tasks.withtype (javacompile) {Compiletask-Compiletask.dependson ndkbuild}task ndkbuild (type:exec) {Workingdir file ('Src/main/jni') CommandLine getndkbuildcmd ()}task cleannative (type:exec) {Workingdir file ('Src/main/jni') CommandLine getndkbuildcmd (),' Clean'}clean.dependson cleannativedef Getndkdir () {if(System.env.ANDROID_NDK_ROOT! =NULL)        returnSystem.env.ANDROID_NDK_ROOT Properties Properties=NewProperties () properties.load (Project.rootProject.file ('local.properties'). Newdatainputstream ()) def Ndkdir= Properties.getproperty ('Ndk.dir',NULL)    if(Ndkdir = =NULL)        Throw NewGradleexception ("NDK location is not found. Define location with Ndk.dir in the Local.properties file or with an android_ndk_root environment variable.")    returnndkdir}def Getndkbuildcmd () {def ndkbuild= Getndkdir () +"/ndk-build"Ndkbuild+=". Cmd"    returnNdkbuild}

It's so simple, it's done. After joining these tasks, the compilation process becomes the Ndk-build clean task before the clean operation, which executes the Ndk-build task before compiling the action, ensuring that Gradle clean is executed when the compilation is executed Ndk-build The clean operation, which performs the Ndk-build operation before the compilation starts, ensures that all the dynamic libraries are compiled, and in the directory Src/main/libs, and then when the APK is compiled, it is packaged directly using the dynamic library in Src/main/libs.

Finally execute the gradle clean build, we can see in the log ndk-build clean and Ndk-build commands are running, the out directory generated by the APK also has a corresponding dynamic library.

2015.12.28

------END------

Android Studio android.mk files written using Eclipse engineering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.