Flutter: Displaying debug info

To send info to the debug console add the following import: –

import 'dart:developer' as debug;

…then, in your code, add debug output using: –

debug.log("This is debug output");

Useful Flutter Commands

Quick list of Flutter commands. I’ll keep adding to this as I think of them…

flutter doctor

This does a health check on your flutter installation and checks that everything is set up as it’s supposed to be. It’ll flag problems and will usually give instructions on how to solve those problems

If you want to find more detailed info (including installation paths) then add -v to get the verbose version

flutter devices

Checks for connected flutter capable devices (targets) and displays information on them. Will look something like this…

flutter doctor --android-licenses

This will agree to Android licenses. Often handy when flutter doctor points out that you haven’t agreed to them

flutter clean

Cleans the project and removes any content adding during a build. Handy for when you’re checking in and don’t want to additional files

flutter upgrade

upgrade flutter to the latest version

flutter build web

Build content to your local web directory to allow pushing to a website

flutter pub get

Will update the local dependencies to match the pubspec.yaml file

flutter build appbundle

This will build an aar for Android (assuming you’ve set everything up correctly)

ADB Quick Commands

I keep looking these up, so dropping them here so I don’t have to: –

This will echo all output from a Unity project; adb logcat Unity:V *:S

This will echo all output from a Unity project to a file; adb logcat Unity:V *:S -d >> output.txt

This will clear the log; adb logcat -c

This will increase the buffer size so you have more than a few seconds before your logs start to disappear; adb logcat -G 16M

Using C++ within an Android Java app

Just putting this here so I have it when I need it. I haven’t cleaned this up so it’s easy to understand, but there’s not a lot to it. Just need to have the pieces below and you’ve got everything you need to quickly and easily bold cpp into an Android Java app.

Handy links I used to figure it out: –

here

and here

and maybe here too

Prereqs: Android Studio 3.4.1, NDK version 16.1 (but I doubt it matters which NDK)

Files you’ll need: –

DeltaJNI.cpp: Simple cpp file with a method that returns a String to Java. Must be in a cpp directory

#include <jni.h>        // JNI header provided by JDK
#include <stdio.h>      // C Standard IO Header
#include <iostream>
#include "DeltaJNI.h"      // Generated
#include <string>

// Implementation of the native method sayHello()
JNIEXPORT jstring JNICALL Java_com_ancientcoder_recog_MainActivity_sayHello(JNIEnv *env, jobject thisObj) {

    std::string hello = "-------------------------------- Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

 

DeltaJNI.h: Header file to go with the above cpp. Must be with the cpp file above in a cpp directory (see CMakeLists.txt for where I put it)

#include <jni.h>

#ifndef RECOG_DELTAJNI_H
#define RECOG_DELTAJNI_H

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_com_ancientcoder_recog_MainActivity_sayHello(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif

#endif //RECOG_DELTA_H

 

CMakeLists.txt: Added to the app directory to facilitate building of the ‘native-lib’ lib

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
        src/main/cpp/DeltaJNI.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

 

build.gradle (in the ‘app’ directory): Add this at the bottom of (and inside of ) the “android {}” section:

externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

…and this at the bottom of (and inside of) of the “defaultConfig {}” section: –

externalNativeBuild {
    cmake {
        cppFlags ""
    }
}

 

Java: Add this to the java class that’s calling the C++. This loads the lib that’s built by the above CMakeLists.txt (includes the cpp file(s)) and declares a prototype for the sayHello method.

static {
    System.loadLibrary("native-lib"); // Load native library at runtime
    // This library contains a native method called sayHello()
}
private native String sayHello();

 

More Java: …and finally, add this to actually call the cpp from the java code. This will log out the returned string from the cpp code: –

Log.e(TAG, sayHello());

 

 

CUDA Project Import Problem on Visual Studio 2019

I had a CUDA project that was running under Visual Studio 2017 and I moved to Visual Studio 2019, and got this error: –

The imported project “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 10.1.props” was not found

The problem was a missing extension that had been installed as part of the CUDA SDK. To solve it, copy this directory content…

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\visual_studio_integration\MSBuildExtensions

…into this directory: –

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations

Hope it helps!