3

I cannot get an Android Wear project to use C++. I am able to get a "Phone and Tablet" project to use C++. Here is what I have done.

  1. Here is an image of the SDK Tools I have installed. I also have SDK API Levels 24-27 installed.

    SDK Tools

  2. I create a new project. I check "Include C++ support". I check "Wear" (API 26: Android 8.0 (Oreo)). I select "Next" a bunch.

  3. For C++ Standard I have tried all three (Toolchain Default, C++11, c++14).

    • I do not check -fexceptions or -frtti.

In the project that is created under the "mobile" Module, I have a "cpp" folder, but I do not have one under the "wear" Module.

If I create a project without "Phone and Tablet" support ("mobile" Module), then I still do not get a "cpp" folder under the "wear" Module.

I tried forcing the project to use C++ under the "wear" Module. Here is what I did.

  1. I modified build.gradle (Module: wear) to look like this

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.example.xorgaming.watchtestcpp"
            minSdkVersion 25
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    cppFlags "-std=c++11"
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.google.android.support:wearable:2.2.0'
        implementation 'com.google.android.gms:play-services-wearable:11.8.0'
        implementation 'com.android.support:percent:26.1.0'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation 'com.android.support:recyclerview-v7:26.1.0'
        implementation 'com.android.support:wear:26.1.0'
        compileOnly 'com.google.android.wearable:wearable:2.2.0'
    }
    
  2. I restart the project which makes a "cpp" folder under the "wear" module.

  3. I create a native-lib.cpp file in the "cpp" folder. It looks like this:

    extern "C"
    JNIEXPORT jstring JNICALL Java_com_example_xorgaming_watchtestcpp_MainActivity_stringFromJNIWatch(
        JNIEnv *env, jobject /* this */)
    {
        std::string hello = "Hello from C++ WATCH!";
        return env->NewStringUTF(hello.c_str());
    }
    
  4. In my java onCreate() function, I call: stringFromJNIWatch().

  5. Everything builds without error (green hammer)

  6. When I run the project (as a wearable device) I get this error:

    No implementation found for java.lang.String com.example.xorgaming.watchtestcpp.MainActivity.stringFromJNIWatch()

Any idea what I'm doing wrong? Does Android Wear support C++?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
xorgaming
  • 31
  • 2
  • Check out this [SO post](https://stackoverflow.com/questions/28906516/is-c-suitable-for-android-development), `For Android development, Java is just the natural, normal, default language, and C++ is for exotic special tasks, typically those which involve really intensive calculations. You use it when you need it, not because you don't "want to" write in Java or because "Java is slow".` It is also suggested to use [Android NDK](https://developer.android.com/tools/sdk/ndk/index.html) but not a good idea. – MαπμQμαπkγVπ.0 Feb 27 '18 at 11:09
  • That is helpful. I am trying to use C++ because I am porting code between multiple platforms and do not want to re-write the whole thing for each platform. Does Android wear support C++? – xorgaming Feb 27 '18 at 17:06
  • @xorgaming Java would be a better option than C++ if you want to write cross platform code. – Will_Panda Feb 28 '18 at 06:07
  • Maybe that is true. The question still stands: Does Android Wear support C++, and if so what is incorrect about my setup? – xorgaming Feb 28 '18 at 17:54

1 Answers1

0

Things have changed quite a bit since this question was posted. But half of the answer is: Yes, you can use C++ and Java together with Wear OS. You can follow the same instructions for Android applications.

As for why this failed, my suggestion would be double-check the paths. If you follow the instructions here you will note that the cpp directory goes in the same directory with the java directory. Typically something like MyApplication/app/src/main will have a java directory and a cpp directory. That helps everything get wrapped up into the eventual .apk

John Jones
  • 43
  • 7