Wednesday, April 20, 2016

Android Studio 2.0 to 2.2 (updated) Release

http://android-developers.blogspot.com/2016/04/android-studio-2-0.html
Java SE download -> http://www.oracle.com/technetwork/java/javase/downloads/index.html

Update on the new build.gradle dependencies classes com.android.tools.build:gradle:2.0.0 and the experimental com.android.tools.build:gradle-experimental:0.7.0-beta1 in Android Studio 2.0
for the C++ ndk static library build (native_app_glue) with multiple target cpu architectures

The original HelloDroid project file for Android 1.3 is here
or from https://mega.nz/#!iwoHjDiC!dJ45TLYAmCoUYnSOE9H1faYYOSyt5FxfBnmlXeqIRgU

Modifications to the following files are needed for Android Studio 2.0

app/build.gradle    Select all
apply plugin: 'com.android.model.application' Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def ndkFolder = properties.getProperty('ndk.dir') def projectFolder = file(project.projectDir).absolutePath model { android { compileSdkVersion = 23 buildToolsVersion = '23.0.2' defaultConfig.with { applicationId 'com.beginndkgamecode.hellodroid' minSdkVersion.apiLevel = 9 targetSdkVersion.apiLevel = 23 versionCode = 1 versionName = '1.0' archivesBaseName = 'HELLODRIOD_' + versionName } } /* * native build settings */ android.ndk { moduleName = 'hellodroid' abiFilters.addAll(['armeabi-v7a','x86_64', 'x86']) // cppFlags.addAll(["-I${file("${ndkFolder}/sources/android/native_app_glue")}".toString()]) ldLibs.addAll(['EGL', 'GLESv2', 'android', 'log']) } /* * dependency library */ repositories { libs(PrebuiltLibraries) { native_app_glue { headers.srcDir ndkFolder+"/sources/android/native_app_glue" binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("libs/local/${targetPlatform.getName()}/libandroid_native_app_glue.a") } } } } // jni is the default dir; config this if yours is in different directory android.sources { main { jni { source { srcDirs 'src/main/jni' } dependencies { library 'native_app_glue' linkage 'static' } } } } android.buildTypes { debug { minifyEnabled = false zipAlignEnabled = true ndk.with { debuggable = true } useProguard = false } release { minifyEnabled = false zipAlignEnabled = true useProguard = true proguardFiles.add(file('proguard-rules.txt')) } } android.productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa create('armeabi-v7a') { ndk.abiFilters.add('armeabi-v7a') } create('x86_64') { ndk.abiFilters.add('x86_64') } create('x86') { ndk.abiFilters.add('x86') } // To include all cpu architectures, leaves abiFilters empty create('all') } } /* * task to build static library files using ndk-build.cmd (for windows OS), ndk-build (for Mac) */ task staticLibBuild(type: Exec, description: 'Build Static Library files') { commandLine ndkFolder + '/ndk-build.cmd', 'APP_BUILD_SCRIPT='+ndkFolder+'/sources/android/native_app_glue/Android.mk', 'NDK_APPLICATION_MK='+file(projectDir).absolutePath+'/src/main/jni/Application.mk', 'NDK_PROJECT_PATH='+ndkFolder, 'NDK_OUT='+file(projectDir).absolutePath+'/libs' } tasks.withType(CppCompile) { compileTask -> compileTask.dependsOn staticLibBuild } task cleanLibDir(type: Delete) { delete 'libs' } clean.dependsOn cleanLibDir

app/src/main/jni/Application.mk    Select all
APP_PLATFORM := android-9 APP_ABI := armeabi-v7a x86 x86_64

Project:HelloDroid/build.gradle    Select all
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.7.0-beta1' classpath 'com.android.tools.build:gradle:2.0.0' } } allprojects { repositories { jcenter() } }






HelloBoost, demo of using boost c++ libraries in Android Studio 2.0
a) Download the Android project from https://github.com/donbright/android_hello_boost
b) Download the prebuilt boost libraries for Android from https://github.com/emileb/Boost-for-Android-Prebuilt/ and extract to folder under sdkFolder (see local.properties)
c) Use Android Studio 2.0 menu function to Import Project... from android_hello_boost project and will create new project structure for Android Studio 2.0
d) Modify the app/build.gradle as below
app/build.gradle    Select all
apply plugin: 'com.android.model.application' Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def sdkFolder = properties.getProperty('sdk.dir') model { android { compileSdkVersion = 23 buildToolsVersion = '23.0.3' defaultConfig { applicationId = 'com.example.helloboost' minSdkVersion.apiLevel = 4 targetSdkVersion.apiLevel = 23 versionCode = 1 versionName = '1.0' archivesBaseName = 'HELLOBOOST_' + versionName } /* * native build settings */ ndk { moduleName = 'hello-boost' abiFilters.addAll(['armeabi','armeabi-v7a', 'x86']) cppFlags.addAll(['-Wall', '-fexceptions', '-frtti']) stl = 'gnustl_static' } buildTypes { debug { minifyEnabled = false zipAlignEnabled = true ndk.with { debuggable = true } useProguard = false } release { minifyEnabled = false zipAlignEnabled = true useProguard = true proguardFiles.add(file('proguard-rules.txt')) } } productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa create("arm") { ndk.abiFilters.add("armeabi") } create("arm7") { ndk.abiFilters.add("armeabi-v7a") } create("x86") { ndk.abiFilters.add("x86") } // To include all cpu architectures, leaves abiFilters empty create("all") } } // jni is the default dir; config this if yours is in different directory android.sources { main { jni { source { srcDirs 'src/main/jni' } dependencies { library 'boost_program_options' linkage 'static' library 'boost_thread' linkage 'static' } } } } // PrebuiltLibraries repositories { libs(PrebuiltLibraries) { boost_program_options { headers.srcDir sdkFolder+'/Boost-for-Android-Prebuilt-master/boost_1_53_0/include' binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("${sdkFolder}/Boost-for-Android-Prebuilt-master/boost_1_53_0/${targetPlatform.getName()}/lib/libboost_program_options-gcc-mt-1_53.a") } } boost_thread { headers.srcDir sdkFolder+'/Boost-for-Android-Prebuilt-master/boost_1_53_0/include' binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("${sdkFolder}/Boost-for-Android-Prebuilt-master/boost_1_53_0/${targetPlatform.getName()}/lib/libboost_thread-gcc-mt-1_53.a") } } } } }

Project:android_hello_boost-master/build.gradle    Select all
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.7.0-beta1' } } allprojects { repositories { jcenter() } }



ATTENTION: Android Studio 2.2 supports NDK build


Android Studio 2.2.2 HelloDroid External Build Files using Android.mk & Application.mk
app/build.gradle    Select all
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '23.0.3' defaultConfig { applicationId 'com.beginndkgamecode.hellodroid' minSdkVersion 9 targetSdkVersion 23 versionCode 1 versionName "1.0" archivesBaseName = 'HELLODRIOD_' + versionName ndk { abiFilters 'armeabi-v7a', 'x86', 'x86_64' } externalNativeBuild { ndkBuild { arguments "NDK_APPLICATION_MK=src/main/jni/Application.mk" } } } buildTypes { debug { minifyEnabled false zipAlignEnabled true debuggable true useProguard false } release { minifyEnabled false zipAlignEnabled true useProguard true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { ndkBuild { path "src/main/jni/Android.mk" } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support.constraint:constraint-layout:1.0.0-beta2' }


Project:HelloDroid/build.gradle    Select all
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { //classpath 'com.android.tools.build:gradle-experimental:0.7.0-beta1' //classpath 'com.android.tools.build:gradle:2.2.0-alpha3' classpath 'com.android.tools.build:gradle:2.2.2' } } allprojects { repositories { jcenter() } }



gradle-wrapper.properties    Select all
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists #distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip





Android Studio 2.2.2 HelloBoost External Build Files using Android.mk & Application.mk

HelloBoost, demo of using boost c++ libraries in Android Studio 2.2.2
After the download, import and setup as HelloBoost for Android Studio 2.0 above, modify build.gradle, Android.mk and Application.mk as below

app/build.gradle    Select all
apply plugin: 'com.android.application' Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def sdkFolder = properties.getProperty('sdk.dir') def ndkFolder = properties.getProperty('ndk.dir') android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.example.helloboost" minSdkVersion 3 targetSdkVersion 23 versionCode = 1 versionName = '1.0' archivesBaseName = 'HELLOBOOST_' + versionName ndk { moduleName "hello-boost" abiFilters 'armeabi','armeabi-v7a', 'x86' } externalNativeBuild { ndkBuild { arguments "NDK_APPLICATION_MK=src/main/jni/Application.mk", "NDK_MODULE_PATH=${sdkFolder}/Boost-for-Android-Prebuilt" // default module search path is at ${ndkFolder}/sources } } } buildTypes { debug { minifyEnabled false zipAlignEnabled true debuggable true useProguard false } release { minifyEnabled false zipAlignEnabled true useProguard true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } externalNativeBuild { ndkBuild { path "src/main/jni/Android.mk" } } }



app/src/main/jni/Android.mk    Select all
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-boost LOCAL_SRC_FILES := hello-boost.cpp first.cpp LOCAL_STATIC_LIBRARIES := libboost_thread libboost_program_options include $(BUILD_SHARED_LIBRARY) $(call import-module, boost_1_53_0)



app/src/main/jni/Application.mk    Select all
APP_STL = gnustl_static APP_CPPFLAGS = -Wall -fexceptions -frtti APP_CPPFLAGS += -I$(NDK_MODULE_PATH)/boost_1_53_0/include #It is recommended to add these two variables for each LOCAL_MODULE in boost_1_53_0/Android.mk rather than modifying the Application.mk for the app #LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include #LOCAL_CPP_FEATURES := rtti exceptions



boost_1_53_0/Android.mk    Select all
... include $(CLEAR_VARS) LOCAL_MODULE := libboost_program_options LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/lib/libboost_program_options-gcc-mt-1_53.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) ... include $(CLEAR_VARS) LOCAL_MODULE := libboost_thread LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/lib/libboost_thread-gcc-mt-1_53.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) ...



HelloJNI, sample of CMakeLists.txt after adding Boost libraries
Assume boost prebuilt libraries are installed to ~/Library/Android/sdk/ndk-bundle/sources/Boost-for-Android-Prebuilt
CMakeLists.txt    Select all
cmake_minimum_required(VERSION 3.4.1) # build native_app_glue as a static lib add_library(app-glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c) # now build app's shared lib set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") add_library(hello-jni SHARED hello-jni.c) #BOOST set(Boost_INCLUDE_DIR ${ANDROID_NDK}/sources/Boost-for-Android-Prebuilt/boost_1_53_0/include) set(Boost_LIBRARY_DIR ${ANDROID_NDK}/sources/Boost-for-Android-Prebuilt/boost_1_53_0/${ANDROID_ABI}/lib) set(Boost_USE_STATIC_LIBS ON) # only find static libs set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_COMPILER -gcc) find_package(Boost COMPONENTS thread program_options REQUIRED) #BOOSTEND if(Boost_FOUND) # Include libraries needed include_directories(${Boost_INCLUDE_DIR}) target_link_libraries(hello-jni android app-glue log ${Boost_LIBRARIES}) endif()



No comments: