Skip to content

Instantly share code, notes, and snippets.

@daniaDlbani
Created February 13, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daniaDlbani/6ebd0014583f870982c73ca15db579b3 to your computer and use it in GitHub Desktop.
Save daniaDlbani/6ebd0014583f870982c73ca15db579b3 to your computer and use it in GitHub Desktop.
Android Make generator for Cocos2dx 2.2.6 Android studio project
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Android Make file generator for cocos2dx 2.2.6 project running with Android studio:
* 1.copy AndroidMakeGenerator.java to your jni folder
* 2.open terminal and compile it javac AndroidMakeGenerator.java
* 3.run output class java AndroidMakeGenerator then an output file android.mk file will be created under jni folder
*/
public class AndroidMakeGenerator {
/**
* @param args
*/
static AndroidMakeGenerator runner;
static String result;
String before = "LOCAL_PATH := $(call my-dir)\n\ninclude $(CLEAR_VARS)\n\n# !!!! dangerous, ONLY for debuging c++ with Android Studio\nifeq ($(NDK_DEBUG),1)\n LOCAL_ALLOW_UNDEFINED_SYMBOLS := true\nendif\n# !!!! dangerous!!!!\n\nLOCAL_MODULE := cocos2dcpp_shared" +
"\n\nLOCAL_MODULE_FILENAME := libcocos2dcpp\n\n";
String classes = "LOCAL_SRC_FILES := ";
String headers = "\n\n\nLOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes/\\\n" +
"\t\t\t$(LOCAL_PATH)/../../../../../external/Box2D/\\\n" +
"\t\t\t$(LOCAL_PATH)/../../../../../extensions/GUI/\\\n"+
"\t\t\t$(LOCAL_PATH)/../../../../../extensions/GUI/CCControlExtension/\\\n"+
"\t\t\t$(LOCAL_PATH)/../../../../../extensions/GUI/CCEditBox/";
String after = "\n\n\n" +
"LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static\n"+
"LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static\n"+
"LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static\n"+
"LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static\n"+
"LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static\n\n"+
"include $(BUILD_SHARED_LIBRARY)\n\n"+
"$(call import-module,cocos2dx)\n"+
"$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)\n"+
"$(call import-module,CocosDenshion/android)\n"+
"$(call import-module,extensions)\n"+
"$(call import-module,external/Box2D)\n"+
"$(call import-module,external/chipmunk)\n";
static String prefixString = "\t\t\t../../../Classes/";
static String headerPrefixString = "../../../Classes/";
public static void main(String[] args) {
// TODO Auto-generated method stub
runner = new AndroidMakeGenerator();
result = "";
// rename the Android.mk file to Android_BAK.mk
File oldfile =new File(System.getProperty("user.dir")+"/Android.mk");
File newfile =new File(System.getProperty("user.dir")+"/Android_BAK.mk");
try{
if(oldfile.renameTo(newfile)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
}catch (Exception et) {
et.printStackTrace();
}
//System.out.println("Current dir : "+System.getProperty("user.dir"));
//AndroidMakeGenerator runner = new AndroidMakeGenerator();
String path;
Path currentRelativePath = Paths.get(System.getProperty("user.dir"));
currentRelativePath = currentRelativePath.getParent().getParent().getParent();
String s = currentRelativePath.toAbsolutePath().toString();
//System.out.println("Path is : " + s);
path = s +"/Classes";
//System.out.println("Path to Classes : " + path);
// making new Android.mk file
String classConcat;
prefixString = "\t\t\t../../../Classes/";
headerPrefixString = "../../../Classes/";
runner.readFilesFromDir(path,runner.classes);
path = s +"//proj.android-studio";
prefixString = "\t\t\t../../../proj.android-studio/";
headerPrefixString = "../../../proj.android-studio/";
runner.readFilesFromDir(path, runner.classes);
classConcat = runner.classes + result;
try {
String content = "";
content = runner.before + classConcat + runner.headers + runner.after;
File file = new File(System.getProperty("user.dir")+"/Android.mk");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFilesFromDir(String path, String classesStart){
//System.out.println("readFilesFromDir Path : " + path);
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
String returnStr="";
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
String fileName = listOfFiles[i].getName();
if (fileName.split("\\.").length > 1 && fileName.split("\\.")[1].equals("cpp"))
{
//System.out.println("File " + listOfFiles[i].getName());
returnStr = prefixString + listOfFiles[i].getName() + " \\\n";
result += returnStr;
}
} else if (listOfFiles[i].isDirectory())
{
if(listOfFiles[i].getName().contains("obj") ||
listOfFiles[i].getName().contains("bin") ||
listOfFiles[i].getName().contains("gen") ||
listOfFiles[i].getName().startsWith(".") || // ignore hidden folder
listOfFiles[i].getName().contains("build") ||
listOfFiles[i].getName().contains("src") ||
listOfFiles[i].getName().equals("res") ||
listOfFiles[i].getName().equals("gradle") ||
listOfFiles[i].getName().contains("assets"))
continue;
String oldPath = path;
path += "/" + listOfFiles[i].getName();
String oldPrefix = prefixString;
prefixString += listOfFiles[i].getName() + "/";
String oldHeaderPrefix = headerPrefixString;
headerPrefixString += listOfFiles[i].getName() + "/";
runner.headers += "\\\n\t\t\t$(LOCAL_PATH)/" + headerPrefixString;
runner.readFilesFromDir(path, runner.classes);
path = oldPath;
prefixString = oldPrefix;
headerPrefixString = oldHeaderPrefix;
}
}
}
}
@daniaDlbani
Copy link
Author

Android Make file generator for cocos2dx 2.2.6 project running with Android studio:

  1. Copy AndroidMakeGenerator.java to your proj.android-studio -> app -> jni folder
  2. Open terminal and compile it javac AndroidMakeGenerator.java
  3. Run output class java AndroidMakeGenerator then an output file android.mk file will be created under jni folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment