Android studio 3.3.2 NDK

配置好Android Studio以后
File => New => "New project" => select "Native C++" 填好相关信息

修改build.gradle
jcenter() => maven { url "http://mirrors.rnd.huawei.com/maven" }
或者jcenter{url "http://jcenter.bintray.com"}

即可连接手机运行apk

 

代码说明:

Android 3.0以后NDK必须使用CMake编译

package com.huawei.testjni;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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 = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }
    
    public native String stringFromJNI();
}

 

//native-lib.cpp
#include <jni.h>
#include <string>
#include <android/log.h>

#define LOG_TAG "Test"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

extern "C" JNIEXPORT jstring JNICALL
Java_com_huawei_testjni_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
    LOGD("Native hello world!");
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

#Native  lib
add_library(native-lib  SHARED  native-lib.cpp )

#Add the lib about log
#can be removed if u don't use the log lib
find_library(log-lib  log )
target_link_libraries(native-lib  ${log-lib} )

build.gradle 多了红色部分代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.huawei.testjni"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

 添加三方jar包

 

在libs(libs与src是同级目录)中加入dom4j-2.1.1.jar
点击jar包,右键=>"Add As Library..."

build.gradle中已经自动添加该jar包

dependencies {
    ......
    implementation files('libs/dom4j-2.1.1.jar')
}

同时.idea=>libraries中生成该文件
Gradle____local_aars___D__code_TestJni_app_libs_dom4j_2_1_1_jar_unspecified_jar.xml
(如果不使用该jar包,则可能需手动删除此文件)

即可使用如下三方包
import org.dom4j.dom.DOMComment;

 

posted @ 2019-04-09 18:41  牧 天  阅读(336)  评论(0)    收藏  举报