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文件