Как добавить компилятор apk в проект androidAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Как добавить компилятор apk в проект android

Сообщение Anonymous »

Я хочу создать игровой движок для Android (ядро визуального романа).
Создал этот код для компиляции в файл dex и т. д. и архивирования в apk. Используя этот код, у меня возникли некоторые проблемы. Вот код

Код: Выделить всё

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.android.tools.r8.D8;
import com.android.tools.r8.D8Command;
import com.android.tools.r8.OutputMode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class TestCompiler {

private Context context;

public TestCompiler(Context context) {
this.context = context;
}

public boolean compileApk() {
try {
File projectDir = createProjectStructure();
if (projectDir == null) {
return false;
}

boolean success = compileToDex(projectDir);
if (!success) {
return false;
}

success = packIntoApk(projectDir);
if (!success) {
return false;
}

File apkFile = new File(projectDir, "app.apk");
if (apkFile.exists()) {
File outputApk = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "output.apk");
try (InputStream in = new FileInputStream(apkFile);
OutputStream out = new FileOutputStream(outputApk)) {
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
Toast.makeText(context, "APK compiled successfully", Toast.LENGTH_LONG).show();
return true;
} else {
Toast.makeText(context, "Failed to find APK file", Toast.LENGTH_LONG).show();
return false;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Failed to compile APK", Toast.LENGTH_LONG).show();
return false;
}
}

private File createProjectStructure() throws IOException {
File projectDir = new File(context.getCacheDir(), "MyApp");
if (projectDir.exists()) {
deleteRecursive(projectDir);
}

if (!projectDir.mkdirs()) {
return null;
}

File appDir = new File(projectDir, "app");
if (!appDir.mkdirs()) {
return null;
}

File srcDir = new File(appDir, "src/main/java/com/example/myapp");
if (!srcDir.mkdirs()) {
return null;
}

File resDir = new File(appDir, "src/main/res");
if (!resDir.mkdirs()) {
return null;
}

File manifestFile = new File(appDir, "src/main/AndroidManifest.xml");
try (FileWriter writer = new FileWriter(manifestFile)) {
writer.write("\n" +
"    \n" +
"        \n" +
"            \n" +
"                \n" +
"                \n" +
"            \n" +
"         \n" +
"    \n" +
"");
}

File mainActivityFile = new File(srcDir, "MainActivity.java");
try (FileWriter writer = new FileWriter(mainActivityFile)) {
writer.write("package com.example.myapp;\n\n" +
"import android.app.Activity;\n" +
"import android.os.Bundle;\n" +
"import android.widget.TextView;\n\n" +
"public class MainActivity extends Activity {\n" +
"    @Override\n" +
"    protected void onCreate(Bundle savedInstanceState) {\n" +
"        super.onCreate(savedInstanceState);\n" +
"        TextView textView = new TextView(this);\n" +
"        textView.setText(\"Hello, World!\");\n" +
"        setContentView(textView);\n" +
"    }\n" +
"}");
}

File valuesDir = new File(resDir, "values");
if (!valuesDir.mkdirs()) {
return null;
}

File stringsFile = new File(valuesDir, "strings.xml");
try (FileWriter writer = new FileWriter(stringsFile)) {
writer.write("\n" +
"    MyApp\n" +
"");
}

return projectDir;
}

private void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteRecursive(child);
}
}
fileOrDirectory.delete();
}

private boolean compileToDex(File projectDir) {
try {
File classesOutputDir = new File(context.getCacheDir(), "classes");
if (classesOutputDir.exists()) {
deleteRecursive(classesOutputDir);
}
classesOutputDir.mkdirs();

File srcDir = new File(projectDir, "app/src/main/java");
List sourceFiles = getJavaFiles(srcDir);
List sourceFilePaths = new ArrayList();
for (File file : sourceFiles) {
sourceFilePaths.add(file.getAbsolutePath());
}

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List compileOptions = new ArrayList();
compileOptions.add("-d");
compileOptions.add(classesOutputDir.getAbsolutePath());
compileOptions.addAll(sourceFilePaths);

int compilationResult = compiler.run(null, null, null, compileOptions.toArray(new String[0]));
if (compilationResult != 0) {
return false;
}

File dexOutputDir = new File(context.getCacheDir(), "dex");
if (dexOutputDir.exists()) {
deleteRecursive(dexOutputDir);
}
dexOutputDir.mkdirs();

File dexFile = new File(dexOutputDir, "classes.dex");

D8.run(D8Command.builder()
.addProgramFiles(Paths.get(classesOutputDir.getAbsolutePath()))
.setOutput(Paths.get(dexFile.getAbsolutePath()), OutputMode.DexFilePerClassFile)
.build());

return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

private List getJavaFiles(File dir) {
List  javaFiles = new ArrayList();
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
javaFiles.addAll(getJavaFiles(file));
} else if (file.getName().endsWith(".java")) {
javaFiles.add(file);
}
}
}
return javaFiles;
}

private boolean packIntoApk(File projectDir) {
try {
File dexFile = new File(context.getCacheDir(), "dex/classes.dex");
if (!dexFile.exists()) {
return false;
}

File apkFile = new File(projectDir, "app.apk");
try (FileOutputStream fos = new FileOutputStream(apkFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {

ZipEntry manifestEntry = new ZipEntry("AndroidManifest.xml");
zos.putNextEntry(manifestEntry);
try (InputStream in = new FileInputStream(new File(projectDir, "app/src/main/AndroidManifest.xml"))) {
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
}
zos.closeEntry();

ZipEntry dexEntry = new ZipEntry("classes.dex");
zos.putNextEntry(dexEntry);
try (InputStream in = new FileInputStream(dexFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
}
zos.closeEntry();
}

return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
Я использую пользовательские библиотеки:

Код: Выделить всё

implementation('com.zeoflow:jx:1.2.1') {
exclude group: 'jakarta.activation', module: 'jakarta.activation-api'
}
implementation('com.android.tools.build:builder:8.4.0') {
exclude group: 'com.sun.activation', module: 'javax.activation'
}
Может быть, вы знаете несколько примеров того, как создать apk-файл в ОС Android, потому что, если у нас нет root-доступа - я вижу много ошибок, таких как: доступ запрещен или разрешение отсутствует. отказано?

Подробнее здесь: https://stackoverflow.com/questions/785 ... id-project
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»