Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)

"Reference C ++ in Android Studio module (Java / kotlin)" https://qiita.com/sanoh/items/484e61729ab69f250fec

Memorandum of procedure to refer to the module created in step 3 from other projects

■ Step1: Create a project

2019-05-30.png Create a project from "File" "New" "New Project". 2019-05-30 (1).png Select Native C ++ and then press Next. 2019-05-30 (2).png This time the name is "test03" Save location specifies the working directory Language specifies "Java" (Kotklin is fine) After completing the settings, click "Next" to go to the next screen. 2019-05-30 (3).png "Finish" completes the project creation.

■ Step2: Refer to modules of other projects

Edit settings.gradle to reference the external module.

settings.gradle


include ':app', ':native-module'
project(':native-module').projectDir = new File(settingsDir, '../test02/test02module')

Please adjust the position of the test02 module you want to refer to in this project If you press "Sync Now" here 2019-05-30 (5).png You can see that it has been added to the project.

Then edit the application's build.gradle (Module: app).

build.gradle


android {
  :
  :
}

dependencies {
  :
  :
    implementation project(':native-module')
}

Now that we have added the native-module, it can be referenced by the application.

■ Step3: Refer from the application

Register in CmakeLists.txt.

python


CmakeList.txt
![2019-05-30 (7).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/122121/556e419a-8a91-2280-f71a-d034063919f0.png)

# 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).
        native-lib.cpp)

include_directories(../../../../../test02/test02module/src/main/cpp)
add_library( native-module
        SHARED
        IMPORTED )
set_target_properties( native-module
        PROPERTIES IMPORTED_LOCATION
        ../../../../../../test02/test02module/src/lib/${ANDROID_ABI}/libnative-module.so)

# 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
        native-module
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

Finally, refer to the class in the module from native-lib.cpp.

native-lib.cpp


#include <jni.h>
#include <string>

#include "subTest.h"

extern "C" JNIEXPORT jstring JNICALL
Java_l_toox_test03_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    subTest ss;
    ss.Hoge(32);
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

Recommended Posts

Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
Refer to C ++ in the Android Studio module (Java / kotlin)
Java language from the perspective of Kotlin and C #
I tried to summarize the basics of kotlin and java
How to derive the last day of the month in Java
Sample code to get the values of major SQL types in Java + Oracle Database 12c
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Get the result of POST in Java
Try to solve Project Euler in Java
The story of forgetting to close a file in Java and failing
How to find the total number of pages when paging in Java
How to get the date in java
Output of the book "Introduction to Java"
How to get the absolute path of a directory running in Java
The story of writing Java in Emacs
Java to C and C to Java in Android Studio
Summarize the life cycle of Java objects to be aware of in Android development
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)
Set the source of the library set as a dependency in IntelliJ as a separate module of the project
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Conditional branching of the result of SQL statement to search only one in Java
Sample code to get the values of major SQL types in Java + MySQL 8.0
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
Introduce Kotlin to your existing Java Maven Project
The story of learning Java in the first programming
Feel the passage of time even in Java
[Java / Kotlin] Resize considering the orientation of the image
[Java] How to get the authority of the folder
How to write Java String # getBytes in Kotlin?
Import files of the same hierarchy in Java
Java Welcome to the Swamp of 2D Arrays
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Summary of how to use the proxy set in IE when connecting with Java
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
[Java] Let's declare variables used in the loop in the loop [Variables in the block]
[Java] How to omit the private constructor in Lombok
CI the architecture of Java / Kotlin applications with ArchUnit
How to write Scala from the perspective of Java
Please note the division (division) of java kotlin Int and Int
Which is better, Kotlin or Java in the future?
Browse class objects in Kotlin (instead of Java class name.class)
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
[Java] Get the file in the jar regardless of the environment
How to set BuildConfig of library project (module) to Debug
Change the storage quality of JPEG images in Java
Summary of how to implement default arguments in Java
Java: Use Stream to sort the contents of the collection
I tried to implement the Euclidean algorithm in Java
Summarize the additional elements of the Optional class in Java 9