Ubuntu下AndroidStudio4.0 NDK o-llvm CMake环境搭建

Ubuntu18.04

AndroidStudio4.0.1

NDK 16.1.4479499,CMake3.10.2

下载,编译ollvm4.0

git clone -b llvm-4.0 https://github.com/obfuscator-llvm/obfuscator.git
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_INCLUDE_TESTS=OFF ../obfuscator/
make -j$(nproc)
-mllvm -fla:控制流扁平化
-mllvm -sub:指令替换
-mllvm -bcf:虚假控制流程
有人又进行了移植,如果你用的移植版本,还有字符串加密:
-mllvm -sobf

之后直接把build目录下生成的bin, lib文件夹,复制覆盖到NDK目录的这个位置:比如:

~/Android/Sdk/ndk/16.1.4479499/toolchains/llvm/prebuilt/linux-x86_64

然后添加到CMakeList.txt中这个:

set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mllvm -sub -mllvm -sobf -mllvm -fla -mllvm -bcf")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mllvm -sub -mllvm -sobf -mllvm -fla -mllvm -bcf")

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mllvm -sub -mllvm -sobf -mllvm -fla -mllvm -bcf" )
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mllvm -sub -mllvm -sobf -mllvm -fla -mllvm -bcf" )

之后Build会报错说<asm/......>下的头文件找不到,这里在

~/Android/Sdk/ndk/16.1.4479499/build/cmake/android.toolchain.cmake

最后新添加:

if (${ANDROID_ABI} STREQUAL "x86_64")
    include_directories(${ANDROID_SYSROOT}/usr/include/x86_64-linux-android)
elseif (${ANDROID_ABI} STREQUAL "x86")
    include_directories(${ANDROID_SYSROOT}/usr/include/i686-linux-android)
elseif (${ANDROID_ABI} STREQUAL "arm64-v8a")
    include_directories(${ANDROID_SYSROOT}/usr/include/aarch64-linux-android)
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
    include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi)
endif()

就可以了。

附上我的Gradle配置文件:

app/build.gradle

android{
......
    ndk {
        abiFilters 'arm64-v8a', 'armeabi-v7a' // arm64-v8a, armeabi-v7a, x86_64, x86
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    signingConfigs {
        projname {
            keyAlias "alias-key"
            keyPassword "123456"
            storeFile file("../key.jks")
            storePassword "123456"
        }
    }
    buildTypes {
        release {
            lintOptions {
                checkReleaseBuilds false
                abortOnError false
            }
            minifyEnabled true
            signingConfig signingConfigs.projname
        }
        debug{
            minifyEnabled true
            signingConfig signingConfigs.projname
        }
    }
}

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.3.1'
    implementation "com.android.support.constraint:constraint-layout:1.1.3"
    testCompile 'junit:junit:4.12'
}

./build.gradle 

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'

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

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

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

./local.properties

ndk.dir=/home/a/Android/Sdk/ndk/16.1.4479499
sdk.dir=/home/a/Android/Sdk

app/CMakeLists.txt


cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
             Libs

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/secure-lib.cpp
        )


SET(CMAKE_CXX_FLAGS "-mllvm -fla -mllvm -sub -mllvm -bcf")


find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       Libs

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

猜你喜欢

转载自blog.csdn.net/uiop_uiop_uiop/article/details/113798136