[英]Conditional build of dependant NDK with gradle
My project depends on a NDK. 我的项目取决于NDK。 But my NDK build is different for my projectFlavors.
但我的NDK版本对于我的projectFlavors来说是不同的。
I like to build and pack my dependent NDK with -DFLAVOR1
compile option defined if app's flavor1
is selected for my app. 如果为我的应用程序选择了应用程序的
flavor1
我喜欢使用-DFLAVOR1
编译选项构建和打包我的依赖NDK。 -DFLAVOR2
when flavor2
is selected and etc. 选择
-DFLAVOR2
时的flavor2
等
My whole app will not work correctly if app is on flavor1
and incorrectly use a NDK built on -DFLAVOR2
, so the correct selection is important. 如果应用程序在
flavor1
并且错误地使用在-DFLAVOR2
上-DFLAVOR2
的NDK,我的整个应用程序将无法正常工作,因此正确的选择很重要。
Now how we can write our build.gradle
to solve this special conditional build? 现在我们如何编写
build.gradle
来解决这个特殊的条件构建?
I finally found a hacky approach for my problem, then I though it is better to share it here for everyone and improvement. 我终于找到了一个针对我的问题的hacky方法,然后我虽然最好在这里为每个人分享并改进。
Step 1: 第1步:
You need to provide flag from app and depend it on preBuild
. 您需要从应用程序提供标志并将其依赖于
preBuild
。 Here is a sample code for doing it.Thanks How to get current flavor in gradle for function. 这是一个用于执行此操作的示例代码。感谢如何在gradle中获取函数的当前flavor 。 I just modified it a little.
我刚修改了一下。
import java.util.regex.Matcher
import java.util.regex.Pattern
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern;
if( tskReqStr.contains( ":app:assemble" ) )
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else if( tskReqStr.contains( ":app:generate" ) )
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("incremental(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher( tskReqStr )
if( matcher.find() )
return matcher.group(1).toLowerCase()
else
{
println "NO MATCH FOUND"
return "";
}
}
task setFlavorFlag() {
def flavorName = getCurrentFlavor();
if (!flavorName.equals("")) {
printf("Setting flag from app...\n")
def f = file("../build/conf.tmp")
if (!f.exists()) f.createNewFile()
f.write("-D${flavorName.toUpperCase()}")
}
}
preBuild.dependsOn setFlavorFlag
dependencies {
compile project(path: ':mylibrary')
}
Step 2: 第2步:
Access flag from library. 从图书馆访问标志。 I did it in
ndk
section: 我在
ndk
部分做了:
ndk {
...
def f = file("../build/conf.tmp")
if (f.exists()) {
printf("Building library for Flavor:%s\n", f.text)
ndk.CFlags.add(f.text);
f.delete()
}
}
ok. 好。 Now your app Flavor is passed with a
-D
option to compile of library. 现在你的app Flavor传递了一个
-D
选项来编译库。 Here was the approach I found after 2-3 days. 这是我在2-3天后发现的方法。 I'm open for all improvements for this approach.
我对这种方法的所有改进持开放态度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.