How to correctly introduce so file into Android studio

Posted by jgh84 on Fri, 15 May 2020 18:09:41 +0200

How to introduce the third-party so file? However, the third-party official only gives the integration mode in ADT environment.

Android studio uses gradle compiling mode by default, which is different from ADT editing mode, so how should so files be introduced?


It's very simple. Here, take the integrated JPUSH as an example to see how the so file can be introduced into the compilation environment, and finally can be called directly by JNI.


First, create the libs directory in the root directory of our Module, and copy the so files in the jpush integration SDK, as shown in the screenshot:




Then we write our build.gradle file.

The configuration of so file is very simple. The code configuration is as follows:

  1. task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {  
  2.         destinationDir file("$projectDir/libs")  
  3.         baseName "Native_Libs2"  
  4.         extension "jar"  
  5.         from fileTree(dir: "libs", include: "**/*.so")  
  6.         into "lib"  
  7.     }  
  8.   
  9.     tasks.withType(JavaCompile) {  
  10.         compileTask -> compileTask.dependsOn(nativeLibsToJar)  
  11.     }  


Customize a task to specify the directory of so files that the project depends on. Here * * / *. So is used to write. In order to save time, specify the directory to be copied into into "lib". Then the dynamic runtime will be copied into the Lib directory.

The complete build.gradle file is as follows:

  1. apply plugin: 'com.android.application'  
  2.   
  3. android {  
  4.     compileSdkVersion 21  
  5.     buildToolsVersion "21.1.0"  
  6.   
  7.     defaultConfig {  
  8.         applicationId "com.wujay.footerballstar"  
  9.         minSdkVersion 8  
  10.         targetSdkVersion 21  
  11.         versionCode 1  
  12.         versionName "1.0"  
  13.     }  
  14.   
  15.     buildTypes {  
  16.         release {  
  17.             runProguard false  
  18.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  19.         }  
  20.     }  
  21.   
  22.     task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {  
  23.         destinationDir file("$projectDir/libs")  
  24.         baseName "Native_Libs2"  
  25.         extension "jar"  
  26.         from fileTree(dir: "libs", include: "**/*.so")  
  27.         into "lib"  
  28.     }  
  29.   
  30.     tasks.withType(JavaCompile) {  
  31.         compileTask -> compileTask.dependsOn(nativeLibsToJar)  
  32.     }  
  33. }  
  34.   
  35. dependencies {  
  36.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  37.     compile 'com.android.support:appcompat-v7:21.0.0'  
  38.     compile files('libs/jpush-sdk-release1.7.2.jar')  
  39.     compile files('libs/umeng_sdk.jar')  
  40.     compile files('libs/libammsdk.jar')  
  41. }  


Based on someone's questions in the comment area, here's a quick way to configure

In the build.gradle configuration, configure the following information:

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

Your so package can be placed in the same directory as the jar package, such as lib/armeabi/libjpush172.so.


Topics: Android Gradle SDK