如何在Android.mk中使用gradle变量? [英] How to use the gradle variables in Android.mk?

查看:325
本文介绍了如何在Android.mk中使用gradle变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,我想知道如何使用Android.mk文件中gradle定义的变量,或者是否有将gradle中的某些变量传递给Android.mk的方法?

Please, I would like to know how to use a variable defined from the gradle in the Android.mk file or is there anyway to pass some variable from gradle to Android.mk?

我知道这是一个看起来很奇怪的问题,但是我尝试了很多可能性,但没有成功.

I know this is a question that seems a weird, but I tried a lot of possibity and it did not work.

实际上,我正在与同学一起进行协作项目,我们使用OpenCV和其他类型的库.问题在于,这些库中的大多数库的路径随开发人员的计算机而异.因此,由于我们使用的是git,因此这些文件中始终存在冲突.

Indeed, I'm working on a collaborative project with classmates and we use OpenCV and other type of library. The problem is that most of these libraries have paths that vary according to developer's computer. So, since we're using git, there are constant conflicts in these files.

要解决该问题,我首先使用文件/etc/profile.d/my_proj.sh中的环境变量,但该方法不起作用.因此,我创建了一个属性文件,在其中放置了所有这些路径,并编写了一个函数来读取文件并在build.gradle文件中获取这些值.现在,我想知道如何与Android.mk文件共享这些变量.

To solve the problem, I first use the environment variables in the file /etc/profile.d/my_proj.sh, and it did not work. So I created a properties file in which I put all these paths and I wrote a function to read the file and get those values in the build.gradle file. Now I would like to know how to share those variable with the Android.mk file.

我在这里与您分享我的代码. build.gradle中的功能.

Here I'm sharing my code with you. The function in build.gradle.

apply plugin: 'com.android.library'
import org.apache.tools.ant.taskdefs.condition.Os

def Properties props = new Properties()
def propFile = file('/etc/profile.d/my_proj.properties')   //pay attention to the path
def opencvRoot;
if (propFile.canRead()){
    props.load(new FileInputStream(propFile))
    println(props)

    if (props!=null && props.containsKey('OPENCVROOT')) {
        opencvRoot = props['OPENCVROOT']
        println("opencvRoot = ${opencvRoot}")
    }else{
        println("Doesn't contains opencvRoot = ${opencvRoot}")
    }
}else{
    println("I can't read opencvRoot = ${opencvRoot}")
}

android {
    compileSdkVersion versionCompiler
    buildToolsVersion versionBuildTool

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion versionTarget
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            ndkBuild {
                arguments "OPENCV_ROOT:=${opencvRoot}"
            }
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            jniLibs.srcDir 'src/main/libs'
            jni.srcDirs = []
        }
    }
}

Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT := $(OPENCV_ROOT) # This doesn't work
# OPENCVROOT:= /home/username/OpenCV-android-sdk

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

我将非常感谢您的帮助!

I would really appreciate any help !

推荐答案

/etc/profile.d/my_proj.sh中进行设置应该可以.您是否尝试注销并登录?

Setting it in /etc/profile.d/my_proj.sh should work. Did you try to logout and login?

我正在~/.profile中设置我的OPENCVROOT,它在Android.mk内部对于gradle和ndk-build是可见的. 只需记住即可在~/.bashrc中进行设置,该设置仅用于终端子外壳,而不是从Android Studio通常从GUI启动的程序.

I am setting my OPENCVROOT in ~/.profile and it is visible inside Android.mk for gradle and ndk-build. Just remember not to set it in ~/.bashrc which is only for terminal subshells not programs started from GUI which Android Studio often is.

这篇关于如何在Android.mk中使用gradle变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆