Skip to content

Instantly share code, notes, and snippets.

@wuyexiong
Last active December 21, 2015 08:48
Show Gist options
  • Save wuyexiong/6280305 to your computer and use it in GitHub Desktop.
Save wuyexiong/6280305 to your computer and use it in GitHub Desktop.
1.ndk动态库打包 2.多渠道编译
import java.util.regex.Pattern
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':omusic-library')
}
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: '**/*.so' ) into 'build/native-libs'
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn copyNativeLibs
options.encoding = "UTF-8"
}
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir file('build/native-libs')
}
def buildOutputApkName() {
def apkName = "OM_TV_" + android.defaultConfig.versionName + "_" + getChannel("AndroidManifest.xml");
return apkName;
}
android {
compileSdkVersion 16
buildToolsVersion "17.0.0"
defaultConfig {
versionCode 3
versionName "2.0.0"
minSdkVersion 8
targetSdkVersion 17
project.archivesBaseName = buildOutputApkName();
}
signingConfigs {
omusic {
storeFile file("xx")
storePassword "xx"
keyAlias "xx"
keyPassword "xx"
}
}
buildTypes {
release {
runProguard true
proguardFile 'proguard-project.txt'
signingConfig signingConfigs.omusic
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
def getChannel(manifestLocation) {
def manifestFile = file(manifestLocation)
if (!manifestFile.exists()) {
return -1
}
def pattern = Pattern.compile("android:value=\"(.*)\" android:name=\"UMENG_CHANNEL\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def channel = matcher.group(1)
return channel;
}
@cczscq
Copy link

cczscq commented Dec 9, 2013

请问一个问题:有关so库问题
错误日志:specified for property 'jniDir' does not exist(引起这个问题的原因是?)
我的部分build.gradle代码:

task copyNativeLibs(type: Copy) {
from(new File('libs')) { include 'armeabi/*.so' }
into new File(buildDir, 'native-libs')
}

tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')
}

 我感觉 pkgTask.jniDir new File(buildDir, 'native-libs') 与 pkgTask.jniDir file(buildDir, 'native-libs')没有什么区别。

同样一份代码,在我的电脑A上so库没有问题,在另外一台打包电脑B上一直在报上面的错误日志。我对比了两台电脑的
gradlew -v是一致的。电脑A和电脑B都是win7(64)。java和android的环境变量我也统一配置好啦!
但是在电脑B上问题还是存在,纠结了一下午,尝试了各种方案,始终报错,麻烦您,介绍了这个报错日志的可能
原因。

@cczscq
Copy link

cczscq commented Dec 10, 2013

昨天我提出上面的这个问题,最终解决了。方法比较笨,如果其他人也遇到同样的问题,希望能够来你这里。

@cczscq
Copy link

cczscq commented Dec 10, 2013

我的解决方案:
1.理解build.gradle中自定义task的目的。对于so库,我理解的目的就是copy到\build\native-libs\中。至于gradle如何使用\build\native-libs我不是很了解。
2.了解目的后,那么我们就可以采用很多方案解决这个问题(如果build.gradle能够做到那就更好了)。不行的话,手动copy到\build\native-libs\就可以解决这个问题。也可以自己写个python脚本去copy。毕竟我们经常会gradle clean。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment