我想不同的 字符串 值从NDK库。因为我有两个演示和生活我想价值“你好,我是从演示”为演示风味和现场的味道,我想:“我是从活你好”
这是我的java文件代码
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文件代码
#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文件
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"
}
}
}
我有相同的粘贴文件CPP demo文件夹中,以及在主文件夹,但能实现我的任务。任何帮助将欣赏这是一些refrence链接
https://developer.android.com/studio/projects/gradle-external-native-builds.html
最后,我的解决方案。这里是我的cpp文件代码
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文件的代码。
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);
}
现在我可以根据口味选择任何文本,我想。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句