不要なコードの除去
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
はじめに
Java アプリケーションのソース JAR には、おそらく
完全に使用されていないか、一部のメソッドにのみ含まれている場合です。このデッドコードを Objective-C に翻訳する
iOS アプリが不必要に肥大化して、問題が発生する可能性が
特に Java ライブラリの 1 つで、ライブラリでサポートされない機能が使われている場合に
J2ObjC トランスレータ。
ProGuard は、圧縮、結合、
Java バイトコードを難読化します。必要に応じて、バイトコード jar を指定すると、
「usage」アプリケーションで使用されていないクラスとメソッドの一覧を表示するレポートです。J2ObjC は
変換時にこれらのクラスとメソッドをスキップするようにします。
ProGuard はこちらからダウンロードできます。
ProGuard の設定
ProGuard は構成ファイルをコマンドライン引数として受け取り、構成ファイル内で最適化を指定する
実行すべきレポートと生成するレポートを定義しますJ2ObjC はデッドコードについてのみ知る必要があるので、
最適化と無関係なロギングをすべて無効にする必要があります。渡す必要があります。
ProGuard ヘッダー テキストと使用状況レポートのみで構成されます。
まず、ソース jar に対応するアプリケーションのバイトコード jar があることを確認します。
ProGuard マニュアルからコピーした次の構成ファイルをテンプレートとして使用して、
J2ObjC が想定する出力は次のとおりです。
-injars app-bin.jar
-libraryjars /lib/rt.jar
-dontoptimize
-dontobfuscate
-dontpreverify
-printusage
-dontnote
-keep public class com.foo.app.Main {
public static void main(java.lang.String[]);
}
-keepclassmembers class * {
static final % *;
static final java.lang.String *;
}
この構成ファイルを変更して、実行すべきでないものが ProGuard によって消去されないようにします。
jar が単なるライブラリではなくアプリケーションの場合は、上記のように main()
メソッドを指定します。
ここで実行すると、必要なすべてのものが保持されます。Chronicle SOAR から生成される出力を
サニティ チェックとしての ProGuard。
ProGuard の実行
アプリケーションのバイトコード JAR とカスタマイズされた ProGuard 構成ファイル(
usage.pg
など)、次のコマンドを使用して使用状況レポート ファイルを作成できます。
java -jar proguard.jar @usage.pg > usage.log
これにより、アプリケーションで使用されていないクラスとメソッドをリストする usage.log
というファイルが作成されます。
デッドコードを除去して J2ObjC を実行する
ProGuard の使用状況レポートを入手したので、次の場所で不要なコードの排除フェーズを有効にできます。
コマンドライン フラグ --dead-code-report <file>
を使用した J2ObjC。DeadCodeEliminator 変換
フェーズでは使用状況レポートを使用して、変換前に各ソースファイルからデッドコードを削除します。
Objective-C:
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-25 UTC。"],[[["\u003cp\u003eJ2ObjC can leverage ProGuard's usage reports to identify and eliminate unused Java code during the translation to Objective-C, resulting in smaller and more efficient iOS applications.\u003c/p\u003e\n"],["\u003cp\u003eProGuard needs to be configured to solely generate a usage report, disabling optimizations and extraneous logging, to provide J2ObjC with the necessary information on unused classes and methods.\u003c/p\u003e\n"],["\u003cp\u003eA ProGuard usage report can be created by running ProGuard with a specific configuration file and redirecting its output to a file, which is then used by J2ObjC.\u003c/p\u003e\n"],["\u003cp\u003eJ2ObjC's dead code elimination is activated using the \u003ccode\u003e--dead-code-report\u003c/code\u003e flag, followed by the path to the ProGuard usage report file generated in the previous steps.\u003c/p\u003e\n"]]],[],null,["# Dead Code Elimination\n\nIntroduction\n------------\n\nYour Java application's source jar probably contains a huge number of source files that are either\ncompletely unused or included for just a few methods. Translating this dead code into Objective-C\nwill bloat your iOS application unnecessarily and increase the likelihood of encountering\ntranslation errors, especially if one of your Java libraries uses features not supported by the\nJ2ObjC translator.\n\n[ProGuard](http://proguard.sourceforge.net/) is an open source tool that helps you shrink,\nobfuscate, and otherwise mangle Java bytecode. Optionally, given a bytecode jar, it can print out a\n\"usage\" report listing all of the unused classes and methods in your application. J2ObjC can use\nsuch a report to skip these classes and methods during translation.\n\nProGuard can be downloaded [here](http://proguard.sourceforge.net/index.html#downloads.html).\n\nConfiguring ProGuard\n--------------------\n\nProGuard accepts a configuration file as a command-line argument that specifies the optimizations it\nshould perform and the reports it should generate. Since J2ObjC only needs to know about dead code,\nyou should disable all optimizations and extraneous logging; the file passed to J2ObjC should\nconsist only of the ProGuard header text and the usage report.\n\nFirst, ensure that you have a bytecode jar for your application that corresponds to the source jar.\nYou can use the following configuration file, copied from the [ProGuard manual](http://proguard.sourceforge.net/index.html#manual/examples.html), as a template to produce the\noutput that J2ObjC expects: \n\n -injars app-bin.jar\n -libraryjars /lib/rt.jar\n\n -dontoptimize\n -dontobfuscate\n -dontpreverify\n -printusage\n -dontnote\n\n -keep public class com.foo.app.Main {\n public static void main(java.lang.String[]);\n }\n\n -keepclassmembers class * {\n static final % *;\n static final java.lang.String *;\n }\n\nModify this configuration file to ensure that ProGuard doesn't eliminate anything that it shouldn't.\nIf your jar is an application and not just a library, then specifying the `main()` method as we have\ndone here should keep everything that is necessary; you can examine the resulting output from\nProGuard as a sanity check.\n\nRunning ProGuard\n----------------\n\nOnce you have your application's bytecode jar and a customized ProGuard configuration file (called,\nsay, `usage.pg`), you can create a usage report file with the following command: \n\n java -jar proguard.jar @usage.pg \u003e usage.log\n\nThis will create a file called `usage.log` that lists your application's unused classes and methods.\n\nRunning J2ObjC with dead code elimination\n-----------------------------------------\n\nNow that you have the ProGuard usage report, you can enable the dead code elimination phase in\nJ2ObjC using the command-line flag `--dead-code-report \u003cfile\u003e`. The DeadCodeEliminator translation\nphase will use the usage report to remove dead code from each source file prior to translation to\nObjective-C."]]