安卓编译环境设置

Posted stalendp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓编译环境设置相关的知识,希望对你有一定的参考价值。

android 
    compileSdkVersion 23
    buildToolsVersion "27.0.2"

    defaultConfig 
        applicationId "com.ucool.heroesarena"
        minSdkVersion 16
        targetSdkVersion 19

        ndk 
            moduleName "hcu3dplugin"
        
    

    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        
    
    externalNativeBuild 
        ndkBuild 
            path '../../MOBA_ANDROID/AndroidBuild/HeroesArena/jni/Android.mk'
        
    
    splits 
        abi 
            // Configures multiple APKs based on screen density.
            enable true
            reset()  // Clears the default list from all densities to no densities.
            include "armeabi-v7a", "x86" // Specifies the two ABIs we want to generate APKs for.
        
    

NDK相关的编译

官方文档:Standalone Toolchains

Cross Compiling C/C++ for Android

生成tool-chain环境(由于android编译的target是丰富各异的安卓硬件平台,所以ndk的编译也需要根据平台来生成编译环境,所以提供了一个命令,如下是生成arm64平台的):

./android-ndk-r12b/build/tools/make_standalone_toolchain.py --arch arm64 --install-dir ~/arm
编译的时候,可以使用如下命令:
~/arm/bin/clang -pie hello_world.c
查看编译文件的信息:
file a.out
在手机平台上运行:
adb push a.out /data/local/tmp/.
adb shell "./data/local/tmp/a.out"

#include <stdio.h>
int main () 
  puts("hello world");

一个简单的NDK的例子

TestCurl.cpp

#include <jni.h>
extern "C" 
    JNIEXPORT jstring JNICALL
    Java_com_ucool_hellondk_MyNDK_stringFromJNI( JNIEnv* env,
                                                 jobject thiz ) 
        return (*env)->NewStringUTF(env, "Hello from JNI ! ");
    

MyNDK.java

package com.ucool.hellondk;

public class MyNDK 
    static 
        System.loadLibrary("hello-jni");
    
    public static native String  stringFromJNI();

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

add_library(hello-jni SHARED
            TestCurl.cpp)

# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
                      android
                      log)

模块的build.gradle

android 
    compileSdkVersion 26
    // .....    
    externalNativeBuild 
        cmake 
            path 'src/main/jni/CMakeLists.txt'
        

//        ndkBuild 
//            path 'src/main/jni/Android.mk'
//        
    

安卓的ToolChain环境的准备

# Create an arm64 API 26 libc++ toolchain.
$NDK/build/tools/make_standalone_toolchain.py --arch arm64  --stl=libc++   --install-dir=my-toolchain --force

or
%NDK%/build/tools/make-standalone-toolchain.sh --arch=arm64  --stl=libc++ --install-dir=my-toolchain  --force


# Add the standalone toolchain to the search path.
export PATH=$PATH:`pwd`/my-toolchain/bin

# Tell configure what tools to use.
target_host=aarch64-linux-android
export AR=$target_host-ar
export AS=$target_host-clang
export CC=$target_host-clang
export CXX=$target_host-clang++
export LD=$target_host-ld
export STRIP=$target_host-strip

# Tell configure what flags Android requires.
export CFLAGS="-fPIE -fPIC"
export LDFLAGS="-pie"


---------------------------------------------------

$ ./configure --host=arm
$ ./configure --host=arm --build=arm --with-sysroot=$ANDROID_SYSROOT

CMake的一个例子:

cmake_minimum_required(VERSION 3.4.1)

# 设置编译参数
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -DUSE_FILE32API")

# 设置变量
set(BROTLI_DIR          "$CMAKE_CURRENT_SOURCE_DIR/third_party/Brotli")
set(LIB_CURL_DIR        "$CMAKE_CURRENT_SOURCE_DIR/prebuilt/libcurl")
set(H_CURL_DIR      "$LIB_CURL_DIR/include")

# 头文件相关
include_directories("." $H_CURL_DIR )

# 编译源文件成Library
add_library(hcu3dplugin SHARED
            HcU3dPlugin.cpp
            JniHelper.cpp
            main.cpp
         )

# 编译子文件夹
add_subdirectory($BROTLI_DIR )

# 添加一个编译好的library
add_library(curl    STATIC IMPORTED )
set_target_properties(curl
                        PROPERTIES IMPORTED_LOCATION
                        $LIB_CURL_DIR/libs/$ANDROID_ABI/libcurl.a )

# 连接其他文件
target_link_libraries(hcu3dplugin
                      brotli
                      curl
                     
                      android
                      log
                      z
                      )

CMake参考:

https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html

以上是关于安卓编译环境设置的主要内容,如果未能解决你的问题,请参考以下文章

Mac环境下反编译安卓apk

jenkins+gitlab构建安卓自动编译环境

安卓交叉编译环境配置

自己可以编译安卓源码吗?

cocos2dx 2.2.3编译第一个apk到安卓手机

[编译] 7在Linux下搭建安卓APP的开发烧写环境(makefile版-gradle版)—— 在Linux上用命令行+VIM开发安卓APP