【Android-NDK】(1),2024年最新字节跳动测试开发面试问题及答案

NDK开发环境
3. CMake
构建工具

二:新创建NDK项目

File->new->New Project,自定义项目名称什么的,其他的默认即可。

  1. 自动生成的主要是四个文件

  1. MainActivity.java

public class MainActivity extends AppCompatActivity {

// Used to load the ‘native-lib’ library on application startup.
static {
// 加载的so库,对应的so名称为libnative-lib.so,掐头去尾了
System.loadLibrary(“native-lib”);
}

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

// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}

/**

  • native关键字,声明了jni调用的方法
    */
    public native String stringFromJNI();
    }
  1. CMakeLists.txt

For more information about using CMake with Android Studio, read the

documentation: https://d.android.com/studio/projects/add-native-code.html

Sets the minimum version of CMake required to build the native library.

cmake支持的最低版本

cmake_minimum_required(VERSION 3.10.2)

Declares and names the project.

#项目名称
project(“ndktest”)

Creates and names a library, sets it as either STATIC

or SHARED, and provides the relative paths to its source code.

You can define multiple libraries, and CMake builds them for you.

Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.

生成的so库名称,此处生成的so文件名称是libnative-lib.so

native-lib

Sets the library as a shared library.

STATIC:静态库,是目标文件的归档文件,在链接其他目标的时候使用

SHARED:动态库,会被动态链接,在运行时被加载

MODULE:模块库,是不会被链接到其他目标中的插件,但是可能会在运行时使用dlopen-系列的函数动态链接

SHARED

Provides a relative path to your source file(s).

资源文件,可以多个

native-lib.cpp )

Searches for a specified prebuilt library and stores the path as a

variable. Because CMake includes system libraries in the search path by

default, you only need to specify the name of the public NDK library

you want to add. CMake verifies that the library exists before

completing its build.

从系统查找依赖库

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

Specifies the name of the NDK library that

you want CMake to locate.

log )

Specifies libraries CMake should link to your target library. You

can link multiple libraries, such as libraries you define in this

build script, prebuilt third-party libraries, or system libraries.

库的依赖

target_link_libraries( # Specifies the target library.
native-lib

Links the target library to the log library

included in the NDK.

${log-lib} )

  1. native-lib.cpp

#include <jni.h>
#include

extern “C” JNIEXPORT jstring JNICALL
Java_com_kongge_ndktest_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = “Hello from C++”;
return env->NewStringUTF(hello.c_str());
}

  1. build.gradle


android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags “” // 命令参数,非必须
}
}
}

externalNativeBuild {
cmake {
path “src/main/cpp/CMakeLists.txt” // 指定了该文件的路径,必须
version “3.10.2” // 支持的最低版本,非必须
}
}
}

5.构建或者运行之后,会生成对应的so文件
image

三:现有项目手动集成NDK

  1. 创建存放c文件的目录

src/mian 目录上右键->new->Directory->jni,创建c文件,我这里创建DataEncryption.cpp
image

  1. 新建CMakeLists.txt

这个位置可以随意放,我是在app根目录或者module的根目录创建的CMakeLists.txt文件,后面可以在build.gradle里面配置路径。
image

CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.4.1)

add_library(DataEncryptionLib SHARED src/main/jni/DataEncryption.cpp )

  1. 在build.gradle中配置CMakeLists.txt的路径。

app根目录或者module根目录下的build.gradle中配置:

android {

externalNativeBuild {
cmake {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

结语

网上高级工程师面试相关文章鱼龙混杂,要么一堆内容,要么内容质量太浅, 鉴于此我整理了上述安卓开发高级工程师面试题以及答案。希望帮助大家顺利进阶为高级工程师。
目前我就职于某大厂安卓高级工程师职位,在当下大环境下也想为安卓工程师出一份力,通过我的技术经验整理了面试经常问的题,答案部分是一篇文章或者几篇文章,都是我认真看过并且觉得不错才整理出来。

大家知道高级工程师不会像刚入门那样被问的问题一句话两句话就能表述清楚,所以我通过过滤好文章来帮助大家理解。

1307页字节跳动Android面试真题解析火爆全网,完整版开放下载

现在都说互联网寒冬,其实只要自身技术能力够强,咱们就不怕!我这边专门针对Android开发工程师整理了一套【Android进阶学习视频】、【全套Android面试秘籍】、【Android知识点PDF】。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
师整理了一套【Android进阶学习视频】、【全套Android面试秘籍】、【Android知识点PDF】。**

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-xNWrGK4j-1712683365836)]

  • 29
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值