[Android Studio]-Add a JNI generated file (. so file) to the Gradle Project)

Source: Internet
Author: User

We have been using Android Studio for more than half a year to adapt to the trend!

Adding. jar files and. so files from the Eclipse migration project to Android Studio is undoubtedly a very important and headache!

In the latest version, all. jar files under libs are automatically packaged by default, so you don't need to talk about jar.

Add. SO (this depends on your project Gradle Version)

Tip: both build. gradle files are set.

(1) the old version seems to be earlier than 0.5. The specific method is as follows:

task copyNativeLibs(type: Copy) {    from(new File(project(':MyProject').buildDir, 'native-libs')) { include '**/*.so' }    into new File(buildDir, 'native-libs')}tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }clean.dependsOn 'cleanCopyNativeLibs'tasks.withType(com.android.build.gradle.PackageApplicationTask) { pkgTask ->    pkgTask.jniDir new File(buildDir, 'native-libs')}

(2) The new version is missing. If you download the latest version, it comes with the latest compiler.

I now know three methods to package the. SO file.

(2.1) package the package into a. Jar file and then automatically unpackage it to the apk File

task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {    destinationDir file("$buildDir/native-libs")    baseName 'native-libs'    extension 'jar'    from fileTree(dir: 'libs', include: '**/*.so')    into 'lib/'}tasks.withType(Compile) {    compileTask -> compileTask.dependsOn(nativeLibsToJar)}

The following statement is the. jar file in the build \ native-libs directory.

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')


(2.2) manually generate the. Jar file and then automatically unpackage it to the apk File

This method needs to be performed manually. to compress the SO file, perform the following steps: compress all the. sofiles used in the zip file into a. zip file (the file directory structure in the zip file is lib/armeabi /*. so) then change the suffix of the zip file. jar and put it in libs to generate the apk.


By default, all. Jar files are automatically packaged:

dependencies {    compile fileTree(dir: 'libs', include: '*.jar')}

(2.3) This is now Method I am using (recommended)In fact, it takes more than half a day. SO files are packaged into the lib folder of the APK. if you carefully read the usage of Gradle, you will naturally know that the official version of Gradle has automatically implemented packaging. SO file. at a very simple level, you can add the following content to the configured android node:

sourceSets {        main {            jniLibs.srcDirs = ['libs']        }    }

The configuration file of the entire project is as follows:

apply plugin: 'android'android {    compileSdkVersion 19    buildToolsVersion "19.0.0"    defaultConfig {        minSdkVersion 16        targetSdkVersion 19        versionCode 1        versionName "1.0"    }    buildTypes {        release {            runProguard false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }    }    sourceSets {        main {            jniLibs.srcDirs = ['libs']        }    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])}

This is not very simple. You do not need to manually package or copy commands in a complicated way, so you can simply perform the operation. It can be said that it is important to look at the official documentation.

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.