簡體   English   中英

NDK支持不同的產品風味

[英]NDK support with different Product Flavour

我想從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"
        }
    }
}

我在demo文件夾和主文件夾中粘貼了相同的cpp文件,但可以完成我的任務。 任何幫助將是欣賞這是一些參考鏈接

https://developer.android.com/studio/projects/gradle-external-native-builds.html

如何為每個Android ABI設置產品風味中的CmakeLists路徑?

也許,在編譯時實現目標的最小代碼是為每個flavor設置cppFLags

productFlavors {
  demo {
    applicationId "com.android.demo"
    versionName "2.1"
    dimension "default"

    externalNativeBuild.cmake {
      cppFlags '-DDEMO'
    }
  }
  live {
     applicationId "com.android.live"
     versionName "2.1"
     dimension "default"
    externalNativeBuild.cmake {
      cppFlags '-DLIVE'
    }
  }
}

並在您的cpp文件中

#ifdef DEMO
  std::string hello = "hello I am from demo";
#endif
#ifdef LIVE
  std::string hello = "hello I am from live";
#endif

或者您可以在此答案中使用stingify模式。

當然,您的條件編譯不僅限於字符串變體。

最后我解決了。 這是我的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);
}

現在我可以根據所選的味道得到我想要的任何文字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM