问题描述
我想从ndk库中获取不同的 字符串 值。
因为我有两个风味演示和现场我想要的价值你好我来自演示的演示风味和现场风味我想要你好我来自现场
I want different string value from ndk library.as i have two flavor demo and live I want value "hello I am from demo " for demo flavor and for live flavor i want "hello I am from live "
这是我的java文件代码
Here is my java file code
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
这是我的cpp文件代码
Here is my cpp file code
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_de_demo_ndk2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "hello I am from demo";
return env->NewStringUTF(hello.c_str());
}
这是我的build.gradle文件
this is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.de.demo.ndk2"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
flavorDimensions "default"
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
demo
{
// applicationId "com.readwhere.whitelabel.test"
//applicationId "com.android.rwstaging"
applicationId "com.android.demo"
versionName "2.1"
dimension "default"
externalNativeBuild {
cmake {
targets "native-lib-demo","my-executible- demo"
}}
}
live
{
// applicationId "com.readwhere.whitelabel.test"
//applicationId "com.android.rwstaging"
applicationId "com.android.live"
versionName "2.1"
dimension "default"
}}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
我在demo文件夹和主文件夹
中粘贴了相同的cpp文件,但可以完成我的任务。任何帮助将是欣赏
这是一些参考链接
I have paste same cpp file in demo folder as well as in main folderbut could achieve my task. Any help would be appreciatethis are some refrence links
推荐答案
最后我是一个解决方案。
这是我的cpp文件代码
Finally I a solution.Here is my cpp file code
Java_com_de_demo_ndk2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject jobject1, jstring jstring1) {
std::string hello;
const char *nativeString1 = env->GetStringUTFChars( jstring1, 0);
if (strcmp(nativeString1, "demo") == 0) {
hello = "Hello from demo C++";
} else if (strcmp(nativeString1, "live") == 0) {
hello = "Hello from live C++";
}
return env->NewStringUTF(hello.c_str());
}
我从java代码传递风味值这里是我的java文件代码。
I am passing flavor value from java code here is my code for java file.
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
String value = BuildConfig.FLAVOR;
String ndkValue = stringFromJNI(value);
tv.setText(ndkValue);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
* @param value
*/
public native String stringFromJNI(String value);
}
现在我可以根据所选的风格获得我想要的任何文字。
Now i can get whatever text i want according to selected flavor.
这篇关于NDK支持不同的产品风味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!