Android studio 配置(NDK)


1. 配置JDK

2.生成一個項目

 

默認會報錯:

Error:Could not download junit.jar (junit:junit:4.12): No cached version available for offline mode

解決方法:注釋build.gradle中測試相關的內容

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
//        exclude group: 'com.android.support', module: 'support-annotations'
//    })
    compile 'com.android.support:appcompat-v7:25.0.0'
//    testCompile 'junit:junit:4.12'
}

 然后就可以編譯並運行

===================================================================

Android Studio中沒有Workspace這個概念
Project:一個Project,就是一個工作區間,該工程里包括多個Module,該Project類似Eclipse的workspace概念
Module:一個工程,或者是一個應用

Android Studio引入Project和Module的概念,是建議使用者一個工程使用一個Project,該工程里可以包含多個Module,
但是只建議該Project里有一個Application應用,其余的Module是類庫Module,這樣方便工程管理

build.gradle              頂級構建文件
build.gradle(Module:app)  模塊構建文件
gradle-wrapper.properties gradle配置文件
proguard-rules.pro        混淆配置文件
gradle.properties         gradle項目配置文件
settings.gradle           gradle項目設置配置文件
local.properties          sdk、ndk配置文件

 build.gradle (Project)說明

//jcenter和maven一樣也是一種中央庫管理工具
buildscript {
    repositories {
        jcenter()    //存儲庫,這里是jcenter庫,也可以配置maven等
    }
    dependencies {
        //依賴的gradle版本
        classpath 'com.android.tools.build:gradle:2.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {       //所有項目的存儲庫
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (Module)說明

apply plugin: 'com.android.application' //這里配置的插件說明我們是一個Android的項目

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig { //默認配置
        applicationId "com.example.c00390.myapplication"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {  //構建類型
        release {  //正式版配置
            minifyEnabled false   //APK變小壓縮
            //混淆配置文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies { //依賴配置
    compile fileTree(dir: 'libs', include: ['*.jar'])  //依賴二進制文件
    compile 'com.android.support:appcompat-v7:25.0.0'
}

 local.properties

//配置SDK或者NDK路徑
sdk.dir=D\:\\soft\\adt-bundle-windows-x86_64-20140702\\sdk

 

================================================================================

Android studio NDK配置

1. 安裝NDK,安裝Android studio

2.在src/main 目錄下新建jni目錄,並添加c文件daemon_process.c

#include <android/log.h>
#include <jni.h>

#define TAG "Test"

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) //
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) //
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) //
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) //
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) //

JNIEXPORT void JNICALL Java_hisikirim_cn_xxxxx_MainActivity_createDaemon(JNIEnv* env,
        jobject thiz) {
    LOGD("Native createDaemon()");
}

 3. 添加上層調用代碼

package hisikirim.cn.xxxxx;

public class MainActivity extends AppCompatActivity {

    static {
          System.loadLibrary("daemon-jni"); }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createDaemon();
    }

    public native void createDaemon();
}

4.修改build.gradle配置

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "hisikirim.cn.xxxxx"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        ndk {
            moduleName  "daemon-jni"
            ldLibs "log"
            abiFilters "armeabi", "armeabi-v7a", "x86" }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

5.修改local.properties設置NDK路徑

ndk.dir=D\:\\soft\\android-ndk-r10b
編譯生成.so的目錄之一: build\intermediates\ndk\debug\lib\armeabi-v7a\libdaemon-jni.so

 

編譯時遇到的問題

Q1:

Error:Execution failed for task ':xxxxx:compileDebugNdk'.
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.

解決辦法:在gradle.properties中添加如下內容

android.useDeprecatedNdk=true
Q2:

Error:Execution failed for task ':xxxxx:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: Error while executing 'D:\soft\android-ndk-r10b\ndk-build.cmd' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\work\android_stuido_project\Androidpn-client\xxxxx\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-25 NDK_OUT=D:\work\android_stuido_project\Androidpn-client\xxxxx\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=D:\work\android_stuido_project\Androidpn-client\xxxxx\build\intermediates\ndk\debug\lib APP_ABI=armeabi-v7a,armeabi,x86}

當jni目錄只有一個c文件的時候會報這個錯誤

解決辦法:在jni目錄添加一個空的c文件

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM