Android Native Dependencies Resolver Gradle Plugin
As the official Android Gradle plugin cannot resolve the native dependencies, the .so files would not be copied to the sub-directory of jniLibs
. This plugin is aim to solve this problem, in addition to provide file rename and abi filtering utility functions.
Also if you are building Android projects which depends native libraries with Maven and android-maven-plugin, this plugin helps migration from Maven to Gradle easily.
Usage
1. Introduce and apply the plugin
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.linsea:native-dependencies-plugin:0.2.1"
}
}
//Apply the plugin in the build.gradle of *APP* module, this is encouraged, or you may encounter this bug: //https://code.google.com/p/android/issues/detail?id=158630 apply plugin: "com.github.linsea.native-dependencies"
2. declare Native libraries dependencies
in the dependencies
block, declare Native libraries dependencies, but add classifier and ext, for example:
dependencies {
compile "org.ffmpeg:algorithm:1.2.3:armeabi-v7a@so"
}
If you are a maven user, you should understand that this corresponds to the following statement in maven:
<dependency>
<groupId>org.ffmpeg</groupId>
<artifactId>algorithm</artifactId>
<version>1.2.3</version>
<classifier>armeabi-v7a</classifier>
<type>so</type> </dependency>
3. configure .so file filtering and renaming rules
3.1 soFilters
Add a nativeso
block in the build.gradle
of the app module, the block contains a soFilters
attribute, according these values, the plugin finds the .so files about that ABI and copies them to subdirectory of jniLibs
(default value is src/main/jniLibs), thus the .so file name in the remote repository should contains the ABI value keyword, otherwise the plugin cannot handle them.
3.2 renaming
The plugin provides a renaming chance while copying .so files, renames files based on a regular expression, you can define some attributes in the renaming block:
prefix
: optional, defines the prefix of the file name after renaming.
regex
: mandatory, defines the file name pattern which would be renamed, uses java.util.regex type of regular expressions.
replaceAll
: defines the file name replacement when the file name matches the regex
, i.e. call String.replaceAll(regex,replaceAll) on filename.
replaceWith
: replacement string (use $ syntax for capture groups in the source regular expression). details reference Gradle doc: Copy task,for example:
regex = '(.*)_OEM_BLUE_(.*)' replaceWith = '$1$2'
NOTE
You should define replaceAll
or replaceWith
, one of them, if both, replaceWith
will be ignored.
The file name try to match the renaming
rule from top to below, if one hit, skip others. If none hit, files will not be renamed.
Here is a nativeso
block configuration example:
nativeso {
//.so filename MUST contains 'armeabi-v7a' or 'x86' to identify abi types
soFilters = ['armeabi-v7a']
//['armeabi-v7a','x86'] //rename .so file: textsearch-1.2.3-armeabi-v7a.so -> libtextsearch.so
renaming {
prefix = 'lib'
regex = 'textsearch-1.2.3-armeabi-v7a.*'
replaceAll = 'textsearch' //not include ext .so
}
//rename .so file: libhello-1.4.10-armeabi-v7a.so -> libhello.so
renaming {
regex = 'libhello(.*)'
replaceWith = 'libhello'
}
//rename .so file: libmysdk2-v7.0-armeabi-v7a.so -> libmysdk2.so
renaming {
regex = 'libmysdk(\\d+)-v(.*)'
replaceWith = 'liblocSDK$1'
}
//default renaming rule: name-version-armeabi-v7a.so -> libname.so
renaming {
prefix = 'lib'
regex = '(.*)-([\\d\\.]+)-(.*)'
replaceWith = '$1'
}
}
Notes
The plugin make use of Gradle incremental build feature, it would not execute if the .so files are up to date, but if the files are out of date, the plugin would delete jniLibs
directory, this cause problem if your APP module has already contains pre-build .so files in jniLibs
directory(default:src/main/jniLibs), to avoid this problem, you should add a dedicated directory for the plugin, below snippet can achieve this:
android {
sourceSets {
main {
//...
jniLibs.srcDirs += 'src/main/nativeso' //MUST add to the last of the array
}
}
Tips
1.The plugin create a Task named collectso
, if you want to debug the configuration, run below command:
./gradlew -q --rerun-tasks collectso --info
2.commonly your APP support limited ABI, others ABI .so files should be excluded from APK while packaging, in android plugin 2.2 and above, below configuration achieve desired effect:
android {
packagingOptions {
//assume your APP support armeabi-v7a only
exclude '/lib/armeabi/**'
exclude '/lib/arm64-v8a/**'
exclude '/lib/x86/**'
exclude '/lib/x86_64/**'
exclude '/lib/mips/**'
exclude '/lib/mips64/**'
}
}
License
Copyright 2015 Android Native Dependencies Resolver Gradle Plugin author Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
?????
Android ???????Gradle??
?????Android Gradle??????? dependencies
????.so???,????????.so??????? jniLibs
???,?????????????????,????so??????abi???????.
????????maven? android-maven-plugin ??Android??,???????native????,????????Gradle??????,??????????.
????
1. ????
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.linsea:native-dependencies-plugin:0.2.1"
}
}
//?*???*???(??APP???)????, //??????Android????????BUG:https://code.google.com/p/android/issues/detail?id=158630 apply plugin: "com.github.linsea.native-dependencies"
2. ??Native???
Native??????????????? dependencies
??,????classifier?ext,??:
dependencies {
compile "org.ffmpeg:algorithm:1.2.3:armeabi-v7a@so"
}
???????maven,???????maven???????:
<dependency>
<groupId>org.ffmpeg</groupId>
<artifactId>algorithm</artifactId>
<version>1.2.3</version>
<classifier>armeabi-v7a</classifier>
<type>so</type> </dependency>
3. ??so???????????
3.1 soFilters
?????build.gradle??????? nativeso
?,??????? soFilters
??,?????????????, ????????so?????????????,???????????so???? jniLibs
????????. ????so??????????????????,???? armeabi-v7a
?????so??,????????? ????? armeabi-v7a
???,????????so????????????,??????.
3.2 renaming
??Linux???so???????????,???lib??,??????????????????????,?? ????????????,Android?????so??.??APP??????,????????,???????so? ??? jniLibs
??Android??????,?????????so??.
** renaming
**?????????,?? renaming
???????????so?????, regex
???????? ????so???,?????????,?so????????????? renaming
??. ???????? replaceAll
?? replaceWith
???,???????, ? replaceAll
????? replaceWith
. ? replaceAll
??????,?????so filename??? String.replaceAll(regex,replaceAll). ?? replaceWith
??????,????????????($+????)??,??????,????????????. ??????Gradle??? Copy????.??:
regex = '(.*)_OEM_BLUE_(.*)' replaceWith = '$1$2'
prefix
??so???????????,????lib?????????.
??
??????,???????????,?????????,?????????????,??????????????,????????.
????? nativeso
????????:
nativeso {
//.so filename MUST contains 'armeabi-v7a' or 'x86' to identify abi types
soFilters = ['armeabi-v7a']
//['armeabi-v7a','x86'] //rename .so file: textsearch-1.2.3-armeabi-v7a.so -> libtextsearch.so
renaming {
prefix = 'lib'
regex = 'textsearch-1.2.3-armeabi-v7a.*'
replaceAll = 'textsearch' //not include ext .so
}
//rename .so file: libhello-1.4.10-armeabi-v7a.so -> libhello.so
renaming {
regex = 'libhello(.*)'
replaceWith = 'libhello'
}
//rename .so file: libmysdk2-v7.0-armeabi-v7a.so -> libmysdk2.so
renaming {
regex = 'libmysdk(\\d+)-v(.*)'
replaceWith = 'liblocSDK$1'
}
//??????: name-version-armeabi-v7a.so -> libname.so
renaming {
prefix = 'lib'
regex = '(.*)-([\\d\\.]+)-(.*)'
replaceWith = '$1'
}
}
????
????so?????jniLibs???????,??jniLibs/armeabi-v7a,?????Gradle???????,??????,???????????,?????????,?????,?????jniLibs??.??????????????????native??,??.so??????jniLibs???????,??????????so?????.??????????,???????? ?jniLibs???????,??:
android {
sourceSets {
main {
//...
jniLibs.srcDirs += 'src/main/nativeso' //??????????
}
}
????
1.????????? collectso
?Task,?????gradle???????,???????,Task??????.????????????,????????:
./gradlew -q --rerun-tasks collectso --info
??????????????????,???????????.
2.??,????APP????????ABI,?????????ABI?so?????APK??????,???????????? ??ABI????so,?android plugin 2.2?????????????????:
android {
packagingOptions {
//????APP???armeabi-v7a??,?????????so?????
exclude '/lib/armeabi/**'
exclude '/lib/arm64-v8a/**'
exclude '/lib/x86/**'
exclude '/lib/x86_64/**'
exclude '/lib/mips/**'
exclude '/lib/mips64/**'
}
}