Conditional compilation issues for Android platform Java code

Source: Internet
Author: User

In the code development process, whether it is C + + or Java, it is necessary to face the problem of porting the platform, so it is inevitable to consider the issue of conditional compilation. The implementation of conditional compilation is easy to implement in the development of C + + code, and can be divided into two types: one in a single code file #ifdef ... #endif or #if ... #endif way to selectively compile the code in a file, The other is to selectively compile a file in makefile by macro judgment. In the development of Java code, the implementation of conditional compilation does not have a direct corresponding method, but can still be used to implement conditional compilation.

For conditional compilation of code within a single file, you can use the optimizations of Java code to implement:


  Private Final Boolean DEBUG = false;  if (DEBUG) {     log.i ("TEST", "Hello,world");  }

Here, because debug is a final variable, which is equivalent to a const variable in C + +,
So the Java compiler optimizes the code when it compiles, and when debug is False, the code inside the if is not compiled.
However, there is a flaw in this approach, that is, it is not possible to implement conditional compilation of functions, and this time, Java developers may be envious of C # developers, because in C # can be like a C + + implementation of conditional compilation functions.

The following focuses on how to implement selective compilation of some Java code files in the development of Android Java code.

In Android.mk, there is a line of script that you should be very familiar with:

Local_src_files: = $ (call All-java-files-under, SRC)

This code means to find all Java files from the SRC directory and save them to Local_src_files


So we can selectively add platform-related code to local_src_files.

Here's how:
Create the following directory structure:


│android.mk


└─platform
│android.mk

├─bb
││android.mk
││
│└─src
└─aa
│android.mk

└─src


The contents of the platform/android.mk are as follows:


Include $ (call All-makefiles-under, $ (local_path)/platform)


The local_path here are defined in the android.mk of the root directory Local_path

Execution will contain all android.mk under the platform subdirectory
Platform-dependent Java code in the platform directory

The android.mk content under the PLATFORM/AA directory is as follows:
Ifeq ($ (target_product), aa) Platform_dir: = AA Endif

The android.mk content under the Platform/bb directory is as follows:
Ifeq ($ (target_product), bb) platform_dir: = BB endif


Platform the platform-dependent subdirectory android.mk file will determine whether the platform meets the requirements, if required, will be assigned to the directory Platform_dir variable.

Look at the contents of the root directory under Android.mk as follows:

Local_path:= $ (call My-dir) include $ (clear_vars) ... Local_src_files: = $ (call All-java-files-under, SRC) include $ (local_path)/platform/android.mk Local_src_files + = $ ( Call All-java-files-under, platform/$ (mta_platform_dir)/src) ...

The first line of red code contains PLATFORM/ANDROID.MK, and the executed script will define the PLATFORM_DIR variable based on the platform. The second line of code is to include all the Java code in the platform-related SRC directory.


In this way, the Java code that compiles the different files is implemented.



Another way to implement conditional compilation is to compile the platform-related code into a static Java library,

method is to use

Include $ (build_static_java_library) instead of include $ (build_java_library)

The include $ (build_java_library) will compile the jar package directly. The include $ (build_static_java_library) also generates a JAR package,
However, when compiled by other apk, if the library is included through Local_static_java_libraries, it will also compile the code statically into the APK without relying on the additional jar package.
But this approach is significantly more complex and interesting to try.

Conditional compilation issues for Android platform Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.